-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
85 lines (67 loc) · 1.69 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
'use strict';
var through = require('through');
var log = require('fancy-log');
var helper = require('./lib/helper');
var path = require('path');
var spawn = require('child_process').spawn;
var PluginError = require('plugin-error');
var PLUGIN_NAME = 'gulp-nightwatch';
var nightwatchPlugin = function(options) {
var child,
stream,
files = [],
nightwatchOptions = { config: 'nightwatch.json', env: 'default' };
options = options || {};
if (options.configFile) {
if (typeof options.configFile === 'string') {
nightwatchOptions.config = path.resolve(options.configFile);
} else {
throw new PluginError(PLUGIN_NAME, 'configFile option must be string');
}
}
if (options.cliArgs) {
helper.merge(nightwatchOptions, helper.parseCliArgs(options.cliArgs));
}
function done(code) {
if (child) {
child.kill();
}
if (stream) {
if (code) {
stream.emit('error', new PluginError(PLUGIN_NAME, 'nightwatch exited with code ' + code));
} else {
stream.emit('end');
}
}
}
function startNightwatch() {
log('Starting nightwatch...');
child = spawn(
'node',
[
path.join(__dirname, 'lib', 'background.js'),
JSON.stringify(nightwatchOptions) // Nightwatch args
],
{
stdio: 'inherit'
}
);
child.on('exit', function(code) {
done(code);
});
}
function queueFile(file) {
if (file) {
files.push(file.path);
}
}
function endStream() {
if (files.length) {
options.files = files;
}
startNightwatch();
}
stream = through(queueFile, endStream);
return stream;
};
module.exports = nightwatchPlugin;