From 058629c8bd4a310b4b69022b829b123bc39277cc Mon Sep 17 00:00:00 2001 From: pflannery Date: Fri, 15 Apr 2016 20:16:22 +0700 Subject: [PATCH] adds preferLocal option for including 'node_modules/.bin' to path (#102) closes #101 Closes #102 --- package.json | 5 ++++- readme.md | 7 +++++++ tasks/shell.js | 14 +++++++++++++- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index d8a75e3..b859b1f 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/readme.md b/readme.md index a2a98b4..f4cff15 100644 --- a/readme.md +++ b/readme.md @@ -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`
+Default: `false` + +When set to `true` will execute local binaries by name like npm run script + ### execOptions Type: `object` diff --git a/tasks/shell.js b/tasks/shell.js index 747ccc1..74a09ed 100644 --- a/tasks/shell.js +++ b/tasks/shell.js @@ -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 () { @@ -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; @@ -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);