Skip to content

Commit 7a70fe2

Browse files
committed
add main function
1 parent f910189 commit 7a70fe2

File tree

1 file changed

+99
-84
lines changed

1 file changed

+99
-84
lines changed

scripts/build.js

Lines changed: 99 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
import fs from "fs";
2+
import path from "path";
23
import yaml from "js-yaml";
34
import mergeAllOf from "json-schema-merge-allof";
45
import { dereferenceDocument } from "@open-rpc/schema-utils-js";
56

7+
function listYamlFiles(baseDir) {
8+
return fs.readdirSync(baseDir).filter((entry) => {
9+
const fullPath = path.join(baseDir, entry);
10+
return fs.statSync(fullPath).isFile() && /\.ya?ml$/i.test(entry);
11+
});
12+
}
13+
614
function sortByMethodName(methods) {
715
return methods.slice().sort((a, b) => {
816
if (a['name'] > b['name']) {
@@ -17,101 +25,108 @@ function sortByMethodName(methods) {
1725

1826
console.log("Loading files...\n");
1927

20-
let methods = [];
21-
let methodsBase = "src/eth/";
22-
let methodFiles = fs.readdirSync(methodsBase);
23-
methodFiles.forEach(file => {
24-
console.log(file);
25-
let raw = fs.readFileSync(methodsBase + file);
26-
let parsed = yaml.load(raw);
27-
methods = [
28-
...methods,
29-
...parsed,
30-
];
31-
});
28+
async function main() {
29+
let methods = [];
30+
let methodsBase = "src/eth/";
31+
let methodFiles = listYamlFiles(methodsBase);
32+
methodFiles.forEach(file => {
33+
console.log(file);
34+
let raw = fs.readFileSync(path.join(methodsBase, file));
35+
let parsed = yaml.load(raw);
36+
methods = [
37+
...methods,
38+
...parsed,
39+
];
40+
});
3241

33-
methodsBase = "src/debug/";
34-
methodFiles = fs.readdirSync(methodsBase);
35-
methodFiles.forEach(file => {
36-
console.log(file);
37-
let raw = fs.readFileSync(methodsBase + file);
38-
let parsed = yaml.load(raw);
39-
methods = [
40-
...methods,
41-
...parsed,
42-
];
43-
});
42+
methodsBase = "src/debug/";
43+
methodFiles = listYamlFiles(methodsBase);
44+
methodFiles.forEach(file => {
45+
console.log(file);
46+
let raw = fs.readFileSync(path.join(methodsBase, file));
47+
let parsed = yaml.load(raw);
48+
methods = [
49+
...methods,
50+
...parsed,
51+
];
52+
});
4453

45-
methodsBase = "src/engine/openrpc/methods/";
46-
methodFiles = fs.readdirSync(methodsBase);
47-
methodFiles.forEach(file => {
48-
console.log(file);
49-
let raw = fs.readFileSync(methodsBase + file);
50-
let parsed = yaml.load(raw);
51-
methods = [
52-
...methods,
53-
...parsed,
54-
];
55-
});
54+
methodsBase = "src/engine/openrpc/methods/";
55+
methodFiles = listYamlFiles(methodsBase);
56+
methodFiles.forEach(file => {
57+
console.log(file);
58+
let raw = fs.readFileSync(path.join(methodsBase, file));
59+
let parsed = yaml.load(raw);
60+
methods = [
61+
...methods,
62+
...parsed,
63+
];
64+
});
5665

57-
let schemas = {};
58-
let schemasBase = "src/schemas/"
59-
let schemaFiles = fs.readdirSync(schemasBase);
60-
schemaFiles.forEach(file => {
61-
console.log(file);
62-
let raw = fs.readFileSync(schemasBase + file);
63-
let parsed = yaml.load(raw);
64-
schemas = {
65-
...schemas,
66-
...parsed,
67-
};
68-
});
66+
let schemas = {};
67+
let schemasBase = "src/schemas/"
68+
let schemaFiles = listYamlFiles(schemasBase);
69+
schemaFiles.forEach(file => {
70+
console.log(file);
71+
let raw = fs.readFileSync(path.join(schemasBase, file));
72+
let parsed = yaml.load(raw);
73+
schemas = {
74+
...schemas,
75+
...parsed,
76+
};
77+
});
6978

70-
schemasBase = "src/engine/openrpc/schemas/"
71-
schemaFiles = fs.readdirSync(schemasBase);
72-
schemaFiles.forEach(file => {
73-
console.log(file);
74-
let raw = fs.readFileSync(schemasBase + file);
75-
let parsed = yaml.load(raw);
76-
schemas = {
77-
...schemas,
78-
...parsed,
79-
};
80-
});
79+
schemasBase = "src/engine/openrpc/schemas/"
80+
schemaFiles = listYamlFiles(schemasBase);
81+
schemaFiles.forEach(file => {
82+
console.log(file);
83+
let raw = fs.readFileSync(path.join(schemasBase, file));
84+
let parsed = yaml.load(raw);
85+
schemas = {
86+
...schemas,
87+
...parsed,
88+
};
89+
});
8190

82-
const doc = {
83-
openrpc: "1.2.4",
84-
info: {
85-
title: "Ethereum JSON-RPC Specification",
86-
description: "A specification of the standard interface for Ethereum clients.",
87-
license: {
88-
name: "CC0-1.0",
89-
url: "https://creativecommons.org/publicdomain/zero/1.0/legalcode"
91+
const doc = {
92+
openrpc: "1.2.4",
93+
info: {
94+
title: "Ethereum JSON-RPC Specification",
95+
description: "A specification of the standard interface for Ethereum clients.",
96+
license: {
97+
name: "CC0-1.0",
98+
url: "https://creativecommons.org/publicdomain/zero/1.0/legalcode"
99+
},
100+
version: "0.0.0"
90101
},
91-
version: "0.0.0"
92-
},
93-
methods: sortByMethodName(methods),
94-
components: {
95-
schemas: schemas
102+
methods: sortByMethodName(methods),
103+
components: {
104+
schemas: schemas
105+
}
96106
}
97-
}
98107

99-
fs.writeFileSync('refs-openrpc.json', JSON.stringify(doc, null, '\t'));
108+
fs.writeFileSync('refs-openrpc.json', JSON.stringify(doc, null, '\t'));
100109

101-
let spec = await dereferenceDocument(doc);
110+
let spec = await dereferenceDocument(doc);
102111

103-
spec.components = {};
112+
spec.components = {};
104113

105-
// Merge instances of `allOf` in methods.
106-
for (var i=0; i < spec.methods.length; i++) {
107-
for (var j=0; j < spec.methods[i].params.length; j++) {
108-
spec.methods[i].params[j].schema = mergeAllOf(spec.methods[i].params[j].schema);
114+
// Merge instances of `allOf` in methods.
115+
for (var i=0; i < spec.methods.length; i++) {
116+
for (var j=0; j < spec.methods[i].params.length; j++) {
117+
spec.methods[i].params[j].schema = mergeAllOf(spec.methods[i].params[j].schema);
118+
}
119+
spec.methods[i].result.schema = mergeAllOf(spec.methods[i].result.schema);
109120
}
110-
spec.methods[i].result.schema = mergeAllOf(spec.methods[i].result.schema);
111-
}
112121

113-
let data = JSON.stringify(spec, null, '\t');
114-
fs.writeFileSync('openrpc.json', data);
122+
let data = JSON.stringify(spec, null, '\t');
123+
fs.writeFileSync('openrpc.json', data);
115124

116-
console.log();
117-
console.log("Build successful.");
125+
console.log();
126+
console.log("Build successful.");
127+
}
128+
129+
main().catch((error) => {
130+
console.error(error);
131+
process.exit(1);
132+
});

0 commit comments

Comments
 (0)