From a09c1a69df05b753464cc1272cdccc6af0f4da5a Mon Sep 17 00:00:00 2001 From: mmkal Date: Tue, 4 Jul 2017 00:25:35 -0400 Subject: [PATCH] run-script: allow custom shell (#16687) Fixes: https://github.com/npm/npm/issues/6543 PR-URL: https://github.com/npm/npm/pull/16687 Credit: @mmkal Reviewed-By: @zkat Reviewed-By: @iarna --- doc/misc/npm-config.md | 7 +++++++ lib/config/defaults.js | 2 ++ lib/utils/lifecycle.js | 6 +++++- test/tap/run-script.js | 14 ++++++++++++++ 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/doc/misc/npm-config.md b/doc/misc/npm-config.md index e026c5e4ca350..dd0993d6bb795 100644 --- a/doc/misc/npm-config.md +++ b/doc/misc/npm-config.md @@ -889,6 +889,13 @@ in to a private registry for the first time: will cause `@organization` to be mapped to the registry for future installation of packages specified according to the pattern `@organization/package`. +### script-shell + +* Default: `null` +* Type: path + +The shell to use for scripts run with the `npm run` command. + ### scripts-prepend-node-path * Default: "warn-only" diff --git a/lib/config/defaults.js b/lib/config/defaults.js index da019ac4d6d19..93bac84a6108f 100644 --- a/lib/config/defaults.js +++ b/lib/config/defaults.js @@ -203,6 +203,7 @@ Object.defineProperty(exports, 'defaults', {get: function () { 'save-prefix': '^', 'save-prod': false, scope: '', + 'script-shell': null, 'scripts-prepend-node-path': 'warn-only', searchopts: '', searchexclude: null, @@ -326,6 +327,7 @@ exports.types = { 'save-prefix': String, 'save-prod': Boolean, scope: String, + 'script-shell': [null, String], 'scripts-prepend-node-path': [false, true, 'auto', 'warn-only'], searchopts: String, searchexclude: [null, String], diff --git a/lib/utils/lifecycle.js b/lib/utils/lifecycle.js index 02ba485aee145..412c1c69448a9 100644 --- a/lib/utils/lifecycle.js +++ b/lib/utils/lifecycle.js @@ -264,7 +264,11 @@ function runCmd_ (cmd, pkg, env, wd, stage, unsafe, uid, gid, cb_) { var sh = 'sh' var shFlag = '-c' - if (process.platform === 'win32') { + var customShell = npm.config.get('script-shell') + + if (customShell) { + sh = customShell + } else if (process.platform === 'win32') { sh = process.env.comspec || 'cmd' shFlag = '/d /s /c' conf.windowsVerbatimArguments = true diff --git a/test/tap/run-script.js b/test/tap/run-script.js index 4dea9b83604bf..3ff1e9f4aee28 100644 --- a/test/tap/run-script.js +++ b/test/tap/run-script.js @@ -75,6 +75,14 @@ var exitCode = { } } +var shell = { + name: 'scripted', + version: '1.2.3', + scripts: { + 'start': 'echo foo' + } +} + function testOutput (t, command, er, code, stdout, stderr) { var lines @@ -308,6 +316,12 @@ test('npm run-script no-params (direct only)', function (t) { }) }) +test('npm run-script script-shell config', function (t) { + writeMetadata(shell) + + common.npm(['run-script', 'start', '--script-shell', 'echo'], opts, testOutput.bind(null, t, '-c echo foo')) +}) + test('npm run-script no-params (direct only)', function (t) { var expected = [ 'Lifecycle scripts included in scripted:',