-
Notifications
You must be signed in to change notification settings - Fork 4
/
generate-schema.ts
63 lines (54 loc) · 1.94 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
import * as TJS from 'typescript-json-schema';
import * as path from 'path'
import * as fs from 'fs'
import * as _ from 'lodash';
import {deepMapObject} from '@freephoenix888/deep-map-object';
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');
main()
async function main () {
const parsedArguments = await parseArguments();
const settings: TJS.PartialArgs = {
required: true,
titles: true
};
const compilerOptions: TJS.CompilerOptions = {
strictNullChecks: true,
};
const definitionFilePath = require.resolve(parsedArguments.interfaceFilePath);
const program = TJS.getProgramFromFiles([definitionFilePath], compilerOptions);
let schema = TJS.generateSchema(program, parsedArguments.interfaceName, settings);
if(!schema) {
throw new Error("Failed to generate schema")
}
const keyNamesNotToMap = [
'type',
'properties',
'description',
'items',
'required',
'definitions',
'$schema',
'enum'
];
// schema = await deepMapObject(schema, ({key, value}) => ({newKey: keyNamesNotToMap.includes(key) ? key : _.upperFirst(key), newValue: value}));
schema = await deepMapObject(schema, ({key, value}) => ({newKey: key, newValue: (key === 'title') ? _.upperFirst(value) : value}));
fs.writeFileSync(path.resolve(parsedArguments.outputJsonFilePath), JSON.stringify(schema, null, 2));
}
async function parseArguments(): Promise<{interfaceFilePath: string, interfaceName: string, outputJsonFilePath: string}> {
return yargs(hideBin(process.argv))
.option('interface-file-path', {
type: 'string',
description: 'File path to the interface file which will be passed to require.resolve. For example @capacitor/action-sheet/dist/esm/definitions.d.ts',
})
.option('interface-name', {
type: 'string',
description: 'Interface name',
})
.option('output-json-file-path', {
type: 'string',
description: 'Output json file path',
})
.help()
.argv;
}