/
test-bin.js
67 lines (52 loc) · 1.44 KB
/
test-bin.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
'use strict';
const path = require('path');
const execa = require('execa');
const webpackDevServerPath = path.resolve(
__dirname,
'../../bin/webpack-dev-server.js'
);
const basicConfigPath = path.resolve(
__dirname,
'../fixtures/cli/webpack.config.js'
);
function testBin(testArgs, configPath) {
const cwd = process.cwd();
const env = process.env.NODE_ENV;
let stdout = '';
let stderr = '';
if (!configPath) {
configPath = basicConfigPath;
}
if (!testArgs) {
testArgs = [];
} else if (typeof testArgs === 'string') {
testArgs = testArgs.split(' ');
}
const args = [webpackDevServerPath, '--config', configPath].concat(testArgs);
return new Promise((resolve, reject) => {
const child = execa('node', args, { cwd, env });
child.on('error', (error) => reject(error));
child.stdout.on('data', (data) => {
const str = data.toString();
stdout += str;
// if webpack.config.js is a promise function, it won't be able to call `close`
if (str.includes('Compiled successfully.')) {
child.kill();
}
});
child.stderr.on('data', (data) => {
stderr += data.toString();
});
child.on('end', () => {
console.log('end');
});
// cannot catch when function is promise
child.on('close', (code) => {
if (code !== 0) {
return reject(stderr);
}
resolve({ stdout, stderr, code });
});
});
}
module.exports = testBin;