forked from mrhooray/gulp-mocha-phantomjs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
83 lines (67 loc) · 2.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
'use strict';
var fs = require('fs');
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;
function mochaPhantomJS(options) {
options = options || {};
var scriptPath = lookup('mocha-phantomjs/lib/mocha-phantomjs.coffee');
if (!scriptPath) {
throw new gutil.PluginError(pluginName, 'mocha-phantomjs.coffee not found');
}
return through.obj(function (file, enc, cb) {
var args = [
scriptPath,
url.format({
pathname: file.path.split(require('path').sep).join('/'),
query: options.mocha || {}
}),
options.reporter || 'spec',
JSON.stringify(options.phantomjs || {})
];
spawnPhantomJS(args, options, function (err) {
if (err) {
this.emit('error', err);
}
this.push(file);
cb();
}.bind(this));
});
}
function spawnPhantomJS(args, options, cb) {
// in case npm is started with --no-bin-links
var phantomjsPath = lookup('.bin/phantomjs', true) || lookup('phantomjs/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));
}
phantomjs.stdout.pipe(process.stdout);
phantomjs.stderr.pipe(process.stderr);
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'));
}
});
}
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;