This repository has been archived by the owner on Apr 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
test.js
69 lines (61 loc) · 1.8 KB
/
test.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
const {spawn} = require('child_process');
const commands = [
{cmd: 'rm', args: ['-r', 'test-cmp'], ignoreErrors: true},
{cmd: './node_modules/.bin/vue', args: ['init', '.', 'test-cmp'], yes: true},
{cmd: 'npm', args: ['install'], cwd: 'test-cmp'},
{cmd: 'npm', args: ['run', 'lint'], cwd: 'test-cmp'},
{cmd: 'npm', args: ['run', 'build'], cwd: 'test-cmp'},
{cmd: 'npm', args: ['run', 'build:doc'], cwd: 'test-cmp'},
{cmd: 'npm', args: ['run', 'test'], cwd: 'test-cmp'},
{cmd: 'npm', args: ['run', 'test:cov'], cwd: 'test-cmp'}
];
function executeCommand(command, index) {
return new Promise((resolve, reject) => {
let cp = spawn(command.cmd, command.args, {cwd: command.cwd});
process.on('exit', cp.kill);
cp.stdout.setEncoding('utf-8');
cp.stdin.setEncoding('utf-8');
// Ignore pipe errors
cp.stdin.on('error', () => {});
cp.stdout.on('error',() => {});
// cp.stdout.pipe(process.stdout);
cp.stderr.pipe(process.stderr);
let rejected = false;
if(command.yes) {
cp.stdout.on('data', function() {
if (!rejected) cp.stdin.write("\n");
});
}
cp.once('error', code => {
if (!rejected) {
reject(code);
rejected = true;
}
});
cp.once('exit', code => {
if (code) {
reject(code);
rejected = true;
} else {
console.log(
'=> process',
(index+1),
'of',
commands.length,
'exit with status',
code
);
resolve(code);
}
});
});
}
commands.reduce((prev, next, index) => {
return prev.then(() => {
return executeCommand(next, index)
.catch(code => {
console.log('child process exit with', code);
if (!next.ignoreErrors) process.exit(code);
});
});
}, Promise.resolve());