-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetchModuleSchemas.js
54 lines (44 loc) · 1.49 KB
/
fetchModuleSchemas.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
import { parse } from 'yaml'
import { pascalCase } from "change-case";
import fs from "node:fs"
const modulesDir = "./src-tsp/modules"
const res = await fetch("https://blue-build.org/modules.json")
const modules = await res.json()
let moduleImports = []
let moduleModels = []
fs.existsSync(modulesDir) && fs.rmSync(modulesDir, { recursive: true });
fs.mkdirSync(modulesDir)
for (const module of modules) {
if (module.tsp === "") continue
const res = await fetch(module.tsp)
let text = await res.text()
text = text // add `...ModuleDefaults;` after the model type
.split("\n")
.flatMap(line => {
if (line.trimStart().startsWith("model")) {
return [
`@extension("additionalProperties", false)`,
line
];
}
if (line.trimStart().startsWith(`type: "${module.name}"`)) {
return [
line,
'',
' ...ModuleDefaults; // added by fetchModuleSchemas.js',
];
} else {
return line;
}
})
.join('\n')
fs.writeFileSync(`${modulesDir}/${module.name}.tsp`, text)
moduleImports.push(`${module.name}.tsp`)
moduleModels.push(`${pascalCase(module.name)}`)
}
fs.writeFileSync(`${modulesDir}/index.tsp`,
moduleImports.map(m => `import "./${m}";`).join("\n") + `
union RepoModule {
${moduleModels.map(m => ` ${m}Module`).join(",\n")}
}`
)