Skip to content

Commit b418e5e

Browse files
Add generate schema
1 parent 2aa9a1d commit b418e5e

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

automation/generate-schema.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import * as TJS from 'typescript-json-schema';
2+
import * as path from 'path'
3+
import * as fs from 'fs'
4+
import _ from 'lodash';
5+
import {deepMapObject} from '@freephoenix888/deep-map-object';
6+
import yargs from 'yargs/yargs';
7+
import { hideBin } from 'yargs/helpers';
8+
import { program } from 'commander';
9+
import { createRequire } from 'node:module';
10+
const require = createRequire(import.meta.url);
11+
12+
13+
main()
14+
15+
async function main () {
16+
const parsedArguments = await parseArguments();
17+
const settings: TJS.PartialArgs = {
18+
required: true,
19+
titles: true
20+
};
21+
22+
const compilerOptions: TJS.CompilerOptions = {
23+
strictNullChecks: false,
24+
skipLibCheck: true
25+
};
26+
27+
const definitionFilePath = require.resolve(parsedArguments.interfaceFilePath);
28+
29+
const program = TJS.getProgramFromFiles([definitionFilePath], compilerOptions);
30+
31+
let schema = TJS.generateSchema(program, parsedArguments.interfaceName, settings);
32+
if(!schema) {
33+
throw new Error("Failed to generate schema")
34+
}
35+
36+
schema = await deepMapObject(schema, ({key, value}) => ({newKey: key, newValue: (key === 'title') ? _.upperFirst(value) : value}));
37+
fs.writeFileSync(path.resolve(parsedArguments.outputJsonFilePath), JSON.stringify(schema, null, 2));
38+
}
39+
40+
async function parseArguments(): Promise<{interfaceFilePath: string, interfaceName: string, outputJsonFilePath: string}> {
41+
program
42+
.requiredOption('--interface-file-path <path>', 'File path to the interface file which will be passed to require.resolve. For example @capacitor/action-sheet/dist/esm/definitions.d.ts')
43+
.requiredOption('--interface-name <name>', 'Interface name')
44+
.requiredOption('--output-json-file-path <path>', 'Output json file path')
45+
.parse(process.argv);
46+
47+
const options = program.opts();
48+
49+
return {
50+
interfaceFilePath: options.interfaceFilePath,
51+
interfaceName: options.interfaceName,
52+
outputJsonFilePath: options.outputJsonFilePath
53+
};
54+
}
55+
56+

0 commit comments

Comments
 (0)