diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..9b3da3cb --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +node_modules/ +*.swo +*.swp diff --git a/phantom.coffee b/phantom.coffee index 7502d04d..0efb9e12 100644 --- a/phantom.coffee +++ b/phantom.coffee @@ -3,8 +3,8 @@ express = require 'express' child = require 'child_process' phanta = [] -startPhantomProcess = (port, args) -> - ps = child.spawn 'phantomjs', args.concat [__dirname+'/shim.js', port] +startPhantomProcess = (port, args, binaryPath) -> + ps = child.spawn binaryPath, args.concat [__dirname+'/shim.js', port] ps.stdout.on 'data', (data) -> console.log "phantom stdout: #{data}" ps.stderr.on 'data', (data) -> @@ -27,18 +27,19 @@ wrap = (ph) -> -module.exports = - create: (args..., cb) -> +module.exports = + create: (args..., cb, binaryPath) -> app = express.createServer() app.use express.static __dirname - appServer = app.listen() server = dnode() phantom = null - ps = startPhantomProcess appServer.address().port, args + binaryPath = binaryPath || 'phantomjs' + + ps = startPhantomProcess appServer.address().port, args, binaryPath ps.on 'exit', (code) -> appServer.close() @@ -52,6 +53,7 @@ module.exports = phantom = conn.remote wrap phantom phanta.push phantom + phantom.__binaryPath = binaryPath cb? phantom diff --git a/phantom.js b/phantom.js index 54b4c060..d17ea8d5 100644 --- a/phantom.js +++ b/phantom.js @@ -1,6 +1,7 @@ +// Generated by CoffeeScript 1.3.3 (function() { var child, dnode, express, phanta, startPhantomProcess, wrap, - __slice = Array.prototype.slice; + __slice = [].slice; dnode = require('dnode'); @@ -10,14 +11,16 @@ phanta = []; - startPhantomProcess = function(port, args) { + startPhantomProcess = function(port, args, binaryPath) { var ps; - ps = child.spawn('phantomjs', args.concat([__dirname + '/shim.js', port])); + ps = child.spawn(binaryPath, args.concat([__dirname + '/shim.js', port])); ps.stdout.on('data', function(data) { return console.log("phantom stdout: " + data); }); ps.stderr.on('data', function(data) { - if (data.toString('utf8').match(/No such method.*socketSentData/)) return; + if (data.toString('utf8').match(/No such method.*socketSentData/)) { + return; + } return console.warn("phantom stderr: " + data); }); return ps; @@ -48,14 +51,15 @@ module.exports = { create: function() { - var app, appServer, args, cb, io, phantom, ps, server, _i; - args = 2 <= arguments.length ? __slice.call(arguments, 0, _i = arguments.length - 1) : (_i = 0, []), cb = arguments[_i++]; + var app, appServer, args, binaryPath, cb, io, phantom, ps, server, _i; + args = 3 <= arguments.length ? __slice.call(arguments, 0, _i = arguments.length - 2) : (_i = 0, []), cb = arguments[_i++], binaryPath = arguments[_i++]; app = express.createServer(); - app.use(express.static(__dirname)); + app.use(express["static"](__dirname)); appServer = app.listen(); server = dnode(); phantom = null; - ps = startPhantomProcess(appServer.address().port, args); + binaryPath = binaryPath || 'phantomjs'; + ps = startPhantomProcess(appServer.address().port, args, binaryPath); ps.on('exit', function(code) { var p; appServer.close(); @@ -64,7 +68,9 @@ _results = []; for (_j = 0, _len = phanta.length; _j < _len; _j++) { p = phanta[_j]; - if (p !== phantom) _results.push(p); + if (p !== phantom) { + _results.push(p); + } } return _results; })(); @@ -79,6 +85,7 @@ phantom = conn.remote; wrap(phantom); phanta.push(phantom); + phantom.__binaryPath = binaryPath; return typeof cb === "function" ? cb(phantom) : void 0; }); }