Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Chain runPackageLifecycle and runHookLifecycle.

Instead of a large unruly lifecycle function,
break it up into 2 smaller functions that
are chained together. Thanks isaacs!
  • Loading branch information...
commit a028b736e081f12bc32213bd9e4643791c26d9a1 1 parent b637e94
@reid authored
Showing with 50 additions and 38 deletions.
  1. +50 −38 lib/utils/lifecycle.js
View
88 lib/utils/lifecycle.js
@@ -8,6 +8,7 @@ var log = require("./log")
, path = require("path")
, readJson = require("./read-json")
, fs = require("./graceful-fs")
+ , chain = require("./chain")
function lifecycle (pkg, stage, cb) {
while (pkg && pkg._data) pkg = pkg._data
@@ -19,48 +20,59 @@ function lifecycle (pkg, stage, cb) {
env.npm_lifecycle_event = stage
log.silly(env, "lifecycle env")
- function runHook () { // check for a hook script
- var hook = path.join(npm.dir, ".hooks", stage)
- fs.stat(hook, function (er) {
- if (er) return cb()
- exec(hook, [], env, function (er) {
- if (er) log("Failed to exec "+stage+" hook script", pkg._id)
- if (npm.ROLLBACK) return cb()
- cb(er)
- })
- })
+ var packageLifecycle = pkg.scripts && (stage in pkg.scripts)
+
+ if (packageLifecycle) {
+ // define this here so it's available to all scripts.
+ env.npm_lifecycle_script = pkg.scripts[stage]
}
- if (pkg.scripts && (stage in pkg.scripts)) {
- // define the lifecycle script, since it exists.
- // this will also be available in runHook
- env.npm_lifecycle_script = pkg.scripts[stage]
+ chain
+ ( packageLifecycle && [runPackageLifecycle, pkg, env]
+ , [runHookLifecycle, pkg, env]
+ , cb
+ )
+}
- // run package lifecycle scripts in the package root, or the nearest parent.
- var d = path.join(npm.dir, pkg.name, pkg.version, "package")
- while (d) {
- try {
- process.chdir(d)
- break
- } catch (ex) {
- d = path.dirname(d)
- }
- }
- log(pkg._id, stage)
+function runPackageLifecycle (pkg, env, cb) {
+ // run package lifecycle scripts in the package root, or the nearest parent.
+ var stage = env.npm_lifecycle_event
+ var d = path.join(npm.dir, pkg.name, pkg.version, "package")
+ while (d) {
+ try {
+ process.chdir(d)
+ break
+ } catch (ex) {
+ d = path.dirname(d)
+ }
+ }
+ log(pkg._id, stage)
- exec("sh", ["-c", env.npm_lifecycle_script], env, function (er) {
- if (er && !npm.ROLLBACK) {
- log("Failed to exec "+stage+" script", pkg._id)
- er.message = pkg._id + " " + stage + ": `" + env.npm_lifecycle_script+"`\n"
- + er.message
- return cb(er)
- } else if (er) {
- log.error(er, pkg._id+"."+stage+" failed")
- }
- runHook()
- })
- } else runHook() // try to run a hook script even
- // if a pkg.script isn't defined
+ exec("sh", ["-c", env.npm_lifecycle_script], env, function (er) {
+ if (er && !npm.ROLLBACK) {
+ log("Failed to exec "+stage+" script", pkg._id)
+ er.message = pkg._id + " " + stage + ": `" + env.npm_lifecycle_script+"`\n"
+ + er.message
+ return cb(er)
+ } else if (er) {
+ log.error(er, pkg._id+"."+stage+" failed")
+ }
+ cb(er)
+ })
+}
+
+function runHookLifecycle (pkg, env, cb) {
+ // check for a hook script, run if present.
+ var stage = env.npm_lifecycle_event
+ var hook = path.join(npm.dir, ".hooks", stage)
+ fs.stat(hook, function (er) {
+ if (er) return cb()
+ exec(hook, [], env, function (er) {
+ if (er) log("Failed to exec "+stage+" hook script", pkg._id)
+ if (npm.ROLLBACK) return cb()
+ cb(er)
+ })
+ })
}
function makeEnv (data, prefix, env) {
Please sign in to comment.
Something went wrong with that request. Please try again.