-
Notifications
You must be signed in to change notification settings - Fork 7
/
test.js
93 lines (82 loc) · 2.66 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const {spawn} = require('child_process');
process.setMaxListeners(20); // increase listeners limit
const initQuestions = [
{search: /^\?.+/, response: '\n'},
];
const initNoExtQuestions = [
{search: /^\? Add basic Login and Admin.+/, response: 'No\n'},
{search: /^\?.+/, response: '\n'},
];
/* eslint-disable max-len */
const commands = [
{cmd: 'rm', args: ['-r', 'test-project'], ignoreErrors: true},
{cmd: './node_modules/.bin/vue', args: ['init', '.', 'test-project'], responses: initQuestions},
{cmd: 'npm', args: ['install'], cwd: 'test-project'},
{cmd: 'npm', args: ['run', 'lint'], cwd: 'test-project'},
{cmd: 'npm', args: ['run', 'test'], cwd: 'test-project'},
{cmd: 'npm', args: ['run', 'build'], cwd: 'test-project'},
{cmd: 'rm', args: ['-r', 'test-project'], ignoreErrors: true},
{cmd: './node_modules/.bin/vue', args: ['init', '.', 'test-project'], responses: initNoExtQuestions},
{cmd: 'npm', args: ['install'], cwd: 'test-project'},
{cmd: 'npm', args: ['run', 'lint'], cwd: 'test-project'},
{cmd: 'npm', args: ['run', 'test'], cwd: 'test-project'},
{cmd: 'npm', args: ['run', 'build'], cwd: 'test-project'},
];
/* eslint-enable max-len */
function executeCommand(command, index) {
return new Promise((resolve, reject) => {
const cp = spawn(command.cmd, command.args, {cwd: command.cwd});
process.on('exit', cp.kill);
cp.stdout.setEncoding('utf-8');
cp.stdin.setEncoding('utf-8');
cp.stderr.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 (!rejected && command.responses) {
const registerResponse = (q) => {
cp.stdout.on('data', (output) => {
if (q.search.test(output)) {
// console.log('sending', q);
cp.stdin.write(q.response);
}
});
};
command.responses.forEach(registerResponse);
}
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) =>
prev.then(() =>
executeCommand(next, index).catch((code) => {
console.log('child process exit with', code);
if (!next.ignoreErrors) process.exit(code);
})),
Promise.resolve()
);