This repository has been archived by the owner on Nov 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
92 lines (75 loc) · 1.76 KB
/
index.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
'use strict';
const spawn = require('child_process').spawn;
const EventEmitter = require('events');
function callAsync(fn) {
return new Promise((resolve, reject) => {
const callback = (err, value) => {
if (err != null) {
reject(err);
} else {
resolve(value);
}
};
let fnResult;
try {
fnResult = fn(callback);
} catch (err) {
reject(err);
}
if (
fnResult &&
typeof fnResult.then === 'function' &&
typeof fnResult.catch === 'function'
) {
fnResult.then(resolve).catch(reject);
}
});
}
function run(config) {
const emitter = new EventEmitter();
config = config || {};
config.exec = config.exec || "'node'";
config.files = config.files || [];
config.numProcesses = config.numProcesses || 1;
let beforePromise = Promise.resolve();
if (config.before) {
beforePromise = callAsync(config.before);
}
beforePromise
.then(() => {
let child = spawn(
'prove',
[
'--exec',
config.exec,
'--jobs',
config.numProcesses
].concat(config.files)
);
const promise = new Promise((resolve, reject) => {
child.on('error', reject);
child.on('exit', (code, signal) => {
if (signal) {
reject(signal);
}
resolve(code);
});
});
emitter.emit('start', child);
return promise;
})
.then(code => {
let afterPromise = Promise.resolve();
if (config.after) {
afterPromise = callAsync(config.after);
}
return afterPromise.then(() => {
emitter.emit('end', code);
});
})
.catch(err => emitter.emit('error', err));
return emitter;
}
module.exports = {
run: run
};