Skip to content

Commit

Permalink
fixes #92: handled issue that which throws error if path was not found
Browse files Browse the repository at this point in the history
  • Loading branch information
chafnan committed Sep 25, 2013
2 parents 242a267 + 1246fea commit 065b6a0
Showing 1 changed file with 60 additions and 24 deletions.
84 changes: 60 additions & 24 deletions bin/mocha-phantomjs
Expand Up @@ -87,42 +87,78 @@ var config = JSON.stringify({
viewportSize: program.view,
useColors: program.color
});
var spawnArgs = [script, page, reporter, config];

var phantomjs;
if (program.path !== undefined) {
if (exists(program.path)) {
phantomjs = spawn(program.path, spawnArgs);
} else {
console.error("PhantomJS does not exist at '" + program.path + "'. Looking for PhantomJS in the PATH.");
function startPhantomJS(cmdPath, args, callback) {
var cmd = getCmdFromPath(cmdPath)
if (cmd != null) {
return spawnPhantomJS(cmd, args)
}

getCmdByEnvironmentPath(function (err, cmdPath) {
if(err || !cmdPath) {
getCmdByModulePath(function(err, cmdPath) {
if (err) {
console.error('PhantomJS was not found.');
process.exit(1);
}
spawnPhantomJS(cmdPath, args)
});
} else {
spawnPhantomJS(cmdPath, args);
}
})
}

function spawnPhantomJS(cmdPath, args) {
var phantomjs = spawn(cmdPath, args);

phantomjs.stdout.pipe(process.stdout);
phantomjs.stderr.pipe(process.stdout);

phantomjs.on('exit', function(code){
if (code === 127) { print("Perhaps phantomjs is not installed?\n"); }
process.exit(code);
});
}
if (phantomjs === undefined) {
cmd = which.sync('phantomjs');
if (cmd) {
phantomjs = spawn(cmd, spawnArgs);

function getCmdFromPath(cmdPath, callback) {
if (cmdPath === undefined) { return };
var result = null,
errorMessage = null,
tempPath = path.resolve(cmdPath);

if (exists(tempPath)) {
result = tempPath;
} else {
errorMessage = "PhantomJS does not exist at '" + cmdPath + "'. Looking for PhantomJS in the PATH."
if (!callback) {
console.error(errorMessage);
}
}

if (callback) {
callback(errorMessage, result);
} else {
return result;
}
}

function getCmdByEnvironmentPath(callback) {
which('phantomjs', callback);
}
if (phantomjs === undefined) {

function getCmdByModulePath(callback) {
for (var i=0; i < module.paths.length; i++) {
var bin = path.join(module.paths[i], '.bin/phantomjs');
if (process.platform === 'win32') {
bin += '.cmd';
}
if (exists(bin)) {
phantomjs = spawn(bin, spawnArgs);
break;
return callback(null, bin);
}
}
}
if (phantomjs === undefined) { phantomjs = spawn('phantomjs', spawnArgs); }
return callback('PhantomJS not found.', null)

phantomjs.stdout.on('data', function(data){
print(data.toString());
})

phantomjs.on('exit', function(code){
if (code === 127) { print("Perhaps phantomjs is not installed?\n"); }
process.exit(code);
})
}

startPhantomJS(program.path, [script, page, reporter, config]);

0 comments on commit 065b6a0

Please sign in to comment.