Skip to content

Commit f7a3caa

Browse files
reidisaacs
authored andcommitted
Run hook scripts even if a pkg.script isn't used.
1 parent 4e127e4 commit f7a3caa

File tree

1 file changed

+35
-27
lines changed

1 file changed

+35
-27
lines changed

lib/utils/lifecycle.js

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,13 @@ function lifecycle (pkg, stage, cb) {
1313
while (pkg && pkg._data) pkg = pkg._data
1414
if (!pkg) return cb(new Error("Invalid package data"))
1515
log.verbose(pkg._id, "lifecycle "+stage)
16-
if (!pkg.scripts || !(stage in pkg.scripts)) return cb()
1716

18-
// run package lifecycle scripts in the package root, or the nearest parent.
19-
var d = path.join(npm.dir, pkg.name, pkg.version, "package")
20-
while (d) {
21-
try {
22-
process.chdir(d)
23-
break
24-
} catch (ex) {
25-
d = path.dirname(d)
26-
}
27-
}
28-
log(pkg._id, stage)
29-
30-
// set the env variables, then run the script as a child process.
31-
// NOTE: The env vars won't work until node supports env hashes for child procs
17+
// set the env variables, then run scripts as a child process.
3218
var env = makeEnv(pkg)
3319
env.npm_lifecycle_event = stage
34-
env.npm_lifecycle_script = pkg.scripts[stage]
3520
log.silly(env, "lifecycle env")
36-
exec("sh", ["-c", env.npm_lifecycle_script], env, function (er) {
37-
if (er && !npm.ROLLBACK) {
38-
log("Failed to exec "+stage+" script", pkg._id)
39-
er.message = pkg._id + " " + stage + ": `" + env.npm_lifecycle_script+"`\n"
40-
+ er.message
41-
return cb(er)
42-
} else if (er) {
43-
log.error(er, pkg._id+"."+stage+" failed")
44-
}
45-
// check for a hook script
21+
22+
function runHook () { // check for a hook script
4623
var hook = path.join(npm.dir, ".hooks", stage)
4724
fs.stat(hook, function (er) {
4825
if (er) return cb()
@@ -52,7 +29,38 @@ function lifecycle (pkg, stage, cb) {
5229
cb(er)
5330
})
5431
})
55-
})
32+
}
33+
34+
if (pkg.scripts && (stage in pkg.scripts)) {
35+
// define the lifecycle script, since it exists.
36+
// this will also be available in runHook
37+
env.npm_lifecycle_script = pkg.scripts[stage]
38+
39+
// run package lifecycle scripts in the package root, or the nearest parent.
40+
var d = path.join(npm.dir, pkg.name, pkg.version, "package")
41+
while (d) {
42+
try {
43+
process.chdir(d)
44+
break
45+
} catch (ex) {
46+
d = path.dirname(d)
47+
}
48+
}
49+
log(pkg._id, stage)
50+
51+
exec("sh", ["-c", env.npm_lifecycle_script], env, function (er) {
52+
if (er && !npm.ROLLBACK) {
53+
log("Failed to exec "+stage+" script", pkg._id)
54+
er.message = pkg._id + " " + stage + ": `" + env.npm_lifecycle_script+"`\n"
55+
+ er.message
56+
return cb(er)
57+
} else if (er) {
58+
log.error(er, pkg._id+"."+stage+" failed")
59+
}
60+
runHook()
61+
})
62+
} else runHook() // try to run a hook script even
63+
// if a pkg.script isn't defined
5664
}
5765

5866
function makeEnv (data, prefix, env) {

0 commit comments

Comments
 (0)