Skip to content

Commit

Permalink
adds preferLocal option for including 'node_modules/.bin' to path (#102)
Browse files Browse the repository at this point in the history
closes #101

Closes #102
  • Loading branch information
pflannery authored and sindresorhus committed Apr 15, 2016
1 parent c9ce41e commit 058629c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@
"cli"
],
"dependencies": {
"chalk": "^1.0.0"
"chalk": "^1.0.0",
"npm-run-path": "^1.0.0",
"object-assign": "^4.0.0",
"path-key": "^1.0.0"
},
"devDependencies": {
"grunt": "^0.4.5",
Expand Down
7 changes: 7 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,13 @@ Lets you override the default callback with your own.

**Make sure to call the `cb` method when you're done.**

### preferLocal

Type: `boolean`<br>
Default: `false`

When set to `true` will execute local binaries by name like npm run script

### execOptions

Type: `object`
Expand Down
14 changes: 13 additions & 1 deletion tasks/shell.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
'use strict';
var exec = require('child_process').exec;
var chalk = require('chalk');
var npmRunPath = require('npm-run-path');
var objectAssign = require('object-assign');
var pathKey = require('path-key');

module.exports = function (grunt) {
grunt.registerMultiTask('shell', 'Run shell commands', function () {
Expand All @@ -10,7 +13,11 @@ module.exports = function (grunt) {
stderr: true,
stdin: true,
failOnError: true,
stdinRawMode: false
stdinRawMode: false,
preferLocal: false,
execOptions: {
env: null
}
});

var cmd = typeof this.data === 'string' ? this.data : this.data.command;
Expand All @@ -21,6 +28,11 @@ module.exports = function (grunt) {

cmd = grunt.template.process(typeof cmd === 'function' ? cmd.apply(grunt, arguments) : cmd);

if (options.preferLocal === true) {
options.execOptions.env = options.execOptions.env || objectAssign({}, process.env);
options.execOptions.env[pathKey()] = npmRunPath(options.execOptions.env);
}

var cp = exec(cmd, options.execOptions, function (err, stdout, stderr) {
if (typeof options.callback === 'function') {
options.callback.call(this, err, stdout, stderr, cb);
Expand Down

0 comments on commit 058629c

Please sign in to comment.