diff options
author | Jussi Kalliokoski <jussi.kalliokoski@gmail.com> | 2015-02-19 11:50:36 +0300 |
---|---|---|
committer | Forrest L Norvell <forrest@npmjs.com> | 2015-02-27 11:24:56 +0300 |
commit | f33e8b8ff2de094071c5976be95e35110cf2ab1a (patch) | |
tree | 3db5f59a06dbdfb84e768a6d4e84a589813e4992 | |
parent | 448efd0eaa6f97af0889bf47efc543a1ea2f8d7e (diff) |
Added an --if-present flag for not erroring on unspecified scripts.
Fixes #7354.
-rw-r--r-- | doc/misc/npm-config.md | 13 | ||||
-rw-r--r-- | lib/config/defaults.js | 2 | ||||
-rw-r--r-- | lib/run-script.js | 2 | ||||
-rw-r--r-- | test/tap/run-script.js | 10 |
4 files changed, 25 insertions, 2 deletions
diff --git a/doc/misc/npm-config.md b/doc/misc/npm-config.md index 7e4c10af2..e727e1666 100644 --- a/doc/misc/npm-config.md +++ b/doc/misc/npm-config.md @@ -243,7 +243,7 @@ If true, then only prints color codes for tty file descriptors. * Default: Infinity * Type: Number -The depth to go when recursing directories for `npm ls`, +The depth to go when recursing directories for `npm ls`, `npm cache ls`, and `npm outdated`. For `npm outdated`, a setting of `Infinity` will be treated as `0` @@ -390,6 +390,17 @@ A proxy to use for outgoing https requests. If the `HTTPS_PROXY` or `https_proxy` or `HTTP_PROXY` or `http_proxy` environment variables are set, proxy settings will be honored by the underlying `request` library. +### if-present + +* Default: false +* Type: Boolean + +If true, npm will not exit with an error code when `run-script` is invoked for +a script that isn't defined in the `scripts` section of `package.json`. This +option can be used when it's desirable to optionally run a script when it's +present and fail if the script fails. This is useful, for example, when running +scripts that may only apply for some builds in an otherwise generic CI setup. + ### ignore-scripts * Default: false diff --git a/lib/config/defaults.js b/lib/config/defaults.js index a46e74b5b..e49ce210b 100644 --- a/lib/config/defaults.js +++ b/lib/config/defaults.js @@ -160,6 +160,7 @@ Object.defineProperty(exports, "defaults", {get: function () { , group : process.platform === "win32" ? 0 : process.env.SUDO_GID || (process.getgid && process.getgid()) , heading: "npm" + , "if-present": false , "ignore-scripts": false , "init-module": path.resolve(home, ".npm-init.js") , "init-author-name" : "" @@ -262,6 +263,7 @@ exports.types = , "https-proxy" : [null, url] , "user-agent" : String , "heading": String + , "if-present": Boolean , "ignore-scripts": Boolean , "init-module": path , "init-author-name" : String diff --git a/lib/run-script.js b/lib/run-script.js index 7603e29e1..b6aa5fe7e 100644 --- a/lib/run-script.js +++ b/lib/run-script.js @@ -159,6 +159,8 @@ function run (pkg, wd, cmd, args, cb) { log.verbose("run-script using default platform env: env (Unix)") pkg.scripts[cmd] = "env" } + } else if (npm.config.get("if-present")) { + return cb(null); } else { return cb(new Error("missing script: " + cmd)) } diff --git a/test/tap/run-script.js b/test/tap/run-script.js index fca9a76ff..35790684f 100644 --- a/test/tap/run-script.js +++ b/test/tap/run-script.js @@ -156,10 +156,18 @@ test('npm run-script nonexistent-script', function (t) { }) }) -test('npm run-script restart when there isn"t restart', function (t) { +test('npm run-script restart when there isn\'t restart', function (t) { common.npm(['run-script', 'restart'], opts, testOutput.bind(null, t, 'stop;start')) }) +test('npm run-script nonexistent-script with --if-present flag', function (t) { + common.npm(['run-script', '--if-present', 'nonexistent-script'], opts, function (er, code, stdout, stderr) { + t.ifError(er, 'npm run-script --if-present non-existent-script ran without issue') + t.notOk(stderr, 'should not generate errors') + t.end() + }) +}) + test('npm run-script no-params (lifecycle only)', function (t) { var expected = [ 'Lifecycle scripts included in scripted:', |