Skip to content
This repository has been archived by the owner on Jun 5, 2020. It is now read-only.

Use global binaries, when all else fails #34

Merged
merged 4 commits into from
Nov 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
language: node_js
sudo: false
node_js:
- "0.10"
- "0.12"
- "4"
- "5"
before_install:
- npm i -g npm
21 changes: 11 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,27 @@
"license": "MIT",
"main": "./tasks/buster.js",
"engines": {
"node": ">= 0.8.0"
"node": ">= 4"
},
"scripts": {
"start": "grunt --force default watch",
"test": "grunt"
},
"devDependencies": {
"buster": ">=0.7.6",
"grunt": "~0.4.1",
"grunt-cli": "~0.1.8",
"grunt-contrib-jshint": "~0.10.0",
"grunt-contrib-watch": "~0.4.0"
"buster": "0.7.x",
"grunt": "0.4.x",
"grunt-cli": "0.1.x",
"grunt-contrib-jshint": "0.11.x",
"grunt-contrib-watch": "0.6.x"
},
"dependencies": {
"when": "~2.0.0",
"resolve-bin": "~0.3.0"
"resolve-bin": "0.3.x",
"when": "3.x",
"which": "1.x"
},
"peerDependencies": {
"buster": ">=0.7.x",
"grunt": "~0.4.0"
"buster": ">=0.7.6",
"grunt": ">=0.4.0"
},
"keywords": [
"gruntplugin",
Expand Down
15 changes: 13 additions & 2 deletions tasks/buster/cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,21 @@ var cp = require('child_process');
var grunt = require('grunt');
var when = require('when');
var resolveBin = require('resolve-bin');
var which = require('which');

var findExecutable = function (moduleName, cmd, cb) {
resolveBin(moduleName, {executable:cmd}, function (error, path) {
if (error) { // failed to find in [global/local] node_modules - fallback to PATH
which(cmd, cb);
} else {
cb(null, path);
}
});
};

exports.run = function (moduleName, cmd, args, callback) {
callback = callback || function () {};
resolveBin(moduleName, { executable: cmd }, function (error, path) {
findExecutable(moduleName, cmd, function (error, path) {
if (error) {
callback(error);
} else {
Expand Down Expand Up @@ -103,7 +114,7 @@ exports.runPhantomjs = function (args) {
exports.run('phantomjs', 'phantomjs', args, function (error, phantomProcess) {
if (error) {
grunt.log.error(
'PhantomJS not found. Run `npm install phantomjs` to install.');
'PhantomJS not found. Run `npm install [-g] phantomjs` to install.');
deferred.reject();
} else {
phantomProcess.stdout.on('data', function (data) {
Expand Down
12 changes: 11 additions & 1 deletion test/cmd-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,20 @@ buster.testCase('Cmd', {
});
},

'calls callback with results when a global binary found': function (done) {
var spawnStub = this.spawnStub;
cmd.run('node', 'node', [ 1, 2, 3 ], function (err) {
assert.isNull(err);
// node is the only executable I can trust to be in actual PATH - I just dont' know that path, but there should definitely be no error
assert.calledOnce(spawnStub);
done();
});
},

'calls callback with error when resolve-bin fails': function (done) {
cmd.run('no-such-module', 'no-such-file', [], function (err, actualHandle) {
refute.isNull(err);
assert.match(err.message, 'cannot find');
assert.match(err.message, 'not found');
refute(actualHandle);
done();
});
Expand Down