2 module.exports = publish
4 var npm = require("./npm.js")
5 , log = require("npmlog")
6 , path = require("path")
7 , readJson = require("read-package-json")
8 , lifecycle = require("./utils/lifecycle.js")
9 , chain = require("slide").chain
10 , mapToRegistry = require("./utils/map-to-registry.js")
11 , cachedPackageRoot = require("./cache/cached-package-root.js")
12 , createReadStream = require("graceful-fs").createReadStream
13 , npa = require("npm-package-arg")
14 , semver = require('semver')
15 , getPublishConfig = require("./utils/get-publish-config.js")
17 publish.usage = "npm publish <tarball> [--tag <tagname>]"
18 + "\nnpm publish <folder> [--tag <tagname>]"
19 + "\n\nPublishes '.' if no argument supplied"
20 + "\n\nSets tag `latest` if no --tag specified"
22 publish.completion = function (opts, cb) {
23 // publish can complete to a folder with a package.json
24 // or a tarball, or a tarball url.
25 // for now, not yet implemented.
29 function publish (args, isRetry, cb) {
30 if (typeof cb !== "function") {
34 if (args.length === 0) args = ["."]
35 if (args.length !== 1) return cb(publish.usage)
37 log.verbose("publish", args)
39 var t = npm.config.get('tag').trim()
40 if (semver.validRange(t)) {
41 var er = new Error("Tag name must not be a valid SemVer range: " + t)
46 // if it's a local folder, then run the prepublish there, first.
47 readJson(path.resolve(arg, "package.json"), function (er, data) {
48 if (er && er.code !== "ENOENT" && er.code !== "ENOTDIR") return cb(er)
51 if (!data.name) return cb(new Error("No name provided"))
52 if (!data.version) return cb(new Error("No version provided"))
55 // Error is OK. Could be publishing a URL or tarball, however, that means
56 // that we will not have automatically run the prepublish script, since
57 // that gets run when adding a folder to the cache.
58 if (er) return cacheAddPublish(arg, false, isRetry, cb)
59 else cacheAddPublish(arg, true, isRetry, cb)
63 // didPre in this case means that we already ran the prepublish script,
64 // and that the "dir" is an actual directory, and not something silly
65 // like a tarball or name@version thing.
66 // That means that we can run publish/postpublish in the dir, rather than
68 function cacheAddPublish (dir, didPre, isRetry, cb) {
69 npm.commands.cache.add(dir, null, null, false, function (er, data) {
71 log.silly("publish", data)
72 var cachedir = path.resolve(cachedPackageRoot(data), "package")
74 [lifecycle, data, "prepublish", cachedir]
75 , [publish_, dir, data, isRetry, cachedir]
76 , [lifecycle, data, "publish", didPre ? dir : cachedir]
77 , [lifecycle, data, "postpublish", didPre ? dir : cachedir] ]
82 function publish_ (arg, data, isRetry, cachedir, cb) {
83 if (!data) return cb(new Error("no package.json file found"))
85 var mappedConfig = getPublishConfig(
90 var config = mappedConfig.config
91 var registry = mappedConfig.client
93 data._npmVersion = npm.version
94 data._nodeVersion = process.versions.node
97 if (data.private) return cb(
99 "This package has been marked as private\n" +
100 "Remove the 'private' field from the package.json to publish it."
104 mapToRegistry(data.name, config, function (er, registryURI, auth, registryBase) {
105 if (er) return cb(er)
107 var tarballPath = cachedir + ".tgz"
109 // we just want the base registry URL in this case
110 log.verbose("publish", "registryBase", registryBase)
111 log.silly("publish", "uploading", tarballPath)
114 name : auth.username,
120 body : createReadStream(tarballPath),
124 // registry-frontdoor cares about the access level, which is only
125 // configurable for scoped packages
126 if (config.get("access")) {
127 if (!npa(data.name).scope && config.get("access") === "restricted") {
128 return cb(new Error("Can't restrict access to unscoped packages."))
131 params.access = config.get("access")
134 registry.publish(registryBase, params, function (er) {
135 if (er && er.code === "EPUBLISHCONFLICT" &&
136 npm.config.get("force") && !isRetry) {
137 log.warn("publish", "Forced publish over " + data._id)
138 return npm.commands.unpublish([data._id], function (er) {
139 // ignore errors. Use the force. Reach out with your feelings.
140 // but if it fails again, then report the first error.
141 publish([arg], er || true, cb)
144 // report the unpublish error if this was a retry and unpublish failed
145 if (er && isRetry && isRetry !== true) return cb(isRetry)
146 if (er) return cb(er)
147 console.log("+ " + data._id)