Permalink
Browse files

Add --ignore-scripts option.

It causes all npm commands to no longer run package.json scripts. Most notably,
`npm install --ignore-scripts` will not run preinstall and prepublish scripts.
  • Loading branch information...
sqs authored and domenic committed Aug 25, 2013
1 parent 32cc890 commit d7e67bf0d94b085652ec1c87d595afa6f650a8f6
View
@@ -368,6 +368,13 @@ Sets a User-Agent to the request header
A white-space separated list of glob patterns of files to always exclude
from packages when building tarballs.
+### ignore-scripts
+
+* Default: false
+* Type: Boolean
+
+If true, npm does not run scripts specified in package.json files.
+
### init-module
* Default: ~/.npm-init.js
View
@@ -174,10 +174,13 @@ function linkBins (pkg, folder, parent, gtop, cb) {
if (er) return cb(er)
// bins should always be executable.
// XXX skip chmod on windows?
- fs.chmod(path.resolve(folder, pkg.bin[b]), npm.modes.exec, function (er) {
+ var src = path.resolve(folder, pkg.bin[b])
+ fs.chmod(src, npm.modes.exec, function (er) {
+ if (er && er.code === "ENOENT" && npm.config.get("ignore-scripts")) {
+ return cb()
+ }
if (er || !gtop) return cb(er)
var dest = path.resolve(binRoot, b)
- , src = path.resolve(folder, pkg.bin[b])
, out = npm.config.get("parseable")
? dest + "::" + src + ":BINFILE"
: dest + " -> " + src
View
@@ -31,7 +31,7 @@ function lifecycle (pkg, stage, wd, unsafe, failOk, cb) {
if (!pkg) return cb(new Error("Invalid package data"))
log.info(stage, pkg._id)
- if (!pkg.scripts) pkg.scripts = {}
+ if (!pkg.scripts || npm.config.get('ignore-scripts')) pkg.scripts = {}
validWd(wd || path.resolve(npm.dir, pkg.name), function (er, wd) {
if (er) return cb(er)
View
@@ -0,0 +1,71 @@
+var test = require("tap").test
+var npm = require.resolve("../../bin/npm-cli.js")
+
+var spawn = require("child_process").spawn
+var node = process.execPath
+
+// ignore-scripts/package.json has scripts that always exit with non-zero error
+// codes. The "install" script is omitted so that npm tries to run node-gyp,
+// which should also fail.
+var pkg = __dirname + "/ignore-scripts"
+
+test("ignore-scripts: install using the option", function(t) {
+ createChild([npm, "install", "--ignore-scripts"]).on("close", function(code) {
+ t.equal(code, 0)
+ t.end()
+ })
+})
+
+test("ignore-scripts: install NOT using the option", function(t) {
+ createChild([npm, "install"]).on("close", function(code) {
+ t.notEqual(code, 0)
+ t.end()
+ })
+})
+
+var scripts = [
+ "prepublish", "publish", "postpublish",
+ "preinstall", "install", "postinstall",
+ "preuninstall", "uninstall", "postuninstall",
+ "preupdate", "update", "postupdate",
+ "pretest", "test", "posttest",
+ "prestop", "stop", "poststop",
+ "prestart", "start", "poststart",
+ "prerestart", "restart", "postrestart"
+]
+
+scripts.forEach(function(script) {
+ test("ignore-scripts: run-script"+script+" using the option", function(t) {
+ createChild([npm, "--ignore-scripts", "run-script", script])
+ .on("close", function(code) {
+ t.equal(code, 0)
+ t.end()
+ })
+ })
+})
+
+scripts.forEach(function(script) {
+ test("ignore-scripts: run-script "+script+" NOT using the option", function(t) {
+ createChild([npm, "run-script", script]).on("close", function(code) {
+ t.notEqual(code, 0)
+ t.end()
+ })
+ })
+})
+
+function createChild (args) {
+ var env = {
+ HOME: process.env.HOME,
+ Path: process.env.PATH,
+ PATH: process.env.PATH
+ }
+
+ if (process.platform === "win32")
+ env.npm_config_cache = "%APPDATA%\\npm-cache"
+
+ return spawn(node, args, {
+ cwd: pkg,
+ stdio: "inherit",
+ env: env
+ })
+}
@@ -0,0 +1 @@
+bad_binding_file
@@ -0,0 +1,30 @@
+{
+ "author": "Milton the Aussie",
+ "name": "ignore-scripts",
+ "version": "0.0.0",
+ "scripts": {
+ "prepublish": "exit 123",
+ "publish": "exit 123",
+ "postpublish": "exit 123",
+ "preinstall": "exit 123",
+ "postinstall": "exit 123",
+ "preuninstall": "exit 123",
+ "uninstall": "exit 123",
+ "postuninstall": "exit 123",
+ "preupdate": "exit 123",
+ "update": "exit 123",
+ "postupdate": "exit 123",
+ "pretest": "exit 123",
+ "test": "exit 123",
+ "posttest": "exit 123",
+ "prestop": "exit 123",
+ "stop": "exit 123",
+ "poststop": "exit 123",
+ "prestart": "exit 123",
+ "start": "exit 123",
+ "poststart": "exit 123",
+ "prerestart": "exit 123",
+ "restart": "exit 123",
+ "postrestart": "exit 123"
+ }
+}

0 comments on commit d7e67bf

Please sign in to comment.