-
Notifications
You must be signed in to change notification settings - Fork 1
/
tools.js
441 lines (436 loc) · 10.4 KB
/
tools.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
const childProcess = require('child_process');
const fse = require('fs-extra');
const fs = require('fs');
const util = require('util');
const path = require('path');
/**
* @typedef {{
* title: string
* task: (function(): Promise<void>)
* }} Task
*/
/**
* @param {string} cmd
* @param {string[]} args
* @param {import('child_process').SpawnOptions?} options
*/
async function spawn(cmd, args, options) {
return new Promise((resolve, reject) => {
const proc = childProcess.spawn(
cmd,
args,
options
? options
: {
stdio: 'inherit',
},
);
proc.on('close', (code) => {
if (code !== 0) {
reject(code);
} else {
resolve();
}
});
});
}
/**
* @param {Task[]} tasks
*/
function createTasks(tasks) {
return {
run: async () => {
for (let i = 0; i < tasks.length; i = i + 1) {
const t = tasks[i];
console.log(`${i + 1}. ${t.title}`);
try {
await t.task();
console.log(`✓`);
} catch (error) {
console.log(`⨉`);
throw error;
}
}
},
};
}
/**
*
* @param {string[]} rawArgs
* @returns {Args}
*/
function parseArgs(rawArgs) {
/**
* @type {{
* [key: string]: string,
* }}
*/
const args = {};
let i = 2;
while (i < rawArgs.length) {
const arg = rawArgs[i];
let value = '';
if (rawArgs[i + 1]) {
value = rawArgs[i + 1].startsWith('--') ? '' : rawArgs[i + 1];
}
args[arg] = value;
if (value === '') {
i = i + 1;
} else {
i = i + 2;
}
}
/**
*
* @param {string} name
* @param {'string' | 'boolean'} type
* @returns {string | boolean}
*/
function getArg(
name,
type,
) {
if (type === 'string') {
return args[name];
} else {
return (args[name] === '' || args[name] === 'true' || false);
}
}
return {
bundle: getArg('--bundle', 'boolean' ),
local: getArg('--local', 'boolean' ),
link: getArg('--link', 'boolean' ),
unlink: getArg('--unlink', 'boolean' ),
publish: getArg('--publish', 'boolean' ),
build: getArg('--build', 'boolean' ),
sudo: getArg('--sudo', 'boolean' ),
pack: getArg('--pack', 'boolean' ),
localDevBundle: getArg('--local-dev-bundle', 'boolean' ),
localDevPack: getArg('--local-dev-pack', 'boolean' ),
createImage: getArg('--create-image', 'boolean' ),
};
}
async function bundle() {
const tasks = createTasks([
{
title: 'Remove dist directory.',
task: async () => {
await fse.remove(path.join(__dirname, 'dist'));
},
},
{
title: 'Compile Typescript.',
task: async () => {
await build();
},
},
{
title: 'Fix imports',
async task() {
await fixImports()
}
},
{
title: 'Copy package.json.',
task: async () => {
const data = JSON.parse(
(
await util.promisify(fs.readFile)(
path.join(__dirname, 'package.json'),
)
).toString(),
);
data.devDependencies = undefined;
data.nodemonConfig = undefined;
data.scripts = {
start: 'node src/main.js',
};
await util.promisify(fs.writeFile)(
path.join(__dirname, 'dist', 'package.json'),
JSON.stringify(data, null, ' '),
);
},
},
{
title: 'Copy LICENSE',
task: async () => {
await util.promisify(fs.copyFile)(
path.join(__dirname, 'LICENSE'),
path.join(__dirname, 'dist', 'LICENSE'),
);
},
},
]);
await tasks.run();
}
async function localDevBundle() {
const tasks = createTasks([
{
title: 'Remove dist directory.',
task: async () => {
await fse.remove(path.join(__dirname, 'local-dev-dist'));
},
},
{
title: 'Copy src',
task: async () => {
await fse.copy(
path.join(__dirname, 'src'),
path.join(__dirname, 'local-dev-dist', 'src'),
);
},
},
{
title: 'Copy assets',
task: async () => {
const files = ['tsconfig.json', '.eslintrc', '.eslintignore'];
for (let i = 0; i < files.length; i++) {
const file = files[i];
await fse.copy(
path.join(__dirname, file),
path.join(__dirname, 'local-dev-dist', file),
);
}
},
},
{
// TODO: Remove this set when prod is ready
title: 'Copy packs - DEV ONLY',
task: async () => {
await fse.copy(
path.join(__dirname, 'packs'),
path.join(__dirname, 'local-dev-dist', 'packs'),
);
},
},
{
title: 'Copy package.json.',
task: async () => {
const data = JSON.parse(
(
await util.promisify(fs.readFile)(
path.join(__dirname, 'package.json'),
)
).toString(),
);
await util.promisify(fs.writeFile)(
path.join(__dirname, 'local-dev-dist', 'package.json'),
JSON.stringify(data, null, ' '),
);
},
},
{
title: 'Copy LICENSE',
task: async () => {
await util.promisify(fs.copyFile)(
path.join(__dirname, 'LICENSE'),
path.join(__dirname, 'local-dev-dist', 'LICENSE'),
);
},
},
{
title: 'Copy Dockerfile',
task: async () => {
await util.promisify(fs.copyFile)(
path.join(__dirname, 'Dockerfile.dev'),
path.join(__dirname, 'local-dev-dist', 'Dockerfile'),
);
},
},
]);
await tasks.run();
}
/**
* @param {boolean} sudo
* @returns {Promise<void>}
*/
async function link(sudo) {
await spawn('npm', ['i'], {
cwd: path.join(process.cwd(), 'dist'),
stdio: 'inherit',
});
if (sudo) {
await spawn('sudo', ['npm', 'link'], {
cwd: path.join(process.cwd(), 'dist'),
stdio: 'inherit',
});
} else {
await spawn('npm', ['link'], {
cwd: path.join(process.cwd(), 'dist'),
stdio: 'inherit',
});
}
}
/**
* @param {boolean} sudo
* @returns {Promise<void>}
*/
async function unlink(sudo) {
if (sudo) {
await spawn('sudo', ['npm', 'link'], {
cwd: path.join(process.cwd(), 'dist'),
stdio: 'inherit',
});
} else {
await spawn('npm', ['unlink'], {
cwd: path.join(process.cwd(), 'dist'),
stdio: 'inherit',
});
}
}
async function publish() {
if (
await util.promisify(fs.exists)(
path.join(__dirname, 'dist', 'node_modules'),
)
) {
throw new Error(
`Please remove "${path.join(__dirname, 'dist', 'node_modules')}"`,
);
}
await spawn('npm', ['publish', '--access=private'], {
cwd: path.join(process.cwd(), 'dist'),
stdio: 'inherit',
});
}
async function build() {
await spawn('npm', ['run', 'build:ts']);
await fse.copy(
path.join(__dirname, 'src', 'response-code', 'codes'),
path.join(__dirname, 'dist', 'src', 'response-code', 'codes'),
);
}
async function pack() {
await spawn('npm', ['pack'], {
cwd: path.join(process.cwd(), 'dist'),
stdio: 'inherit',
});
}
async function localDevPack() {
await spawn('npm', ['pack'], {
cwd: path.join(process.cwd(), 'local-dev-dist'),
stdio: 'inherit',
});
}
/**
* @typedef {{
* rel: string;
* abs: string;
* }} FileTreeItem
*/
/**
*
* @param {string} startingLocation
* @param {string} location
* @returns {Promise<FileTreeItem[]>}
*/
async function fileTree(
startingLocation,
location,
) {
/**
* @type FileTreeItem[]
*/
const output = [];
const basePath = path.join(startingLocation, location);
const files = await util.promisify(fs.readdir)(basePath);
for (let i = 0; i < files.length; i++) {
const file = files[i];
const filePath = path.join(basePath, file);
const stat = await util.promisify(fs.lstat)(filePath);
if (stat.isDirectory()) {
const children = await fileTree(
startingLocation,
path.join(location, file),
);
for (let j = 0; j < children.length; j++) {
const child = children[j];
output.push(child);
}
} else {
output.push({
abs: filePath,
rel: location,
});
}
}
return output;
}
async function fixImports() {
const filePaths = await fileTree(path.join(process.cwd(), 'dist'), '');
for (let i = 0; i < filePaths.length; i++) {
const filePath = filePaths[i];
if(filePath.abs.endsWith('.js')) {
let replacer = './';
if (filePath.rel !== '') {
const depth = filePath.rel.split('/').length;
replacer = new Array(depth - 1).fill('..').join('/');
}
const file = (await util.promisify(fs.readFile)(filePath.abs)).toString();
const fileFixed = file.replace(/@bcms/g, replacer).replace(/@becomes\/cms-backend/g, replacer);
if (file !== fileFixed) {
await util.promisify(fs.writeFile)(filePath.abs, fileFixed);
}
}
}
}
async function createImage() {
const tasks = createTasks([
{
title: 'Create bundle',
task: async () => {
await bundle();
}
},
{
title: 'Create lib',
task: async () => {
await fse.copy(
path.join(process.cwd(), 'dist'),
path.join(process.cwd(), 'lib'));
await fse.copy(
path.join(process.cwd(), 'node_modules', '@becomes', 'cms-ui', 'public'),
path.join(process.cwd(), 'lib', 'public'));
}
},
{
title: 'Create docker image',
task: async () => {
await spawn('docker', ['build', '.', '-t', 'becomes/cms-backend'])
}
},
{
title: 'Remove lib',
task: async () => {
await fse.remove(path.join(process.cwd(), 'lib'))
}
}
])
await tasks.run()
}
async function main() {
const options = parseArgs(process.argv);
if (options.bundle === true) {
await bundle();
} else if (options.link === true) {
await link(options.sudo);
} else if (options.unlink === true) {
await unlink(options.sudo);
} else if (options.publish === true) {
await publish();
} else if (options.build === true) {
await build();
} else if (options.pack === true) {
await pack();
} else if (options.localDevBundle) {
await localDevBundle();
} else if (options.localDevPack) {
await localDevPack();
} else if (options.createImage) {
await createImage();
}
}
main().catch((error) => {
console.error(error);
process.exit(1);
});