Skip to content

Commit

Permalink
Chain runPackageLifecycle and runHookLifecycle.
Browse files Browse the repository at this point in the history
Instead of a large unruly lifecycle function,
break it up into 2 smaller functions that
are chained together. Thanks isaacs!
  • Loading branch information
reid committed Sep 7, 2010
1 parent b637e94 commit a028b73
Showing 1 changed file with 50 additions and 38 deletions.
88 changes: 50 additions & 38 deletions lib/utils/lifecycle.js
Expand Up @@ -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
Expand All @@ -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) {
Expand Down

0 comments on commit a028b73

Please sign in to comment.