-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
101 lines (89 loc) · 2.52 KB
/
index.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env node
import path from 'path';
import arg from 'arg';
import makeDir from 'make-dir';
import checkForUpdate from 'update-check';
import packageJson from './package.json';
import { createApp } from './lib/create-app';
import { promptForTemplate } from './lib/templates';
import { executeCommand } from './lib/utils';
import { chooseLanguage } from './lib/templates';
const args = arg({
'--help': Boolean,
'--version': Boolean,
'--debug': Boolean,
'--template': String,
'-t': '--template',
'-v': '--version',
'-h': '--help',
'-d': '--debug'
});
if (args['--version']) {
console.log(`create-cdk-app v${packageJson.version}`);
process.exit(0);
}
if (args['--help']) {
console.log(`
Usage
$ create-cdk-app <project-directory>
Options
--template, -t Template [name]|[url]
--version, -v Version number
--help, -h Displays this message
--debug, -d Enable verbose logging
`);
process.exit(0);
}
const debug = args['--debug'] ? args['--debug'] : false;
async function run() {
// handle project directory
if (args._.length === 0) {
console.error(`No project directory was specified`);
process.exit(1);
}
const projectPath = path.resolve(args._[0]);
// handle template name
const template = args['--template'] ? args['--template'] : await promptForTemplate();
if (template === 'cdk-init') {
try {
await makeDir(projectPath);
process.chdir(projectPath); // cdk init must be run within the new directory
const language = await chooseLanguage();
console.log(`Initializing new cdk ${language} app with the cdk cli`);
console.log('This might take a minute or two.')
const res = await executeCommand(`cdk init app --language=${language}`);
console.log(res);
process.exit(0);
} catch (e) {
console.error(e.toString());
process.exit(1);
}
}
await createApp({
template,
projectPath,
debug
});
}
const update = checkForUpdate(packageJson).catch(() => null)
async function notifyUpdate(): Promise<void> {
try {
const res = await update
if (res?.latest) {
console.log('A new version of `create-cdk-app` is available!')
console.log('You can update by running: yarn global add create-cdk-app');
}
process.exit();
} catch {
// ignore error
}
}
run()
.then(notifyUpdate)
.catch(async (reason) => {
console.log('Aborting installation.');
console.log('Unexpected error');
console.log(reason);
await notifyUpdate();
process.exit(1);
})