-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
61 lines (48 loc) · 1.4 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
exports.summary = 'Run in phantomjs sandbox';
exports.usage = '<script> [options]';
exports.options = {
"script" : {
required: true,
describe : 'script'
},
"args" : {
describe : 'arguments passed to phantomjs script'
},
"opts" : {
describe : 'command line options passed to phantomjs'
}
};
exports.run = function (options, done) {
var script = options.script;
var opts = options.opts;
var args = options.args;
var childProcess = require('child_process');
var phantomjs = require('phantomjs');
var binPath = phantomjs.path;
var childArgs = [];
// https://github.com/ariya/phantomjs/wiki/API-Reference#command-line-options
if(opts){
Object.keys(opts).forEach(function(key) {
var val = opts[key];
if (!/^\-\-/.test(key)) {
key = '--' + key;
}
childArgs.push(key + '=' + val);
});
}
if(script) childArgs.push(script);
if(args){
childArgs = childArgs.concat(args);
}
exports.log("Running PhantomJS...")
var cp = childProcess.execFile(binPath, childArgs, function(err, stdout, stderr) {
done(err);
});
cp.stdout.on('data', function (data) {
process.stdout.write(data);
});
cp.stderr.on('data', function (data) {
process.stderr.write(data);
});
return cp;
};