forked from aws-samples/aws-medialive-channel-orchestrator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·101 lines (87 loc) · 3.23 KB
/
cli.js
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
const { Command } = require('commander');
const toml = require('toml');
const fs = require("fs/promises");
const { CloudFormationClient, DescribeStacksCommand } = require("@aws-sdk/client-cloudformation");
const program = new Command();
const REQUIRED_KEYS = [
"Region",
"ApiEndpoint",
"CognitoUserPoolID",
"CognitoWebClientID",
]
const writeConfigFile = (src, dest, configMap) => fs
.readFile(src)
.then(data => Object.entries(configMap).reduce((acc, [k, v]) => acc.replace(`<${k}>`,v), data.toString()))
.then(data => fs.writeFile(dest, data))
;
const getOutputs = async (stackName, region=null) => {
const clientConfig = {};
if (region) clientConfig.region = region;
const client = new CloudFormationClient(clientConfig);
const command = new DescribeStacksCommand({
StackName: stackName
});
const res = await client.send(command);
const stack = res.Stacks.find(i => i.StackName === stackName);
if (!stack) throw new Error("Stack not found");
const outputMap = stack.Outputs.reduce((acc, next) => ({
...acc,
[next.OutputKey]: next.OutputValue
}), {});
if (REQUIRED_KEYS.some(i => !(i in outputMap))) throw new Error(`Stack outputs missing required keys: ${JSON.stringify(outputMap)}`);
return outputMap;
}
const getStackDetails =(configFile, env) => {
return fs.readFile(configFile)
.then(data => toml.parse(data))
.then(data => data[env].deploy.parameters)
.then(data => ({
stackName: data.stack_name,
region: data.region,
}))
}
const generateConfig = (options) => {
const {
samConfig,
samConfigEnv,
debug
} = options;
if (debug) {
console.debug(`SAM config file: ${samConfig}`)
console.debug(`SAM config env: ${samConfigEnv}`)
}
getStackDetails(samConfig, samConfigEnv)
.then(details => getOutputs(details.stackName, details.region))
.then(stackOutputs => writeConfigFile(".env.template", ".env.local", stackOutputs))
.then(() => console.log("Config generated successfully!"))
.catch((err) => console.error(`Failed to generate config file: ${err}`));
}
const echoOutput = (output) => (options) => {
const {
samConfig,
samConfigEnv,
debug
} = options;
if (debug) {
console.debug(`SAM config file: ${samConfig}`)
console.debug(`SAM config env: ${samConfigEnv}`)
}
getStackDetails(samConfig, samConfigEnv)
.then(details => getOutputs(details.stackName, details.region))
.then(stackOutputs => console.log(stackOutputs[output]));
}
const echoBucket = echoOutput("WebUIBucket");
const echoUrl = echoOutput("WebUrl");
const makeCommand = (name, desc, handler) => program
.command(name)
.option('-c, --sam-config <file>', "The SAM config toml file generated when deploying the solution", "samconfig.toml")
.option('-e, --sam-config-env <env>', "The SAM environment to use to generate the config file", "default")
.option('-d, --debug', "Whether to debug")
.description(desc)
.action(handler)
;
makeCommand("generate-config", "Generates a frontend configuration file for the AWS MediaLive Channel Orchestrator", generateConfig)
makeCommand("echo-bucket", "Prints the frontend UI bucket name", echoBucket)
makeCommand("echo-url", "Prints the deployed frontend URL", echoUrl)
program.parse(process.argv);