forked from mllrsohn/gulp-protractor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
125 lines (109 loc) · 3.04 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
'use strict';
var es = require('event-stream');
var path = require('path');
var childProcess = require('child_process');
var PluginError = require('plugin-error');
var winExt = /^win/.test(process.platform) ? '.cmd' : '';
// optimization: cache for protractor binaries directory
var protractorDir = null;
function getProtractorCli() {
var result = require.resolve('protractor');
if (result) {
return result.replace('index', 'cli');
} else {
throw new Error('Please check whether protractor is installed or not.');
}
}
function getProtractorDir() {
if (protractorDir) {
return protractorDir;
}
var result = require.resolve('protractor');
if (result) {
// console.log(result);
// result is now something like
// c:\\Source\\gulp-protractor\\node_modules\\protractor\\built\\index.js
protractorDir = path.resolve(path.join(path.dirname(result), '..', '..', '.bin'));
return protractorDir;
}
throw new Error('No protractor installation found.');
}
var protractor = function(options) {
var files = [],
child, args;
options = options || {};
args = options.args || [];
return es.through(function(file) {
files.push(file.path);
this.push(file);
}, function() {
var stream = this;
// Enable debug mode
if (options.debug) {
args.push('debug');
}
// Attach Files, if any
if (files.length) {
args.push('--specs');
args.push(files.join(','));
}
// Pass in the config file
if (options.configFile) {
args.unshift(options.configFile);
}
// console.log(getProtractorCli());
// child = childProcess.spawn(path.resolve(getProtractorDir() + '/protractor'), args, {
child = childProcess.fork(getProtractorCli(), args, {
stdio: 'inherit',
env: process.env
}).on('exit', function(code) {
if (child) {
child.kill();
}
if (stream) {
if (code) {
stream.emit('error', new PluginError('gulp-protractor', 'protractor exited with code ' + code));
} else {
stream.emit('end');
}
}
});
});
};
var wdUpdate = function(opts, cb) {
var callback = (cb ? cb : opts);
var options = (cb ? opts : null);
var args = ['update', '--standalone'];
if (options) {
if (options.webdriverManagerArgs) {
options.webdriverManagerArgs.forEach(function(element) {
args.push(element);
});
}
if (options.browsers) {
options.browsers.forEach(function(element) {
args.push('--' + element);
});
}
}
childProcess.spawn(path.resolve(getProtractorDir() + '/webdriver-manager' + winExt), args, {
stdio: 'inherit'
}).once('close', callback);
};
var webdriverUpdateSpecific = function(opts) {
return wdUpdate.bind(this, opts);
};
wdUpdate.bind(null, ['ie', 'chrome']);
var webdriverStandalone = function(cb) {
childProcess.spawn(path.resolve(getProtractorDir() + '/webdriver-manager' + winExt), ['start'], {
stdio: 'inherit'
}).once('close', cb);
};
module.exports = {
getProtractorDir: getProtractorDir,
getProtractorCli: getProtractorCli,
protractor: protractor,
webdriver_standalone: webdriverStandalone,
webdriver_update: wdUpdate,
webdriver_update_specific: webdriverUpdateSpecific
};