UNPKG

3.25 kBJavaScriptView Raw
1'use strict';
2var es = require('event-stream');
3var path = require('path');
4var childProcess = require('child_process');
5var PluginError = require('gulp-util').PluginError;
6var winExt = /^win/.test(process.platform) ? '.cmd' : '';
7
8// optimization: cache for protractor binaries directory
9var protractorDir = null;
10
11function getProtractorCli() {
12 var result = require.resolve('protractor');
13 if (result) {
14 return result.replace('index', 'cli');
15 } else {
16 throw new Error('Please check whether protractor is installed or not.');
17 }
18}
19
20function getProtractorDir() {
21 if (protractorDir) {
22 return protractorDir;
23 }
24 var result = require.resolve('protractor');
25 if (result) {
26 // console.log(result);
27 // result is now something like
28 // c:\\Source\\gulp-protractor\\node_modules\\protractor\\built\\index.js
29 protractorDir = path.resolve(path.join(path.dirname(result), '..', '..', '.bin'));
30 return protractorDir;
31 }
32 throw new Error('No protractor installation found.');
33}
34
35var protractor = function(options) {
36 var files = [],
37 child, args;
38
39 options = options || {};
40 args = options.args || [];
41
42 return es.through(function(file) {
43 files.push(file.path);
44 this.push(file);
45 }, function() {
46 var stream = this;
47
48 // Enable debug mode
49 if (options.debug) {
50 args.push('debug');
51 }
52
53 // Attach Files, if any
54 if (files.length) {
55 args.push('--specs');
56 args.push(files.join(','));
57 }
58
59 // Pass in the config file
60 if (options.configFile) {
61 args.unshift(options.configFile);
62 }
63
64 // console.log(getProtractorCli());
65
66 // child = childProcess.spawn(path.resolve(getProtractorDir() + '/protractor'), args, {
67 child = childProcess.fork(getProtractorCli(), args, {
68 stdio: 'inherit',
69 env: process.env
70 }).on('exit', function(code) {
71 if (child) {
72 child.kill();
73 }
74 if (stream) {
75 if (code) {
76 stream.emit('error', new PluginError('gulp-protractor', 'protractor exited with code ' + code));
77 } else {
78 stream.emit('end');
79 }
80 }
81 });
82 });
83};
84
85var wdUpdate = function(opts, cb) {
86 var callback = (cb ? cb : opts);
87 var options = (cb ? opts : null);
88 var args = ['update', '--standalone'];
89 if (options) {
90 if (options.webdriverManagerArgs) {
91 options.webdriverManagerArgs.forEach(function(element) {
92 args.push(element);
93 });
94 }
95 if (options.browsers) {
96 options.browsers.forEach(function(element) {
97 args.push('--' + element);
98 });
99 }
100 }
101 childProcess.spawn(path.resolve(getProtractorDir() + '/webdriver-manager' + winExt), args, {
102 stdio: 'inherit'
103 }).once('close', callback);
104};
105
106var webdriverUpdateSpecific = function(opts) {
107 return wdUpdate.bind(this, opts);
108};
109
110wdUpdate.bind(null, ['ie', 'chrome']);
111
112var webdriverStandalone = function(cb) {
113 childProcess.spawn(path.resolve(getProtractorDir() + '/webdriver-manager' + winExt), ['start'], {
114 stdio: 'inherit'
115 }).once('close', cb);
116};
117
118module.exports = {
119 getProtractorDir: getProtractorDir,
120 getProtractorCli: getProtractorCli,
121 protractor: protractor,
122 webdriver_standalone: webdriverStandalone,
123 webdriver_update: wdUpdate,
124 webdriver_update_specific: webdriverUpdateSpecific
125};