-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathcheck_component_uniqueness.js
executable file
·67 lines (59 loc) · 2.23 KB
/
check_component_uniqueness.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
/**
* Updates one object with contents of the other.
* Object expected to have nesting: { components: { schemas: { ... }, errors: { ... } } }
* @param {*} globalComponents the object to be updated
* @param {*} newComponents the object to be added to `globalComponents
* @returns Number of encountered duplicates.
*/
function addComponents(globalComponents, newComponents, newComponentsOrigin) {
let duplicates = 0;
for (const componentType in newComponents) {
if (!(componentType in globalComponents)) {
globalComponents[componentType] = {};
for (const componentName in newComponents[componentType]) {
globalComponents[componentType][componentName] = newComponentsOrigin;
}
continue;
}
for (const componentName in newComponents[componentType]) {
const newComponent = newComponents[componentType][componentName];
const isReference = "$ref" in newComponent;
if (componentName in globalComponents[componentType] && !isReference) {
const previousLocation = globalComponents[componentType][componentName];
duplicates++;
console.error(
`Duplicate entry in ${componentType}: ${componentName} defined in ${previousLocation} and ${newComponentsOrigin}`,
);
} else if (!isReference) {
globalComponents[componentType][componentName] = newComponentsOrigin;
}
}
}
return duplicates;
}
function main() {
const globalComponents = {};
const specDir = "api";
let duplicates = 0;
console.error(
"Temporarily ignoring starknet_metadata.json in uniqueness check. Perhaps remove it or use it better.",
);
for (const fileName of fs.readdirSync(specDir)) {
if (fileName.endsWith(".json") && fileName !== "starknet_metadata.json") {
const filePath = path.join(specDir, fileName);
const rawSpec = fs.readFileSync(filePath);
const specContent = JSON.parse(rawSpec);
const newComponents = specContent["components"];
duplicates += addComponents(globalComponents, newComponents, filePath);
}
}
if (duplicates > 0) {
process.exit(duplicates);
} else {
console.log("No duplicate component definitions!");
}
}
main();