-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest-cli-runner.js
74 lines (61 loc) · 1.87 KB
/
test-cli-runner.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
'use strict'
const spawn = require('child_process').spawn
const platform = require('os').platform()
const isUnix = platform.includes('win') ? false : true
class Runner {
start(binary, args, cwd, env) {
return new Promise((resolve, reject) => {
let hasError = false;
this._process = spawn(binary, args, { windowsHide: true, env, cwd });
this._process.stdout.on('data', (data) => {
console.log('data', data.toString());
});
this._process.on('exit', () => {
if (hasError) {
reject();
} else {
resolve();
}
});
this._process.stderr.on('data', (data) => {
console.log('err', data.toString())
hasError = true;
});
process.on('SIGHUP', () => {
this.stop();
});
})
}
_waitForOutputAndCallBack(stream, message, callback) {
stream.on('data', function fn(data) {
if (data.indexOf(message) >= 0) {
stream.removeListener('data', fn)
callback()
}
})
}
stop() {
if (isUnix) {
return this._stopUnix()
} else {
return this._stopWin()
}
}
_stopUnix() {
return new Promise((resolve) => {
if (!this._process.killed) {
const kill = spawn('kill', [this._process.pid]);
kill.on('exit', resolve)
}
})
}
_stopWin() {
return new Promise((resolve, reject) => {
if (!this._process.killed) {
const kill = spawn("taskkill", ["/pid", this._process.pid, '/f', '/t']);
kill.on('exit', resolve)
}
})
}
}
module.exports = Runner