-
Notifications
You must be signed in to change notification settings - Fork 35
/
index.js
122 lines (94 loc) · 3.32 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
'use strict';
var fs = require('fs');
var path = require('path');
var url = require('url');
var spawn = require('child_process').spawn;
var through = require('through2');
var gutil = require('gulp-util');
var pluginName = require('./package.json').name;
var extend = require('./extend');
function mochaPhantomJS(options) {
options = options || {};
var scriptPath = lookup('mocha-phantomjs-core/mocha-phantomjs-core.js');
if (!scriptPath) {
throw new gutil.PluginError(pluginName, 'mocha-phantomjs-core.js not found');
}
return through.obj(function (file, enc, cb) {
var args = [
scriptPath,
toURL(file.path, options.mocha),
options.reporter || 'spec',
JSON.stringify(options.phantomjs || {})
];
spawnPhantomJS(args, options, this, function (err) {
if (err) {
return cb(err);
}
this.push(file);
cb();
}.bind(this));
});
}
function toURL(path, query) {
var parsed = url.parse(path, true);
parsed.query = extend(parsed.query, query);
parsed.search = null;
if (['http:', 'https:', 'file:'].indexOf(parsed.protocol) > -1) {
return url.format(parsed);
} else {
return fileURL(url.format(parsed));
}
}
function fileURL(str) {
var pathName = path.resolve(str).replace(/\\/g, '/');
// for windows
if (pathName[0] !== '/') {
pathName = '/' + pathName;
}
return encodeURI('file://' + pathName);
}
function spawnPhantomJS(args, options, stream, cb) {
// in case npm is started with --no-bin-links
var phantomjsPath = lookup('.bin/phantomjs', true) || lookup('phantomjs-prebuilt/bin/phantomjs', true);
if (!phantomjsPath) {
return cb(new gutil.PluginError(pluginName, 'PhantomJS not found'));
}
var phantomjs = spawn(phantomjsPath, args);
if (options.dump) {
phantomjs.stdout.pipe(fs.createWriteStream(options.dump, {flags: 'a'}));
}
if (!options.suppressStdout) {
phantomjs.stdout.pipe(process.stdout);
}
if (!options.suppressStderr) {
phantomjs.stderr.pipe(process.stderr);
}
phantomjs.stdout.on('data', stream.emit.bind(stream, 'phantomjsStdoutData'));
phantomjs.stdout.on('end', stream.emit.bind(stream, 'phantomjsStdoutEnd'));
phantomjs.stderr.on('data', stream.emit.bind(stream, 'phantomjsStderrData'));
phantomjs.stderr.on('end', stream.emit.bind(stream, 'phantomjsStderrEnd'));
phantomjs.on('error', stream.emit.bind(stream, 'phantomjsError'));
phantomjs.on('exit', stream.emit.bind(stream, 'phantomjsExit'));
phantomjs.on('error', function (err) {
cb(new gutil.PluginError(pluginName, err.message));
});
phantomjs.on('exit', function (code) {
if (code === 0 || options.silent) {
cb();
} else {
cb(new gutil.PluginError(pluginName, 'test failed. phantomjs exit code: ' + code));
}
});
}
function lookup(path, isExecutable) {
for (var i = 0 ; i < module.paths.length; i++) {
var absPath = require('path').join(module.paths[i], path);
if (isExecutable && process.platform === 'win32') {
absPath += '.cmd';
}
if (fs.existsSync(absPath)) {
return absPath;
}
}
}
module.exports = mochaPhantomJS;