-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathcfn2ts.ts
executable file
·54 lines (43 loc) · 1.79 KB
/
cfn2ts.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
#!/usr/bin/env node
import * as fs from 'fs-extra';
import * as yargs from 'yargs';
import generate from '../lib';
/* eslint-disable no-console */
/* eslint-disable max-len */
async function main() {
const argv = yargs.usage('Usage: cfn2ts')
.option('scope', { type: 'string', array: true, desc: 'Scope to generate TypeScript for (e.g: AWS::IAM)' })
.option('out', { type: 'string', desc: 'Path to the directory where the TypeScript files should be written', default: 'lib' })
.option('core-import', { type: 'string', desc: 'The typescript import to use for the CDK core module. Can also be defined in package.json under "cdk-build.cfn2ts-core-import"', default: '@aws-cdk/core' })
.epilog('if --scope is not defined, cfn2ts will try to obtain the scope from the local package.json under the "cdk-build.cloudformation" key.')
.argv;
const pkg = await tryReadPackageJson();
if (!argv.scope) {
argv.scope = await tryAutoDetectScope(pkg);
}
// read "cfn2ts-core-import" from package.json
const coreImport = pkg?.['cdk-build']?.['cfn2ts-core-import'];
if (coreImport) {
argv['core-import'] = coreImport;
}
if (!argv.scope) {
throw new Error('--scope is not provided and cannot be auto-detected from package.json (under "cdk-build.cloudformation")');
}
await generate(argv.scope, argv.out, {
coreImport: argv['core-import'],
});
}
main().catch(err => {
console.error(err);
process.exit(1);
});
async function tryAutoDetectScope(pkg: any): Promise<undefined | string[]> {
const value = pkg['cdk-build'] && pkg['cdk-build'].cloudformation;
return value && (typeof value === 'string' ? [value] : value);
}
async function tryReadPackageJson() {
if (!await fs.pathExists('./package.json')) {
return undefined;
}
return fs.readJSON('./package.json');
}