/
index.ts
154 lines (130 loc) · 4.38 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import * as path from "path";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import * as esbuild from "esbuild";
import execa from "execa";
import { config, environments, Environment } from "@config/index";
import * as process from "process";
import fs from "fs";
import * as fse from "fs-extra";
const baseDir = "../";
const paths = {
workingDir: path.resolve(__dirname, baseDir),
src: path.resolve(__dirname, baseDir, "src"),
srcBackend: path.resolve(__dirname, baseDir, "src", "backend"),
srcFrontend: path.resolve(__dirname, baseDir, "src", "frontend"),
dist: path.resolve(__dirname, baseDir, "dist"),
distBackend: path.resolve(__dirname, baseDir, "dist", "backend"),
distFrontend: path.resolve(__dirname, baseDir, "dist", "frontend"),
};
async function runCommand(
command: string,
args: string[] | string,
options: execa.Options<string> = {},
echoCommand: boolean = true,
exitProcessOnError: boolean = true
) {
if (!Array.isArray(args)) args = args.split(" ");
if (echoCommand) console.log("> Running:", command, args.join(" "));
const resp = await execa(command, args, {
...options,
preferLocal: true,
reject: false,
});
if (resp.exitCode !== 0) {
if (exitProcessOnError) {
console.error(resp.stderr || resp.stdout);
process.exit(1);
} else throw new Error(resp.stderr || resp.stdout);
}
console.log(resp.stdout);
}
const commands = ["validate", "build-src", "cdk"] as const;
export type Command = (typeof commands)[number];
const argv = yargs(hideBin(process.argv))
.option("command", {
alias: "c",
describe: "the command you want to run",
choices: commands,
})
// eslint-disable-next-line @typescript-eslint/no-explicit-any
.demandOption(["c"]).argv as any;
(async () => {
const command = argv.c as Command;
switch (command) {
case "validate":
await validate();
break;
case "build-src":
await buildTsLambdas();
await buildFrontend();
break;
case "cdk":
const cdkArgs = yargs(hideBin(process.argv))
.option("operation", {
alias: "o",
describe: "the cdk operation you want to run",
choices: ["diff", "deploy", "hotswap"],
})
.option("environment", {
alias: "e",
describe: "the environment you want to run the command in",
choices: environments,
})
// eslint-disable-next-line @typescript-eslint/no-explicit-any
.demandOption(["o", "e"]).argv as any;
const environment = cdkArgs.e as keyof typeof config;
const cdkOperation = cdkArgs.o as "diff" | "deploy" | "hotswap";
await cdkCommand(cdkOperation, environment);
break;
default:
throw new Error("Unknown command: " + command);
}
})();
async function validate() {
await runCommand("tsc", ["--noEmit"], { cwd: paths.workingDir });
await runCommand("eslint", ["**/*.ts", "--ignore-pattern", "'**/*.d.ts'", "--fix"], { cwd: paths.workingDir });
}
async function buildTsLambdas() {
console.log("BUILDING TS LAMBDAS");
const tsLambdaDirectories = ["lambda/api"];
for (const lambdaDir of tsLambdaDirectories) {
const fullLambdaDir = path.join(paths.srcBackend, lambdaDir);
const pathTs = path.join(fullLambdaDir, "index.ts");
const pathJs = path.join(paths.distBackend, lambdaDir, "index.js");
await esbuild.build({
platform: "node",
target: ["esnext"],
minify: true,
bundle: true,
keepNames: true,
sourcemap: "linked",
sourcesContent: false,
entryPoints: [pathTs],
outfile: pathJs,
external: [""],
logLevel: "warning",
metafile: true,
});
}
console.log("LAMBDAS TS BUILD");
}
async function buildFrontend() {
console.log("BUILDING FRONTEND");
await fse.copy(paths.srcFrontend, paths.distFrontend);
console.log("BUILDING FRONTEND");
}
async function cdkCommand(command: "diff" | "deploy" | "hotswap", environment: Environment) {
let extraArgs = "--context env=" + environment;
if (command === "deploy" || command === "hotswap") extraArgs += " --require-approval never";
if (command === "hotswap") {
command = "deploy";
extraArgs += " --hotswap";
}
if (!process.env.CI) extraArgs += ` --profile ${config[environment].aws.profile}`;
await runCommand("cdk", `${command} "**" ${extraArgs}`, {
cwd: paths.workingDir,
stdout: "inherit",
stderr: "inherit",
});
}