1 | 'use strict';
|
2 | var es = require('event-stream');
|
3 | var path = require('path');
|
4 | var childProcess = require('child_process');
|
5 | var PluginError = require('gulp-util').PluginError;
|
6 | var winExt = /^win/.test(process.platform) ? '.cmd' : '';
|
7 |
|
8 |
|
9 | var protractorDir = null;
|
10 |
|
11 | function 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 |
|
20 | function getProtractorDir() {
|
21 | if (protractorDir) {
|
22 | return protractorDir;
|
23 | }
|
24 | var result = require.resolve('protractor');
|
25 | if (result) {
|
26 |
|
27 |
|
28 |
|
29 | protractorDir = path.resolve(path.join(path.dirname(result), '..', '..', '.bin'));
|
30 | return protractorDir;
|
31 | }
|
32 | throw new Error('No protractor installation found.');
|
33 | }
|
34 |
|
35 | var 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 |
|
49 | if (options.debug) {
|
50 | args.push('debug');
|
51 | }
|
52 |
|
53 |
|
54 | if (files.length) {
|
55 | args.push('--specs');
|
56 | args.push(files.join(','));
|
57 | }
|
58 |
|
59 |
|
60 | if (options.configFile) {
|
61 | args.unshift(options.configFile);
|
62 | }
|
63 |
|
64 |
|
65 |
|
66 |
|
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 |
|
85 | var 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 |
|
106 | var webdriverUpdateSpecific = function(opts) {
|
107 | return wdUpdate.bind(this, opts);
|
108 | };
|
109 |
|
110 | wdUpdate.bind(null, ['ie', 'chrome']);
|
111 |
|
112 | var webdriverStandalone = function(cb) {
|
113 | childProcess.spawn(path.resolve(getProtractorDir() + '/webdriver-manager' + winExt), ['start'], {
|
114 | stdio: 'inherit'
|
115 | }).once('close', cb);
|
116 | };
|
117 |
|
118 | module.exports = {
|
119 | getProtractorDir: getProtractorDir,
|
120 | getProtractorCli: getProtractorCli,
|
121 | protractor: protractor,
|
122 | webdriver_standalone: webdriverStandalone,
|
123 | webdriver_update: wdUpdate,
|
124 | webdriver_update_specific: webdriverUpdateSpecific
|
125 | };
|