-
Notifications
You must be signed in to change notification settings - Fork 2
/
generate_types.ts
41 lines (37 loc) · 1.45 KB
/
generate_types.ts
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
import { mkdirSync, readdirSync, writeFileSync } from 'fs';
import { compileFromFile } from 'json-schema-to-typescript';
async function generate(): Promise<void> {
try {
const dirs = readdirSync('./definitions', { withFileTypes: true }).filter((d) => d.isDirectory());
const index = [];
for (const dir of dirs) {
mkdirSync(`./types/${dir.name}`, { recursive: true });
const dirTypes = [];
const files = readdirSync(`./definitions/${dir.name}`);
for (const file of files) {
const name = file.replace(/.json$/, '');
console.log(`${dir.name}/${name}`);
const ts = await compileFromFile(
`./definitions/${dir.name}/${name}.json`,
{
cwd: `./definitions/${dir.name}`,
},
)
writeFileSync(`./types/${dir.name}/${name}.d.ts`, ts);
dirTypes.push(name);
}
// Creating the index.d.ts file for each group.
const subIndex = [];
subIndex.push(...dirTypes.map((t) => `import { ${t} as ${t}_ } from './${t}'\n`));
subIndex.push(`\nexport namespace ${dir.name} {\n`);
subIndex.push(...dirTypes.map((t) => ` interface ${t} extends ${t}_ {}\n`));
subIndex.push(`}\n`)
writeFileSync(`./types/${dir.name}/index.d.ts`, `${subIndex.join('')}`);
index.push(`export * from './${dir.name}';`);
}
writeFileSync(`./types/index.d.ts`, `${index.join('\n')}\n`);
} catch (err) {
console.log(err);
}
}
generate();