]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/lib/publish.js
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / lib / publish.js
1
2 module.exports = publish
3
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")
16
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"
21
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.
26   return cb()
27 }
28
29 function publish (args, isRetry, cb) {
30   if (typeof cb !== "function") {
31     cb = isRetry
32     isRetry = false
33   }
34   if (args.length === 0) args = ["."]
35   if (args.length !== 1) return cb(publish.usage)
36
37   log.verbose("publish", args)
38
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)
42     return cb(er)
43   }
44
45   var arg = args[0]
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)
49
50     if (data) {
51       if (!data.name) return cb(new Error("No name provided"))
52       if (!data.version) return cb(new Error("No version provided"))
53     }
54
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)
60   })
61 }
62
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
67 // in the cache dir.
68 function cacheAddPublish (dir, didPre, isRetry, cb) {
69   npm.commands.cache.add(dir, null, null, false, function (er, data) {
70     if (er) return cb(er)
71     log.silly("publish", data)
72     var cachedir = path.resolve(cachedPackageRoot(data), "package")
73     chain([ !didPre &&
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] ]
78       , cb )
79   })
80 }
81
82 function publish_ (arg, data, isRetry, cachedir, cb) {
83   if (!data) return cb(new Error("no package.json file found"))
84
85   var mappedConfig = getPublishConfig(
86     data.publishConfig,
87     npm.config,
88     npm.registry
89   )
90   var config = mappedConfig.config
91   var registry = mappedConfig.client
92
93   data._npmVersion  = npm.version
94   data._nodeVersion = process.versions.node
95
96   delete data.modules
97   if (data.private) return cb(
98     new Error(
99       "This package has been marked as private\n" +
100       "Remove the 'private' field from the package.json to publish it."
101     )
102   )
103
104   mapToRegistry(data.name, config, function (er, registryURI, auth, registryBase) {
105     if (er) return cb(er)
106
107     var tarballPath = cachedir + ".tgz"
108
109     // we just want the base registry URL in this case
110     log.verbose("publish", "registryBase", registryBase)
111     log.silly("publish", "uploading", tarballPath)
112
113     data._npmUser = {
114       name  : auth.username,
115       email : auth.email
116     }
117
118     var params = {
119       metadata : data,
120       body     : createReadStream(tarballPath),
121       auth     : auth
122     }
123
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."))
129       }
130
131       params.access = config.get("access")
132     }
133
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)
142         })
143       }
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)
148       cb()
149     })
150   })
151 }