-
Notifications
You must be signed in to change notification settings - Fork 3
/
dev.js
61 lines (56 loc) · 1.72 KB
/
dev.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
const process = require('process');
const { exec } = require('child_process');
const { color, debounce, sleep, checkNodeVersion } = require('./util');
main();
async function main() {
/**
* "init-dev": "(npm i && cd web && npm i) && ((cd web && npm run dev) & node app.js)",
* "dev": "(cd web && npm run dev) & node app.js",
* "init-start": "npm i && (cross-env NODE_ENV=production node app.js)"
* "start": "cross-env NODE_ENV=production node app.js"
*/
if (!checkNodeVersion()) {
return console.log(color['bgRedTxtWhite']('ERROR') + ' Requires Node.js version 16.3+');
}
// npm run dev [--init]
let tools = {};
let taskList = [
{
id: 'client',
cmd: 'npm run dev',
options: { cwd: './web' },
colorType: 'bgGreenTxtWhite',
},
{
id: 'server',
cmd: 'node app.js',
colorType: 'bgBlueTxtWhite',
},
];
for (let i = 0; i < taskList.length; i++) {
let { id, cmd, options = {}, colorType } = taskList[i];
if (process.argv[2] === '--init') {
cmd = `npm i && ${cmd}`;
}
tools[id] = { logs: [] };
let colorPrefix = color[colorType](id);
tools[id].showLogs = debounce(conf => {
console.log(`${colorPrefix}\n${conf.logs.join('')}`.replace(/\n+/g, '\n'));
conf.logs = []; // 打印后清空记录
}, 2000);
let event = exec(cmd, options, function (err, stdout, stderr) {
if (err) {
console.log(color['bgRedTxtWhite']('ERROR') + '\n' + err);
process.exit();
}
});
event.stdout.on('data', data => {
tools[id].logs.push(data);
tools[id].showLogs(tools[id]);
});
event.stderr.on('data', data => {
tools[id].logs.push(data);
tools[id].showLogs(tools[id]);
});
}
}