-
Notifications
You must be signed in to change notification settings - Fork 12
/
start-helper.js
88 lines (78 loc) · 2.03 KB
/
start-helper.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
const { spawnSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const ngrok = require('ngrok');
const figlet = require('figlet');
const dotenv = require('dotenv');
const chalk = require('chalk');
const kill = require('tree-kill');
const _ = require('lodash');
dotenv.config();
/**
* This script provide a cross-platform way for starting npm process in child directory.
*
* Usage: node start-helper.js [api|client|test|install-children]
*
*/
const options = [
{
name: 'api',
command: 'npm',
arguments: ['start'],
workingDirs: 'api',
message: 'Starting API...',
},
{
name: 'client',
command: 'npm',
arguments: ['start'],
workingDirs: 'client',
message: 'Starting web client...',
},
{
name: 'test',
command: 'npm',
arguments: ['run', 'test'],
workingDirs: ['api'],
message: 'Running tests for',
},
{
name: 'install-children',
command: 'npm',
arguments: ['install'],
workingDirs: ['api', 'client'],
message: 'Installing dependencies for',
},
];
const start = () => {
if (process.argv.length !== 3) {
throw new Error(`[-] Invalid command. Please specify the argument`);
}
let command = _.find(options, (opt) => {
if (opt.name === process.argv[2]) {
return opt;
}
});
if (!command) {
throw new Error(`[-] Invalid argument "${process.argv[2]}"`);
}
process.exit(run(command));
};
const run = (command) => {
const cmd = command.command;
if (_.isArray(command.workingDirs)) {
status = 0;
command.workingDirs.forEach((dir) => {
const opts = { cwd: dir, shell: true, stdio: 'inherit' };
console.log(chalk.cyan(`\n[*] ${command.message} ${dir}...\n`));
const child = spawnSync(cmd, command.arguments, opts);
status += child.status;
});
return status;
} else {
const opts = { cwd: command.workingDirs, shell: true, stdio: 'inherit' };
console.log(chalk.cyan(`[*] ${command.message}`));
return spawnSync(cmd, command.arguments, opts);
}
};
start();