1 |
|
2 |
|
3 | const { promises: fsp } = require('fs');
|
4 | const { join, basename, relative } = require('path');
|
5 | const { compile } = require('json-schema-to-typescript');
|
6 | const camelcase = require('camelcase');
|
7 | const parameterSchema = require('../schemas/parameter-config.json');
|
8 |
|
9 | const configSchemas = ['display-config.json', 'serverscript-config.json'];
|
10 | const log = console.log;
|
11 | const writeJSON = (path, value) => fsp.writeFile(path, `${JSON.stringify(value, null, ' ')}\n`);
|
12 |
|
13 | async function bundleSchemas() {
|
14 | for (const filename of configSchemas) {
|
15 |
|
16 | const schemaPath = join(__dirname, '../schemas', filename);
|
17 |
|
18 | log(`Updating schema at '${relative(process.cwd(), schemaPath)}'`);
|
19 |
|
20 | const schema = require(schemaPath);
|
21 | schema.properties.parameters.items = {
|
22 | $comment: "NOTE: This is imported from './parameter-config.json'",
|
23 | ...parameterSchema,
|
24 | $schema: undefined,
|
25 | };
|
26 |
|
27 | await writeJSON(schemaPath, schema);
|
28 |
|
29 |
|
30 | const schemaName = basename(schemaPath, '.json');
|
31 | const typeName = camelcase(schemaName, { pascalCase: true });
|
32 | const dtsPath = join(__dirname, '../types/schemas/', `${schemaName}.d.ts`);
|
33 |
|
34 | log(`Updating type definitions at '${relative(process.cwd(), dtsPath)}'`);
|
35 |
|
36 | const compiled = await compile(schema, typeName, { format: false });
|
37 | await fsp.writeFile(dtsPath, compiled);
|
38 | }
|
39 |
|
40 | log('Done');
|
41 | }
|
42 |
|
43 | module.exports = bundleSchemas;
|
44 | if (require.main === module) {
|
45 | bundleSchemas().catch((error) => {
|
46 | console.error(error);
|
47 | process.exitCode = 1;
|
48 | });
|
49 | }
|