diff options
-rw-r--r-- | doc/scripts.md | 6 | ||||
-rw-r--r-- | lib/publish.js | 59 |
2 files changed, 41 insertions, 24 deletions
diff --git a/doc/scripts.md b/doc/scripts.md index 0be621173..681e95656 100644 --- a/doc/scripts.md +++ b/doc/scripts.md @@ -18,8 +18,10 @@ following scripts: Run BEFORE the package is updated with the update command. * update, postupdate: Run AFTER the package is updated with the update command. -* (pre,post,)updatedependency-foo: - Run (before,after) the "foo" dependency is modified. +* prepublish: + Run BEFORE the package is published. +* publish, postpublish: + Run AFTER the package is published. * pretest, test, posttest: Run by the `npm test` command. * prestop, stop, poststop: diff --git a/lib/publish.js b/lib/publish.js index 3fab6f35d..e695de277 100644 --- a/lib/publish.js +++ b/lib/publish.js @@ -9,6 +9,8 @@ var npm = require("../npm") , path = require("path") , readJson = require("./utils/read-json") , fs = require("fs") + , lifecycle = require("./utils/lifecycle") + , chain = require("./utils/chain") publish.usage = "npm publish <tarball>" + "\nnpm publish <folder>" @@ -28,34 +30,47 @@ function publish (args, isRetry, cb) { npm.commands.cache.add(args[0], args[1], true, function (er, data) { if (er) return cb(er) log.silly(data, "publish") + var cachedir = path.resolve( npm.cache + , data.name + , data.version + , "package" ) + chain + ( [lifecycle, data, "prepublish", cachedir] + , [publish_, args, data, isRetry] + , [lifecycle, data, "publish", cachedir] + , [lifecycle, data, "postpublish", cachedir] + , cb ) + }) +} - // check for publishConfig hash - if (data.publishConfig) { - Object.keys(data.publishConfig).forEach(function (k) { - log.info(k + "=" + data.publishConfig[k], "publishConfig") - npm.config.set(k, data.publishConfig[k]) - }) - } +function publish_ (args, data, isRetry, cb) { + // check for publishConfig hash + if (data.publishConfig) { + Object.keys(data.publishConfig).forEach(function (k) { + log.info(k + "=" + data.publishConfig[k], "publishConfig") + npm.config.set(k, data.publishConfig[k]) + }) + } - if (!data) return cb(new Error("no package.json file found")) - delete data.modules - if (data.private) return cb(new Error - ("This package has been marked as private\n" - +"Remove the 'private' field from the package.json to publish it.")) + if (!data) return cb(new Error("no package.json file found")) + delete data.modules + if (data.private) return cb(new Error + ("This package has been marked as private\n" + +"Remove the 'private' field from the package.json to publish it.")) - // pre-build - var bd = data.scripts - && ( data.scripts.preinstall - || data.scripts.install - || data.scripts.postinstall ) - && npm.config.get("bindist") - preBuild(data, bd, function (er, tb) { - if (er) return cb(er) - return regPublish(data, tb, isRetry, args, cb) - }) + // pre-build + var bd = data.scripts + && ( data.scripts.preinstall + || data.scripts.install + || data.scripts.postinstall ) + && npm.config.get("bindist") + preBuild(data, bd, function (er, tb) { + if (er) return cb(er) + return regPublish(data, tb, isRetry, args, cb) }) } + function preBuild (data, bd, cb) { if (!bd) return cb() // unpack to cache/n/v/build |