This repository was archived by the owner on Apr 18, 2024. It is now read-only.
forked from anomalyco/sst
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.js
More file actions
58 lines (48 loc) · 1.74 KB
/
Copy pathdeploy.js
File metadata and controls
58 lines (48 loc) · 1.74 KB
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
"use strict";
const path = require("path");
const fs = require("fs-extra");
const paths = require("./util/paths");
const { synth, deploy } = require("./util/cdkHelpers");
const { STACK_DEPLOY_STATUS } = require("@serverless-stack/core");
module.exports = async function (argv, config, cliInfo) {
// Normalize stack name
const stackPrefix = `${config.stage}-${config.name}-`;
let stackName = argv.stack;
if (stackName) {
stackName = stackName.startsWith(stackPrefix)
? stackName
: `${stackPrefix}${stackName}`;
}
// Run CDK Synth
await synth(cliInfo.cdkOptions);
// Run CDK Deploy
const stacksData = await deploy(cliInfo.cdkOptions, stackName);
// This is native CDK option. According to CDK documentation:
// If an outputs file has been specified, create the file path and write stack outputs to it once.
// Outputs are written after all stacks have been deployed. If a stack deployment fails,
// all of the outputs from successfully deployed stacks before the failure will still be written.
if (argv.outputsFile) {
await writeOutputsFile(
stacksData,
path.join(paths.appPath, argv.outputsFile)
);
}
// Check all stacks deployed successfully
if (stacksData.some(({ status }) => status === STACK_DEPLOY_STATUS.FAILED)) {
throw new Error(`Failed to deploy the app`);
}
return stacksData;
};
async function writeOutputsFile(stacksData, outputsFileWithPath) {
const stackOutputs = stacksData.reduce((acc, { name, outputs }) => {
if (Object.keys(outputs || {}).length > 0) {
return { ...acc, [name]: outputs };
}
return acc;
}, {});
fs.ensureFileSync(outputsFileWithPath);
await fs.writeJson(outputsFileWithPath, stackOutputs, {
spaces: 2,
encoding: "utf8",
});
}