forked from openshift/console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-schema.ts
74 lines (62 loc) · 2.73 KB
/
generate-schema.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
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
68
69
70
71
72
73
74
import * as fs from 'fs';
import * as path from 'path';
import chalk from 'chalk';
import * as tsj from 'ts-json-schema-generator';
import { CodeRefTypeReferenceParser } from './parsers/CodeRefTypeReferenceParser';
import { ConstructorTypeParser } from './parsers/ConstructorTypeParser';
import { ExtensionDeclarationParser } from './parsers/ExtensionDeclarationParser';
import { resolvePath, relativePath } from './utils/path';
import { getConsoleTypeResolver } from './utils/type-resolver';
import { getSchemaGeneratorConfig, getProgram } from './utils/typescript';
type SchemaTypeConfig = {
srcFile: string;
typeName: string;
/** Set to `true` when the generated schema references SupportedExtension union type. */
handleConsoleExtensions?: boolean;
};
const typeConfigs: SchemaTypeConfig[] = [
{
srcFile: 'src/schema/console-extensions.ts',
typeName: 'ConsoleExtensionsJSON',
handleConsoleExtensions: true,
},
{
srcFile: 'src/schema/plugin-manifest.ts',
typeName: 'ConsolePluginManifestJSON',
},
];
const generateSchema = ({ srcFile, typeName, handleConsoleExtensions }: SchemaTypeConfig) => {
const config = getSchemaGeneratorConfig(srcFile, typeName);
const program = getProgram(config);
const typeChecker = program.getTypeChecker();
const annotationsReader = new tsj.ExtendedAnnotationsReader(typeChecker);
const consoleTypeResolver = getConsoleTypeResolver(program);
const parser = tsj.createParser(program, config, (p) => {
p.addNodeParser(new ConstructorTypeParser());
if (handleConsoleExtensions) {
const consoleTypeDeclarations = consoleTypeResolver.getDeclarations();
const consoleExtensions = consoleTypeResolver.getConsoleExtensions(true).result;
const getMainParser = () => parser;
p.addNodeParser(
new CodeRefTypeReferenceParser(typeChecker, consoleTypeDeclarations, getMainParser),
);
p.addNodeParser(
new ExtensionDeclarationParser(annotationsReader, consoleExtensions, getMainParser),
);
}
});
const formatter = tsj.createFormatter(config);
const generator = new tsj.SchemaGenerator(program, parser, formatter, config);
return generator.createSchema(typeName);
};
console.log('Generating Console plugin JSON schemas');
typeConfigs.forEach((tc) => {
const schema = generateSchema(tc);
const schemaString = JSON.stringify(schema, null, 2);
const outPath = resolvePath(`generated/schema/${path.parse(tc.srcFile).name}`);
fs.mkdirSync(path.dirname(outPath), { recursive: true });
fs.writeFileSync(`${outPath}.json`, schemaString);
fs.writeFileSync(`${outPath}.cjs`, `exports.default = ${schemaString};`);
console.log(chalk.green(relativePath(`${outPath}.json`)));
console.log(chalk.green(relativePath(`${outPath}.cjs`)));
});