generated from rhdeck/ts-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin.ts
147 lines (147 loc) · 4.76 KB
/
bin.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
#!/usr/bin/env node
const start = Date.now().valueOf();
import { spawnSync } from "child_process";
import { join, normalize } from "path";
import { existsSync, readFileSync } from "fs";
import resolve from "@raydeck/dependency-tree-resolver";
import commander, { command } from "commander";
import { getServerlessConfig } from "@raydeck/serverless-base";
const { version, description } = JSON.parse(
readFileSync(join(__dirname, "..", "package.json"), { encoding: "utf8" })
);
commander
.version(version)
.description(description)
// .option("-p --package", "Package to deploy if specified")
// .option(
// "-w --working-directory",
// "Working directory for the top of the monorepo",
// "."
// )
.option(
"-f --force",
"Force deploy (slower but guarantees full deployment",
false
);
commander.parse(process.argv);
const force = commander.opts().force || false;
if (!commander.isDocumenting) {
const cwd = process.cwd();
const serverlessInfo = getServerlessConfig(cwd);
const [targetservice] = commander.args;
const { deploy, remove } = <{ deploy: string[]; remove: string[] }>(
serverlessInfo
);
if (targetservice) {
Deploy(
normalize(
targetservice.includes("/")
? targetservice
: join(cwd, "serverless", targetservice)
),
force
);
} else {
let dependencies = <{ fp: string; deps: string[] }[]>deploy
.map((p) => join(cwd, p))
.filter((name) => doesPackageJSONExist(name))
.map((fp) => {
const { dependencies } = <
{ dependencies: { [key: string]: string } | undefined }
>getServerlessConfig(fp);
if (!dependencies) return undefined;
const deps = Object.values(dependencies).map((v) => join(fp, v));
return { fp, deps };
})
.filter(Boolean);
let count, newCount;
do {
count = Object.keys(dependencies).length;
dependencies = dependencies.reduce(
(acc, { deps }) =>
deps.reduce((acc, fp) => {
if (acc.find(({ fp: a }) => a == fp)) return acc;
else {
const { dependencies } = <
{ dependencies: { [key: string]: string } | undefined }
>getServerlessConfig(fp);
if (dependencies) {
const deps = Object.values(dependencies).map((v) =>
join(fp, v)
);
return [...acc, { fp, deps }];
} else return [...acc, { fp, deps: [] }];
}
}, acc),
dependencies
);
newCount = Object.keys(dependencies).length;
} while (count != newCount);
const depObj = dependencies.reduce((acc, { fp, deps }) => {
acc[fp] = deps;
return acc;
}, <{ [key: string]: any }>{});
resolve(depObj)
.filter((fp) => ![cwd, cwd + "/"].includes(fp))
.forEach((path) => Deploy(path, force));
}
if (remove) Object.values(remove).map((fp) => Remove(join(cwd, fp)));
const end = Date.now().valueOf();
const secs = (end - start) / 1000;
console.log(`Done with all in ${secs}\n`);
}
export { commander };
function doesPackageJSONExist(fp: string) {
const jsonPath = join(fp, "package.json");
return existsSync(jsonPath);
}
function Deploy(dir: string, force: boolean = false) {
try {
if (!existsSync(dir)) return;
process.chdir(dir);
console.log("directory is now ", process.cwd());
console.log(`Deploying ${dir}`);
spawnSync("yarn", [], { stdio: "inherit" });
spawnSync("npx", ["fix-local-dependencies"], { stdio: "inherit" });
//check local package.json for deployment
const p = <{ scripts: { deploy: string | undefined } | undefined }>(
JSON.parse(readFileSync(join(dir, "package.json"), { encoding: "utf-8" }))
);
if (p?.scripts?.deploy) {
const { status, signal } = spawnSync(
"yarn",
["deploy", ...(force ? ["--force"] : [])],
{
stdio: "inherit",
}
);
if (status) {
//that's not good
throw new Error(
`Could not deploy ${dir} - deploy step exited with code ${status}`
);
}
if (signal) {
//that's also not good
throw new Error(
`Could not deploy ${dir} - deploy step terminated with signal ${signal}`
);
}
console.log(`Finished deploying serverless pacakge ${dir}`);
} else {
console.log(`no deploy step for package at ${dir}`);
}
} catch (e) {
throw new Error(`Could not deploy ${dir}: ${e.message}`);
}
}
function Remove(dir: string) {
if (!existsSync(dir)) return;
process.chdir(dir);
console.log("cwd is now", process.cwd());
console.log(`Removing ${dir}`);
spawnSync("yarn", ["run", "serverless-stage", "remove"], {
stdio: "inherit",
});
console.log(`Finished removing ${dir}`);
}