forked from dendronhq/dendron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genScripts.js
executable file
·287 lines (247 loc) · 6.88 KB
/
genScripts.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* eslint-disable no-console */
/* eslint-disable no-use-before-define */
const fs = require("fs");
const _ = require("lodash");
const DEPENDENCIES = {
CLIENT_WEB: "@dendronhq/web-client",
CLIENT_ELECTRON: "@dendronhq/electron-client",
COMMON_ALL: "@dendronhq/common-all",
COMMON_CLIENT: "@dendronhq/common-client",
COMMON_SERVER: "@dendronhq/common-server",
ENGINE_SERVER: "@dendronhq/engine-server",
PLUGIN_CORE: "@dendronhq/plugin-core",
};
const frontCommonDeps = [DEPENDENCIES.COMMON_ALL];
const electronClientDeps = [
DEPENDENCIES.CLIENT_ELECTRON,
DEPENDENCIES.COMMON_CLIENT,
];
const webClientDeps = [
DEPENDENCIES.CLIENT_WEB,
DEPENDENCIES.COMMON_ALL,
DEPENDENCIES.COMMON_CLIENT,
];
const codePluginDeps = [
DEPENDENCIES.COMMON_ALL,
DEPENDENCIES.COMMON_SERVER,
DEPENDENCIES.ENGINE_SERVER,
DEPENDENCIES.PLUGIN_CORE,
];
const packages = {
"electron-client": electronClientDeps,
"web-client": webClientDeps,
"code-plugin": codePluginDeps,
};
function generateFrontCommonScripts() {
const scope = frontCommonDeps.join(" --scope ");
const group = "front-common";
generateBootstrapScript({ scope, group });
//generateBuildScript({ scope, group, standalone });
}
function generateClientScripts() {
_.each(packages, (deps, pkg) => {
const scope = deps.join(" --scope ");
const group = pkg;
generateBootstrapScript({ scope, group });
generateBuildScript({ scope, group });
generateWatchScript({ scope, group });
});
}
// --- Lib
// --- Main
async function main() {
// generateBackendScripts();
// generateScraperScripts();
// generateBackendFrontendScripts();
// generateFrontendScripts();
generateFrontCommonScripts();
generateClientScripts();
console.log("done");
}
main();
// === Rest
const backendDependencies = [
DEPENDENCIES.COMMON_TEST_UTILS,
DEPENDENCIES.SERVER_API,
DEPENDENCIES.COMMON_ALL,
DEPENDENCIES.HTTP_AWS_ES,
DEPENDENCIES.SCRAPER_COMMON,
DEPENDENCIES.CDK_CONSTANTS,
];
const backendDependenciesv2 = [
{ name: DEPENDENCIES.COMMON_ALL, phase: 0 },
{ name: DEPENDENCIES.CDK_CONSTANTS, phase: 0 },
{ name: DEPENDENCIES.HTTP_AWS_ES, phase: 0 },
{ name: DEPENDENCIES.SCRAPER_COMMON, phase: 1 },
{ name: DEPENDENCIES.COMMON_TEST_UTILS, phase: 2, dev: true },
{ name: DEPENDENCIES.SERVER_API, phase: 2 },
];
const scraperDependencies = [
DEPENDENCIES.COMMON_TEST_UTILS,
DEPENDENCIES.SCRAPERS,
DEPENDENCIES.COMMON_ALL,
DEPENDENCIES.HTTP_AWS_ES,
DEPENDENCIES.CDK_CONSTANTS,
];
const frontendDependencies = [
DEPENDENCIES.CLIENT_WEB,
DEPENDENCIES.REACT_AUTOSUGGEST,
DEPENDENCIES.HTTP_AWS_ES,
DEPENDENCIES.COMMON_ALL,
];
const frontendStandaloneDependencies = [
DEPENDENCIES.HTTP_AWS_ES,
DEPENDENCIES.COMMON_ALL,
];
function generateBootstrapScript({ scope, group, standalone }) {
const standaloneSection = `
lerna bootstrap --scope ${standalone}
`;
const script = `
#!/usr/bin/env sh
${standalone ? standaloneSection : ""}
lerna bootstrap --scope ${scope}
`;
writeScript(`./scripts/bootstrap-${group}.sh`, script);
}
function generateBuildScript({ scope, group, standalone }) {
const standaloneSection = `
lerna run build --parallel --scope ${standalone}
`;
const script = `
#!/usr/bin/env sh
${standalone ? standaloneSection : ""}
lerna run build --parallel --scope ${scope}
`;
writeScript(`./scripts/build-${group}.sh`, script);
}
function generateScript({
prefix,
suffix,
scope,
ignorePhase,
appendPast,
skipDev,
}) {
const lines = [];
suffix = suffix || "";
let lvl = 0;
if (skipDev) {
scope = _.reject(scope, { dev: true });
}
const left = [...scope];
let pushed = [];
if (ignorePhase) {
lines.push(scope);
} else {
while (!_.isEmpty(left)) {
const current = _.remove(left, { phase: lvl });
pushed = pushed.concat(current);
lvl += 1;
if (appendPast) {
lines.push(current.concat(pushed));
} else {
lines.push(current);
}
// _.remove(left, ent => current.includes(ent));
}
}
console.log(lines);
const scripts = `
#!/usr/bin/env sh
${lines
.map(
// eslint-disable-next-line prefer-template
(l) => prefix + l.map((ent) => ent.name).join(" --scope ") + ` ${suffix}`
)
.join("\n")}
`;
return scripts;
}
function generateWatchScript({ scope, group }) {
const script = `
#!/usr/bin/env sh
lerna run watch --parallel --scope ${scope}
`;
writeScript(`./scripts/watch-${group}.sh`, script);
}
function writeScript(scriptPath, script) {
console.log(script);
fs.writeFileSync(scriptPath, script, "utf8");
fs.chmodSync(scriptPath, "700");
}
// === Main
function generateScraperScripts() {
const scope = scraperDependencies.join(" --scope ");
const group = "scraper";
let script = `
#!/usr/bin/env sh
# need to use production version, otherwise file size exceeds zip limit of lambda
lerna bootstrap --scope ${scope}
`;
writeScript(`./scripts/bootstrap-${group}.sh`, script);
script = `
#!/usr/bin/env sh
lerna run build --parallel --scope ${scope}
`;
writeScript(`./scripts/build-${group}.sh`, script);
script = `
#!/usr/bin/env sh
lerna run watch --parallel --scope ${scope}`;
writeScript(`./scripts/watch-${group}.sh`, script);
}
function generateBackendFrontendScripts() {
const scope = backendDependencies
.concat(frontendDependencies)
.join(" --scope ");
const group = "front_back";
generateBootstrapScript({ scope, group });
generateBuildScript({ scope, group });
generateWatchScript({ scope, group });
}
function generateFrontendScripts() {
const scope = frontendDependencies.join(" --scope ");
const standalone = frontendStandaloneDependencies.join(" --scope ");
const group = "frontend";
generateBootstrapScript({ scope, group });
generateBuildScript({ scope, group, standalone });
}
function generateBackendScripts() {
let prefix;
const scope = backendDependencies.join(" --scope ");
// bootstrap prod
prefix = "lerna bootstrap --scope ";
const bootstrapScriptProd = generateScript({
prefix,
suffix: " -- --production",
scope: backendDependenciesv2,
ignorePhase: true,
appendPast: false,
});
writeScript("./scripts/bootstrap-backend-prod.sh", bootstrapScriptProd);
// bootstrap init
prefix = "lerna bootstrap --scope ";
const bootstrapScript = generateScript({
prefix,
scope: backendDependenciesv2,
ignorePhase: false,
appendPast: true,
});
writeScript("./scripts/bootstrap-backend.sh", bootstrapScript);
// build dev
prefix = "lerna run build --parallel --scope ";
const buildBackendScript = generateScript({
prefix,
scope: backendDependenciesv2,
ignorePhase: false,
});
writeScript("./scripts/build-backend.sh", buildBackendScript);
// watch script
prefix = "lerna run watch --parallel --scope ";
const watchBackendScript = generateScript({
prefix,
scope: backendDependenciesv2,
ignorePhase: false,
});
writeScript("./scripts/watch-backend.sh", watchBackendScript);
}