]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/lib/npm.js
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / lib / npm.js
1 ;(function(){
2 // windows: running "npm blah" in this folder will invoke WSH, not node.
3 if (typeof WScript !== "undefined") {
4   WScript.echo("npm does not work when run\n"
5               +"with the Windows Scripting Host\n\n"
6               +"'cd' to a different directory,\n"
7               +"or type 'npm.cmd <args>',\n"
8               +"or type 'node npm <args>'.")
9   WScript.quit(1)
10   return
11 }
12
13
14 var EventEmitter = require("events").EventEmitter
15   , npm = module.exports = new EventEmitter()
16   , npmconf = require("./config/core.js")
17   , log = require("npmlog")
18   , gfs = require('graceful-fs')
19   , fs = gfs.gracefulify(require('fs'))
20   , path = require("path")
21   , abbrev = require("abbrev")
22   , which = require("which")
23   , CachingRegClient = require("./cache/caching-client.js")
24   , charSpin = require("char-spinner")
25
26 npm.config = {
27   loaded: false,
28   get: function() {
29     throw new Error('npm.load() required')
30   },
31   set: function() {
32     throw new Error('npm.load() required')
33   }
34 }
35
36 npm.commands = {}
37
38 npm.rollbacks = []
39
40 try {
41   // startup, ok to do this synchronously
42   var j = JSON.parse(fs.readFileSync(
43     path.join(__dirname, "../package.json"))+"")
44   npm.version = j.version
45 } catch (ex) {
46   try {
47     log.info("error reading version", ex)
48   } catch (er) {}
49   npm.version = ex
50 }
51
52 var commandCache = {}
53   // short names for common things
54   , aliases = { "rm" : "uninstall"
55               , "r" : "uninstall"
56               , "un" : "uninstall"
57               , "unlink" : "uninstall"
58               , "remove" : "uninstall"
59               , "rb" : "rebuild"
60               , "list" : "ls"
61               , "la" : "ls"
62               , "ll" : "ls"
63               , "ln" : "link"
64               , "i" : "install"
65               , "isntall" : "install"
66               , "up" : "update"
67               , "upgrade" : "update"
68               , "c" : "config"
69               , "dist-tags" : "dist-tag"
70               , "info" : "view"
71               , "show" : "view"
72               , "find" : "search"
73               , "s" : "search"
74               , "se" : "search"
75               , "author" : "owner"
76               , "home" : "docs"
77               , "issues": "bugs"
78               , "unstar": "star" // same function
79               , "apihelp" : "help"
80               , "login": "adduser"
81               , "add-user": "adduser"
82               , "tst": "test"
83               , "t": "test"
84               , "find-dupes": "dedupe"
85               , "ddp": "dedupe"
86               , "v": "view"
87               , "verison": "version"
88               }
89
90   , aliasNames = Object.keys(aliases)
91   // these are filenames in .
92   , cmdList = [ "install"
93               , "uninstall"
94               , "cache"
95               , "config"
96               , "set"
97               , "get"
98               , "update"
99               , "outdated"
100               , "prune"
101               , "pack"
102               , "dedupe"
103
104               , "rebuild"
105               , "link"
106
107               , "publish"
108               , "star"
109               , "stars"
110               , "tag"
111               , "adduser"
112               , "logout"
113               , "unpublish"
114               , "owner"
115               , "access"
116               , "team"
117               , "deprecate"
118               , "shrinkwrap"
119
120               , "help"
121               , "help-search"
122               , "ls"
123               , "search"
124               , "view"
125               , "init"
126               , "version"
127               , "edit"
128               , "explore"
129               , "docs"
130               , "repo"
131               , "bugs"
132               , "faq"
133               , "root"
134               , "prefix"
135               , "bin"
136               , "whoami"
137               , "dist-tag"
138               , "ping"
139
140               , "test"
141               , "stop"
142               , "start"
143               , "restart"
144               , "run-script"
145               , "completion"
146               ]
147   , plumbing = [ "build"
148                , "unbuild"
149                , "xmas"
150                , "substack"
151                , "visnup"
152                ]
153   , littleGuys = [ "isntall", "verison" ]
154   , fullList = cmdList.concat(aliasNames).filter(function (c) {
155       return plumbing.indexOf(c) === -1
156     })
157   , abbrevs = abbrev(fullList)
158
159 // we have our reasons
160 fullList = npm.fullList = fullList.filter(function (c) {
161   return littleGuys.indexOf(c) === -1
162 })
163
164 npm.spinner =
165   { int: null
166   , started: false
167   , start: function () {
168       if (npm.spinner.int) return
169       var c = npm.config.get("spin")
170       if (!c) return
171       var stream = npm.config.get("logstream")
172       var opt = { tty: c !== "always", stream: stream }
173       opt.cleanup = !npm.spinner.started
174       npm.spinner.int = charSpin(opt)
175       npm.spinner.started = true
176     }
177   , stop: function () {
178       clearInterval(npm.spinner.int)
179       npm.spinner.int = null
180     }
181   }
182
183 Object.keys(abbrevs).concat(plumbing).forEach(function addCommand (c) {
184   Object.defineProperty(npm.commands, c, { get : function () {
185     if (!loaded) throw new Error(
186       "Call npm.load(config, cb) before using this command.\n"+
187       "See the README.md or cli.js for example usage.")
188     var a = npm.deref(c)
189     if (c === "la" || c === "ll") {
190       npm.config.set("long", true)
191     }
192
193     npm.command = c
194     if (commandCache[a]) return commandCache[a]
195
196     var cmd = require(__dirname+"/"+a+".js")
197
198     commandCache[a] = function () {
199       var args = Array.prototype.slice.call(arguments, 0)
200       if (typeof args[args.length - 1] !== "function") {
201         args.push(defaultCb)
202       }
203       if (args.length === 1) args.unshift([])
204
205       npm.registry.version = npm.version
206       if (!npm.registry.refer) {
207         npm.registry.refer = [a].concat(args[0]).map(function (arg) {
208           // exclude anything that might be a URL, path, or private module
209           // Those things will always have a slash in them somewhere
210           if (arg && arg.match && arg.match(/\/|\\/)) {
211             return "[REDACTED]"
212           } else {
213             return arg
214           }
215         }).filter(function (arg) {
216           return arg && arg.match
217         }).join(" ")
218       }
219
220       cmd.apply(npm, args)
221     }
222
223     Object.keys(cmd).forEach(function (k) {
224       commandCache[a][k] = cmd[k]
225     })
226
227     return commandCache[a]
228   }, enumerable: fullList.indexOf(c) !== -1, configurable: true })
229
230   // make css-case commands callable via camelCase as well
231   if (c.match(/\-([a-z])/)) {
232     addCommand(c.replace(/\-([a-z])/g, function (a, b) {
233       return b.toUpperCase()
234     }))
235   }
236 })
237
238 function defaultCb (er, data) {
239   if (er) console.error(er.stack || er.message)
240   else console.log(data)
241 }
242
243 npm.deref = function (c) {
244   if (!c) return ""
245   if (c.match(/[A-Z]/)) c = c.replace(/([A-Z])/g, function (m) {
246     return "-" + m.toLowerCase()
247   })
248   if (plumbing.indexOf(c) !== -1) return c
249   var a = abbrevs[c]
250   if (aliases[a]) a = aliases[a]
251   return a
252 }
253
254 var loaded = false
255   , loading = false
256   , loadErr = null
257   , loadListeners = []
258
259 function loadCb (er) {
260   loadListeners.forEach(function (cb) {
261     process.nextTick(cb.bind(npm, er, npm))
262   })
263   loadListeners.length = 0
264 }
265
266 npm.load = function (cli, cb_) {
267   if (!cb_ && typeof cli === "function") cb_ = cli , cli = {}
268   if (!cb_) cb_ = function () {}
269   if (!cli) cli = {}
270   loadListeners.push(cb_)
271   if (loaded || loadErr) return cb(loadErr)
272   if (loading) return
273   loading = true
274   var onload = true
275
276   function cb (er) {
277     if (loadErr) return
278     loadErr = er
279     if (er) return cb_(er)
280     if (npm.config.get("force")) {
281       log.warn("using --force", "I sure hope you know what you are doing.")
282     }
283     npm.config.loaded = true
284     loaded = true
285     loadCb(loadErr = er)
286     onload = onload && npm.config.get('onload-script')
287     if (onload) {
288       try {
289         require(onload)
290       } catch (err) {
291         log.warn('onload-script', 'failed to require onload script', onload)
292         log.warn('onload-script', err)
293       }
294       onload = false
295     }
296   }
297
298   log.pause()
299
300   load(npm, cli, cb)
301 }
302
303 function load (npm, cli, cb) {
304   which(process.argv[0], function (er, node) {
305     if (!er && node.toUpperCase() !== process.execPath.toUpperCase()) {
306       log.verbose("node symlink", node)
307       process.execPath = node
308       process.installPrefix = path.resolve(node, "..", "..")
309     }
310
311     // look up configs
312     //console.error("about to look up configs")
313
314     var builtin = path.resolve(__dirname, "..", "npmrc")
315     npmconf.load(cli, builtin, function (er, config) {
316       if (er === config) er = null
317
318       npm.config = config
319       if (er) return cb(er)
320
321       // if the "project" config is not a filename, and we're
322       // not in global mode, then that means that it collided
323       // with either the default or effective userland config
324       if (!config.get("global")
325           && config.sources.project
326           && config.sources.project.type !== "ini") {
327         log.verbose("config"
328                    , "Skipping project config: %s. "
329                    + "(matches userconfig)"
330                    , config.localPrefix + "/.npmrc")
331       }
332
333       // Include npm-version and node-version in user-agent
334       var ua = config.get("user-agent") || ""
335       ua = ua.replace(/\{node-version\}/gi, process.version)
336       ua = ua.replace(/\{npm-version\}/gi, npm.version)
337       ua = ua.replace(/\{platform\}/gi, process.platform)
338       ua = ua.replace(/\{arch\}/gi, process.arch)
339       config.set("user-agent", ua)
340
341       var color = config.get("color")
342
343       log.level = config.get("loglevel")
344       log.heading = config.get("heading") || "npm"
345       log.stream = config.get("logstream")
346
347       switch (color) {
348         case "always":
349           log.enableColor()
350           npm.color = true
351           break
352         case false:
353           log.disableColor()
354           npm.color = false
355           break
356         default:
357           var tty = require("tty")
358           if (process.stdout.isTTY) npm.color = true
359           else if (!tty.isatty) npm.color = true
360           else if (tty.isatty(1)) npm.color = true
361           else npm.color = false
362           break
363       }
364
365       log.resume()
366
367       // at this point the configs are all set.
368       // go ahead and spin up the registry client.
369       npm.registry = new CachingRegClient(npm.config)
370
371       var umask = npm.config.get("umask")
372       npm.modes = { exec: 0777 & (~umask)
373                   , file: 0666 & (~umask)
374                   , umask: umask }
375
376       var gp = Object.getOwnPropertyDescriptor(config, "globalPrefix")
377       Object.defineProperty(npm, "globalPrefix", gp)
378
379       var lp = Object.getOwnPropertyDescriptor(config, "localPrefix")
380       Object.defineProperty(npm, "localPrefix", lp)
381
382       return cb(null, npm)
383     })
384   })
385 }
386
387 Object.defineProperty(npm, "prefix",
388   { get : function () {
389       return npm.config.get("global") ? npm.globalPrefix : npm.localPrefix
390     }
391   , set : function (r) {
392       var k = npm.config.get("global") ? "globalPrefix" : "localPrefix"
393       return npm[k] = r
394     }
395   , enumerable : true
396   })
397
398 Object.defineProperty(npm, "bin",
399   { get : function () {
400       if (npm.config.get("global")) return npm.globalBin
401       return path.resolve(npm.root, ".bin")
402     }
403   , enumerable : true
404   })
405
406 Object.defineProperty(npm, "globalBin",
407   { get : function () {
408       var b = npm.globalPrefix
409       if (process.platform !== "win32") b = path.resolve(b, "bin")
410       return b
411     }
412   })
413
414 Object.defineProperty(npm, "dir",
415   { get : function () {
416       if (npm.config.get("global")) return npm.globalDir
417       return path.resolve(npm.prefix, "node_modules")
418     }
419   , enumerable : true
420   })
421
422 Object.defineProperty(npm, "globalDir",
423   { get : function () {
424       return (process.platform !== "win32")
425            ? path.resolve(npm.globalPrefix, "lib", "node_modules")
426            : path.resolve(npm.globalPrefix, "node_modules")
427     }
428   , enumerable : true
429   })
430
431 Object.defineProperty(npm, "root",
432   { get : function () { return npm.dir } })
433
434 Object.defineProperty(npm, "cache",
435   { get : function () { return npm.config.get("cache") }
436   , set : function (r) { return npm.config.set("cache", r) }
437   , enumerable : true
438   })
439
440 var tmpFolder
441 var rand = require("crypto").randomBytes(4).toString("hex")
442 Object.defineProperty(npm, "tmp",
443   { get : function () {
444       if (!tmpFolder) tmpFolder = "npm-" + process.pid + "-" + rand
445       return path.resolve(npm.config.get("tmp"), tmpFolder)
446     }
447   , enumerable : true
448   })
449
450 // the better to repl you with
451 Object.getOwnPropertyNames(npm.commands).forEach(function (n) {
452   if (npm.hasOwnProperty(n) || n === "config") return
453
454   Object.defineProperty(npm, n, { get: function () {
455     return function () {
456       var args = Array.prototype.slice.call(arguments, 0)
457         , cb = defaultCb
458
459       if (args.length === 1 && Array.isArray(args[0])) {
460         args = args[0]
461       }
462
463       if (typeof args[args.length - 1] === "function") {
464         cb = args.pop()
465       }
466
467       npm.commands[n](args, cb)
468     }
469   }, enumerable: false, configurable: true })
470 })
471
472 if (require.main === module) {
473   require("../bin/npm-cli.js")
474 }
475 })()