-
Notifications
You must be signed in to change notification settings - Fork 1
/
cdk.launcher.ts
executable file
·61 lines (47 loc) · 1.94 KB
/
cdk.launcher.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
#!/usr/bin/env -S npx tsx
import { Config } from './Config'
import { program } from 'commander'
import { basename, dirname } from 'path'
import col from 'ansi-colors'
import { spawnSync } from 'child_process'
/** useful functions */
const __dirname = dirname(new URL(import.meta.url).pathname)
const __filename = basename(new URL(import.meta.url).pathname)
const logHeading = (msg: string) => console.info(col.bgYellow.black(msg))
/** use commander to handle script inputs */
program
.name(__filename)
.description('wrapper for cdk that loads config. cdk options can be included after a solo `--`')
.option('-c, --config <config-name>', '(required) config name. e.g. dev for `config.dev.ts`')
.option('-h, --help', 'get help')
// .option('-s, --stack <stack>', 'single stack name, if omitted, all stacks will be operated on')
.argument('[cdk-comand]', 'the cdk command to run, e.g. synth, bootstrap, ls, deploy, destroy, etc.')
.argument('[stacks...]', 'the stacks to run the command on')
program.parse(process.argv)
console.debug(program.args)
const options = program.opts()
if(options.help || !options.config){
program.help()
}
logHeading('import config...')
const config: Config = (await import(`${__dirname}/config.${options.config as string}`)).config
console.info(config)
logHeading(`exec cdk commands on stacks... (${config.region})`)
let cdkCommand = `AWS_REGION=${config.region} npx cdk -a 'npx tsx app.ts ${options.config}' ${program.args.join(' ')}`
if(program.args.length > 0){
if(program.args[0] === 'deploy'){
cdkCommand += ' --require-approval never '
// --hotswap-fallback
}else if(program.args[0] === 'destroy'){
cdkCommand += ' --force '
}
if(['synth', 'deploy', 'diff'].includes(program.args[0])){
cdkCommand += ` --output='./cdk.out.${config.region}' --change-set-name 'change-name-${config.region}' `
}
}
console.debug(`executing: ${cdkCommand}`)
spawnSync(cdkCommand, {
// encoding: 'utf8',
stdio: 'inherit',
shell: true,
})