-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
98 lines (62 loc) · 2.14 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
93
94
95
96
97
98
var child_process = require('child_process');
var Through2 = require('through2');
var Join = require('path').join;
var PluginError = require('plugin-error');
var PLUGIN_NAME = 'gulp-lab';
function _isObject (val) {
return Object.prototype.toString.call(val) === '[object Object]';
}
function _isString (val) {
return typeof val === 'string';
}
/**
* Merge args with Array or String args from options
*/
function _mergeArgs (args, optionsArgs) {
if (_isString(optionsArgs)) { // String options
args = args.concat(optionsArgs.split(' '));
} else if (Array.isArray(optionsArgs)) { // Array options
args = args.concat(optionsArgs);
}
return args;
}
module.exports = function (options) {
var paths = [];
var emitErr = false;
// We expect that lab was declared as a project dependencies, and run directly its main script
// this way, it can be executed on every platform.
var args = [Join(process.cwd(), 'node_modules', '@hapi', 'lab', 'bin', 'lab')];
return Through2.obj(function (file, enc, cb) {
paths.push(file.path);
this.push(file);
cb();
}, function (cb) {
var stream = this;
if (_isObject(options)) { // Object options
if (!_isObject(options.opts)) {
stream.emit('error', new PluginError(PLUGIN_NAME, 'Lab - Object property "opts" must be an object!'));
cb();
return;
}
if (options.opts && typeof options.opts.emitLabError !== 'boolean') {
stream.emit('error', new PluginError(PLUGIN_NAME, 'Lab - Object property "emitLabError" must be a boolen!'));
cb();
return;
}
// args is optional
args = _mergeArgs(args, options.args);
// emit lab error?
emitErr = options.opts.emitLabError;
} else { // String or Array options
args = _mergeArgs(args, options);
}
// Spawn process
var child = child_process.spawn(process.execPath, process.execArgv.concat(args.concat(paths)), {stdio: 'inherit'});
child.on('exit', function (code) {
if (code !== 0 && emitErr) {
stream.emit('error', new PluginError(PLUGIN_NAME, 'Lab exited with errors.'));
}
cb();
});
});
};