"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/npm/lib/npm.js" (11 Apr 2017, 11499 Bytes) of package /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 ;(function () {
    2   // windows: running 'npm blah' in this folder will invoke WSH, not node.
    3   /*globals WScript*/
    4   if (typeof WScript !== 'undefined') {
    5     WScript.echo(
    6       'npm does not work when run\n' +
    7       'with the Windows Scripting Host\n\n' +
    8       '"cd" to a different directory,\n' +
    9       'or type "npm.cmd <args>",\n' +
   10       'or type "node npm <args>".'
   11     )
   12     WScript.quit(1)
   13     return
   14   }
   15 
   16   var gfs = require('graceful-fs')
   17   // Patch the global fs module here at the app level
   18   var fs = gfs.gracefulify(require('fs'))
   19 
   20   var EventEmitter = require('events').EventEmitter
   21   var npm = module.exports = new EventEmitter()
   22   var npmconf = require('./config/core.js')
   23   var log = require('npmlog')
   24 
   25   var tty = require('tty')
   26   var path = require('path')
   27   var abbrev = require('abbrev')
   28   var which = require('which')
   29   var CachingRegClient = require('./cache/caching-client.js')
   30   var parseJSON = require('./utils/parse-json.js')
   31   var aliases = require('./config/cmd-list').aliases
   32   var cmdList = require('./config/cmd-list').cmdList
   33   var plumbing = require('./config/cmd-list').plumbing
   34   var output = require('./utils/output.js')
   35 
   36   npm.config = {
   37     loaded: false,
   38     get: function () {
   39       throw new Error('npm.load() required')
   40     },
   41     set: function () {
   42       throw new Error('npm.load() required')
   43     }
   44   }
   45 
   46   npm.commands = {}
   47 
   48   npm.rollbacks = []
   49 
   50   try {
   51     // startup, ok to do this synchronously
   52     var j = parseJSON(fs.readFileSync(
   53       path.join(__dirname, '../package.json')) + '')
   54     npm.version = j.version
   55   } catch (ex) {
   56     try {
   57       log.info('error reading version', ex)
   58     } catch (er) {}
   59     npm.version = ex
   60   }
   61 
   62   var commandCache = {}
   63   var aliasNames = Object.keys(aliases)
   64 
   65   var littleGuys = [ 'isntall', 'verison' ]
   66   var fullList = cmdList.concat(aliasNames).filter(function (c) {
   67     return plumbing.indexOf(c) === -1
   68   })
   69   var abbrevs = abbrev(fullList)
   70 
   71   // we have our reasons
   72   fullList = npm.fullList = fullList.filter(function (c) {
   73     return littleGuys.indexOf(c) === -1
   74   })
   75 
   76   Object.keys(abbrevs).concat(plumbing).forEach(function addCommand (c) {
   77     Object.defineProperty(npm.commands, c, { get: function () {
   78       if (!loaded) {
   79         throw new Error(
   80           'Call npm.load(config, cb) before using this command.\n' +
   81             'See the README.md or cli.js for example usage.'
   82         )
   83       }
   84       var a = npm.deref(c)
   85       if (c === 'la' || c === 'll') {
   86         npm.config.set('long', true)
   87       }
   88 
   89       npm.command = c
   90       if (commandCache[a]) return commandCache[a]
   91 
   92       var cmd = require(path.join(__dirname, a + '.js'))
   93 
   94       commandCache[a] = function () {
   95         var args = Array.prototype.slice.call(arguments, 0)
   96         if (typeof args[args.length - 1] !== 'function') {
   97           args.push(defaultCb)
   98         }
   99         if (args.length === 1) args.unshift([])
  100 
  101         // Options are prefixed by a hyphen-minus (-, \u2d).
  102         // Other dash-type chars look similar but are invalid.
  103         Array(args[0]).forEach(function (arg) {
  104           if (/^[\u2010-\u2015\u2212\uFE58\uFE63\uFF0D]/.test(arg)) {
  105             log.error('arg', 'Argument starts with non-ascii dash, this is probably invalid:', arg)
  106           }
  107         })
  108 
  109         npm.registry.version = npm.version
  110         if (!npm.registry.refer) {
  111           npm.registry.refer = [a].concat(args[0]).map(function (arg) {
  112             // exclude anything that might be a URL, path, or private module
  113             // Those things will always have a slash in them somewhere
  114             if (arg && arg.match && arg.match(/\/|\\/)) {
  115               return '[REDACTED]'
  116             } else {
  117               return arg
  118             }
  119           }).filter(function (arg) {
  120             return arg && arg.match
  121           }).join(' ')
  122         }
  123 
  124         cmd.apply(npm, args)
  125       }
  126 
  127       Object.keys(cmd).forEach(function (k) {
  128         commandCache[a][k] = cmd[k]
  129       })
  130 
  131       return commandCache[a]
  132     }, enumerable: fullList.indexOf(c) !== -1, configurable: true })
  133 
  134     // make css-case commands callable via camelCase as well
  135     if (c.match(/\-([a-z])/)) {
  136       addCommand(c.replace(/\-([a-z])/g, function (a, b) {
  137         return b.toUpperCase()
  138       }))
  139     }
  140   })
  141 
  142   function defaultCb (er, data) {
  143     log.disableProgress()
  144     if (er) console.error(er.stack || er.message)
  145     else output(data)
  146   }
  147 
  148   npm.deref = function (c) {
  149     if (!c) return ''
  150     if (c.match(/[A-Z]/)) {
  151       c = c.replace(/([A-Z])/g, function (m) {
  152         return '-' + m.toLowerCase()
  153       })
  154     }
  155     if (plumbing.indexOf(c) !== -1) return c
  156     var a = abbrevs[c]
  157     if (aliases[a]) a = aliases[a]
  158     return a
  159   }
  160 
  161   var loaded = false
  162   var loading = false
  163   var loadErr = null
  164   var loadListeners = []
  165 
  166   function loadCb (er) {
  167     loadListeners.forEach(function (cb) {
  168       process.nextTick(cb.bind(npm, er, npm))
  169     })
  170     loadListeners.length = 0
  171   }
  172 
  173   npm.load = function (cli, cb_) {
  174     if (!cb_ && typeof cli === 'function') {
  175       cb_ = cli
  176       cli = {}
  177     }
  178     if (!cb_) cb_ = function () {}
  179     if (!cli) cli = {}
  180     loadListeners.push(cb_)
  181     if (loaded || loadErr) return cb(loadErr)
  182     if (loading) return
  183     loading = true
  184     var onload = true
  185 
  186     function cb (er) {
  187       if (loadErr) return
  188       loadErr = er
  189       if (er) return cb_(er)
  190       if (npm.config.get('force')) {
  191         log.warn('using --force', 'I sure hope you know what you are doing.')
  192       }
  193       npm.config.loaded = true
  194       loaded = true
  195       loadCb(loadErr = er)
  196       onload = onload && npm.config.get('onload-script')
  197       if (onload) {
  198         try {
  199           require(onload)
  200         } catch (err) {
  201           log.warn('onload-script', 'failed to require onload script', onload)
  202           log.warn('onload-script', err)
  203         }
  204         onload = false
  205       }
  206     }
  207 
  208     log.pause()
  209 
  210     load(npm, cli, cb)
  211   }
  212 
  213   function load (npm, cli, cb) {
  214     which(process.argv[0], function (er, node) {
  215       if (!er && node.toUpperCase() !== process.execPath.toUpperCase()) {
  216         log.verbose('node symlink', node)
  217         process.execPath = node
  218         process.installPrefix = path.resolve(node, '..', '..')
  219       }
  220 
  221       // look up configs
  222       var builtin = path.resolve(__dirname, '..', 'npmrc')
  223       npmconf.load(cli, builtin, function (er, config) {
  224         if (er === config) er = null
  225 
  226         npm.config = config
  227         if (er) return cb(er)
  228 
  229         // if the 'project' config is not a filename, and we're
  230         // not in global mode, then that means that it collided
  231         // with either the default or effective userland config
  232         if (!config.get('global') &&
  233             config.sources.project &&
  234             config.sources.project.type !== 'ini') {
  235           log.verbose(
  236             'config',
  237             'Skipping project config: %s. (matches userconfig)',
  238             config.localPrefix + '/.npmrc'
  239           )
  240         }
  241 
  242         // Include npm-version and node-version in user-agent
  243         var ua = config.get('user-agent') || ''
  244         ua = ua.replace(/\{node-version\}/gi, process.version)
  245         ua = ua.replace(/\{npm-version\}/gi, npm.version)
  246         ua = ua.replace(/\{platform\}/gi, process.platform)
  247         ua = ua.replace(/\{arch\}/gi, process.arch)
  248         config.set('user-agent', ua)
  249 
  250         var color = config.get('color')
  251 
  252         log.level = config.get('loglevel')
  253         log.heading = config.get('heading') || 'npm'
  254         log.stream = config.get('logstream')
  255 
  256         switch (color) {
  257           case 'always':
  258             log.enableColor()
  259             npm.color = true
  260             break
  261           case false:
  262             log.disableColor()
  263             npm.color = false
  264             break
  265           default:
  266             if (process.stdout.isTTY) npm.color = true
  267             else if (!tty.isatty) npm.color = true
  268             else if (tty.isatty(1)) npm.color = true
  269             else npm.color = false
  270             break
  271         }
  272 
  273         if (config.get('unicode')) {
  274           log.enableUnicode()
  275         } else {
  276           log.disableUnicode()
  277         }
  278 
  279         if (config.get('progress') && (process.stderr.isTTY || (tty.isatty && tty.isatty(2)))) {
  280           log.enableProgress()
  281         } else {
  282           log.disableProgress()
  283         }
  284 
  285         log.resume()
  286 
  287         // at this point the configs are all set.
  288         // go ahead and spin up the registry client.
  289         npm.registry = new CachingRegClient(npm.config)
  290 
  291         var umask = npm.config.get('umask')
  292         npm.modes = {
  293           exec: parseInt('0777', 8) & (~umask),
  294           file: parseInt('0666', 8) & (~umask),
  295           umask: umask
  296         }
  297 
  298         var gp = Object.getOwnPropertyDescriptor(config, 'globalPrefix')
  299         Object.defineProperty(npm, 'globalPrefix', gp)
  300 
  301         var lp = Object.getOwnPropertyDescriptor(config, 'localPrefix')
  302         Object.defineProperty(npm, 'localPrefix', lp)
  303 
  304         return cb(null, npm)
  305       })
  306     })
  307   }
  308 
  309   Object.defineProperty(npm, 'prefix',
  310     {
  311       get: function () {
  312         return npm.config.get('global') ? npm.globalPrefix : npm.localPrefix
  313       },
  314       set: function (r) {
  315         var k = npm.config.get('global') ? 'globalPrefix' : 'localPrefix'
  316         npm[k] = r
  317         return r
  318       },
  319       enumerable: true
  320     })
  321 
  322   Object.defineProperty(npm, 'bin',
  323     {
  324       get: function () {
  325         if (npm.config.get('global')) return npm.globalBin
  326         return path.resolve(npm.root, '.bin')
  327       },
  328       enumerable: true
  329     })
  330 
  331   Object.defineProperty(npm, 'globalBin',
  332     {
  333       get: function () {
  334         var b = npm.globalPrefix
  335         if (process.platform !== 'win32') b = path.resolve(b, 'bin')
  336         return b
  337       }
  338     })
  339 
  340   Object.defineProperty(npm, 'dir',
  341     {
  342       get: function () {
  343         if (npm.config.get('global')) return npm.globalDir
  344         return path.resolve(npm.prefix, 'node_modules')
  345       },
  346       enumerable: true
  347     })
  348 
  349   Object.defineProperty(npm, 'globalDir',
  350     {
  351       get: function () {
  352         return (process.platform !== 'win32')
  353              ? path.resolve(npm.globalPrefix, 'lib', 'node_modules')
  354              : path.resolve(npm.globalPrefix, 'node_modules')
  355       },
  356       enumerable: true
  357     })
  358 
  359   Object.defineProperty(npm, 'root',
  360     { get: function () { return npm.dir } })
  361 
  362   Object.defineProperty(npm, 'cache',
  363     { get: function () { return npm.config.get('cache') },
  364       set: function (r) { return npm.config.set('cache', r) },
  365       enumerable: true
  366     })
  367 
  368   var tmpFolder
  369   var rand = require('crypto').randomBytes(4).toString('hex')
  370   Object.defineProperty(npm, 'tmp',
  371     {
  372       get: function () {
  373         if (!tmpFolder) tmpFolder = 'npm-' + process.pid + '-' + rand
  374         return path.resolve(npm.config.get('tmp'), tmpFolder)
  375       },
  376       enumerable: true
  377     })
  378 
  379   // the better to repl you with
  380   Object.getOwnPropertyNames(npm.commands).forEach(function (n) {
  381     if (npm.hasOwnProperty(n) || n === 'config') return
  382 
  383     Object.defineProperty(npm, n, { get: function () {
  384       return function () {
  385         var args = Array.prototype.slice.call(arguments, 0)
  386         var cb = defaultCb
  387 
  388         if (args.length === 1 && Array.isArray(args[0])) {
  389           args = args[0]
  390         }
  391 
  392         if (typeof args[args.length - 1] === 'function') {
  393           cb = args.pop()
  394         }
  395         npm.commands[n](args, cb)
  396       }
  397     }, enumerable: false, configurable: true })
  398   })
  399 
  400   if (require.main === module) {
  401     require('../bin/npm-cli.js')
  402   }
  403 })()