-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyaml-to-ts.ts
79 lines (75 loc) · 2.62 KB
/
yaml-to-ts.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
75
76
77
78
79
#!/usr/bin/env node
import { readFile } from 'fs/promises';
import * as yaml from 'js-yaml';
import * as path from 'path';
import prettier from 'prettier';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import { build } from './generateDeclaration.js';
const parsedArgs = hideBin(process.argv);
yargs(parsedArgs)
.command(
'$0 <input-file> [--prettier-file prettier-file-URI] [--out-file out-file-URI] [--dry-run] [--interface-name name]',
'Build TS interface from YAML file',
(args) => {
return args
.positional('input-file', {
alias: 'i',
type: 'string',
description: 'YAML Input file',
})
.option('prettier-file', {
alias: 'p',
type: 'string',
description: 'Prettier configuration file',
})
.option('out-file', {
alias: 'o',
type: 'string',
description: 'Output file',
default: path.join(process.cwd(), 'declaration.d.ts'),
})
.option('dry-run', {
alias: 'd',
type: 'boolean',
description: 'Perform a dry run',
default: false,
})
.option('interface-name', {
alias: 'int',
type: 'string',
description: 'Define interface name',
default: 'Declaration',
})
.strict();
},
async (argv) => {
const fileVal = await readFile(argv.inputFile, {
encoding: 'utf-8',
});
const parsedYaml = yaml.load(fileVal);
let prettierConfig: Partial<prettier.Config> = {
printWidth: 80,
tabWidth: 4,
semi: true,
singleQuote: true,
endOfLine: 'lf',
trailingComma: 'es5',
bracketSpacing: true,
arrowParens: 'always',
parser: 'typescript',
};
if (argv.prettierFile) {
prettierConfig = await prettier.resolveConfig(
argv.prettierFile
);
}
await build(parsedYaml as object, {
interfaceName: argv.interfaceName,
outFile: argv.outFile,
prettierConfig,
dryRun: argv.dryRun,
});
}
)
.parse();