"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/npm/lib/config.js" (11 Apr 2017, 8371 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 module.exports = config
    2 
    3 var log = require('npmlog')
    4 var npm = require('./npm.js')
    5 var npmconf = require('./config/core.js')
    6 var fs = require('graceful-fs')
    7 var writeFileAtomic = require('write-file-atomic')
    8 var types = npmconf.defs.types
    9 var ini = require('ini')
   10 var editor = require('editor')
   11 var os = require('os')
   12 var umask = require('./utils/umask')
   13 var usage = require('./utils/usage')
   14 var output = require('./utils/output')
   15 
   16 config.usage = usage(
   17   'config',
   18   'npm config set <key> <value>' +
   19   '\nnpm config get [<key>]' +
   20   '\nnpm config delete <key>' +
   21   '\nnpm config list' +
   22   '\nnpm config edit' +
   23   '\nnpm set <key> <value>' +
   24   '\nnpm get [<key>]'
   25 )
   26 config.completion = function (opts, cb) {
   27   var argv = opts.conf.argv.remain
   28   if (argv[1] !== 'config') argv.unshift('config')
   29   if (argv.length === 2) {
   30     var cmds = ['get', 'set', 'delete', 'ls', 'rm', 'edit']
   31     if (opts.partialWord !== 'l') cmds.push('list')
   32     return cb(null, cmds)
   33   }
   34 
   35   var action = argv[2]
   36   switch (action) {
   37     case 'set':
   38       // todo: complete with valid values, if possible.
   39       if (argv.length > 3) return cb(null, [])
   40       // fallthrough
   41       /*eslint no-fallthrough:0*/
   42     case 'get':
   43     case 'delete':
   44     case 'rm':
   45       return cb(null, Object.keys(types))
   46     case 'edit':
   47     case 'list': case 'ls':
   48       return cb(null, [])
   49     default: return cb(null, [])
   50   }
   51 }
   52 
   53 // npm config set key value
   54 // npm config get key
   55 // npm config list
   56 function config (args, cb) {
   57   var action = args.shift()
   58   switch (action) {
   59     case 'set': return set(args[0], args[1], cb)
   60     case 'get': return get(args[0], cb)
   61     case 'delete': case 'rm': case 'del': return del(args[0], cb)
   62     case 'list': case 'ls': return list(cb)
   63     case 'edit': return edit(cb)
   64     default: return unknown(action, cb)
   65   }
   66 }
   67 
   68 function edit (cb) {
   69   var e = npm.config.get('editor')
   70   var which = npm.config.get('global') ? 'global' : 'user'
   71   var f = npm.config.get(which + 'config')
   72   if (!e) return cb(new Error('No EDITOR config or environ set.'))
   73   npm.config.save(which, function (er) {
   74     if (er) return cb(er)
   75     fs.readFile(f, 'utf8', function (er, data) {
   76       if (er) data = ''
   77       data = [
   78         ';;;;',
   79         '; npm ' + (npm.config.get('global')
   80                   ? 'globalconfig' : 'userconfig') + ' file',
   81         '; this is a simple ini-formatted file',
   82         '; lines that start with semi-colons are comments.',
   83         '; read `npm help config` for help on the various options',
   84         ';;;;',
   85         '',
   86         data
   87       ].concat([
   88         ';;;;',
   89         '; all options with default values',
   90         ';;;;'
   91       ]).concat(Object.keys(npmconf.defaults).reduce(function (arr, key) {
   92         var obj = {}
   93         obj[key] = npmconf.defaults[key]
   94         if (key === 'logstream') return arr
   95         return arr.concat(
   96           ini.stringify(obj)
   97             .replace(/\n$/m, '')
   98             .replace(/^/g, '; ')
   99             .replace(/\n/g, '\n; ')
  100             .split('\n'))
  101       }, []))
  102       .concat([''])
  103       .join(os.EOL)
  104       writeFileAtomic(
  105         f,
  106         data,
  107         function (er) {
  108           if (er) return cb(er)
  109           editor(f, { editor: e }, cb)
  110         }
  111       )
  112     })
  113   })
  114 }
  115 
  116 function del (key, cb) {
  117   if (!key) return cb(new Error('no key provided'))
  118   var where = npm.config.get('global') ? 'global' : 'user'
  119   npm.config.del(key, where)
  120   npm.config.save(where, cb)
  121 }
  122 
  123 function set (key, val, cb) {
  124   if (key === undefined) {
  125     return unknown('', cb)
  126   }
  127   if (val === undefined) {
  128     if (key.indexOf('=') !== -1) {
  129       var k = key.split('=')
  130       key = k.shift()
  131       val = k.join('=')
  132     } else {
  133       val = ''
  134     }
  135   }
  136   key = key.trim()
  137   val = val.trim()
  138   log.info('config', 'set %j %j', key, val)
  139   var where = npm.config.get('global') ? 'global' : 'user'
  140   if (key.match(/umask/)) val = umask.fromString(val)
  141   npm.config.set(key, val, where)
  142   npm.config.save(where, cb)
  143 }
  144 
  145 function get (key, cb) {
  146   if (!key) return list(cb)
  147   if (!publicVar(key)) {
  148     return cb(new Error('---sekretz---'))
  149   }
  150   var val = npm.config.get(key)
  151   if (key.match(/umask/)) val = umask.toString(val)
  152   output(val)
  153   cb()
  154 }
  155 
  156 function sort (a, b) {
  157   return a > b ? 1 : -1
  158 }
  159 
  160 function publicVar (k) {
  161   return !(k.charAt(0) === '_' ||
  162            k.indexOf(':_') !== -1 ||
  163            types[k] !== types[k])
  164 }
  165 
  166 function getKeys (data) {
  167   return Object.keys(data).filter(publicVar).sort(sort)
  168 }
  169 
  170 function list (cb) {
  171   var msg = ''
  172   var long = npm.config.get('long')
  173 
  174   var cli = npm.config.sources.cli.data
  175   var cliKeys = getKeys(cli)
  176   if (cliKeys.length) {
  177     msg += '; cli configs\n'
  178     cliKeys.forEach(function (k) {
  179       if (cli[k] && typeof cli[k] === 'object') return
  180       if (k === 'argv') return
  181       msg += k + ' = ' + JSON.stringify(cli[k]) + '\n'
  182     })
  183     msg += '\n'
  184   }
  185 
  186   // env configs
  187   var env = npm.config.sources.env.data
  188   var envKeys = getKeys(env)
  189   if (envKeys.length) {
  190     msg += '; environment configs\n'
  191     envKeys.forEach(function (k) {
  192       if (env[k] !== npm.config.get(k)) {
  193         if (!long) return
  194         msg += '; ' + k + ' = ' +
  195           JSON.stringify(env[k]) + ' (overridden)\n'
  196       } else msg += k + ' = ' + JSON.stringify(env[k]) + '\n'
  197     })
  198     msg += '\n'
  199   }
  200 
  201   // project config file
  202   var project = npm.config.sources.project
  203   var pconf = project.data
  204   var ppath = project.path
  205   var pconfKeys = getKeys(pconf)
  206   if (pconfKeys.length) {
  207     msg += '; project config ' + ppath + '\n'
  208     pconfKeys.forEach(function (k) {
  209       var val = (k.charAt(0) === '_')
  210               ? '---sekretz---'
  211               : JSON.stringify(pconf[k])
  212       if (pconf[k] !== npm.config.get(k)) {
  213         if (!long) return
  214         msg += '; ' + k + ' = ' + val + ' (overridden)\n'
  215       } else msg += k + ' = ' + val + '\n'
  216     })
  217     msg += '\n'
  218   }
  219 
  220   // user config file
  221   var uconf = npm.config.sources.user.data
  222   var uconfKeys = getKeys(uconf)
  223   if (uconfKeys.length) {
  224     msg += '; userconfig ' + npm.config.get('userconfig') + '\n'
  225     uconfKeys.forEach(function (k) {
  226       var val = (k.charAt(0) === '_')
  227               ? '---sekretz---'
  228               : JSON.stringify(uconf[k])
  229       if (uconf[k] !== npm.config.get(k)) {
  230         if (!long) return
  231         msg += '; ' + k + ' = ' + val + ' (overridden)\n'
  232       } else msg += k + ' = ' + val + '\n'
  233     })
  234     msg += '\n'
  235   }
  236 
  237   // global config file
  238   var gconf = npm.config.sources.global.data
  239   var gconfKeys = getKeys(gconf)
  240   if (gconfKeys.length) {
  241     msg += '; globalconfig ' + npm.config.get('globalconfig') + '\n'
  242     gconfKeys.forEach(function (k) {
  243       var val = (k.charAt(0) === '_')
  244               ? '---sekretz---'
  245               : JSON.stringify(gconf[k])
  246       if (gconf[k] !== npm.config.get(k)) {
  247         if (!long) return
  248         msg += '; ' + k + ' = ' + val + ' (overridden)\n'
  249       } else msg += k + ' = ' + val + '\n'
  250     })
  251     msg += '\n'
  252   }
  253 
  254   // builtin config file
  255   var builtin = npm.config.sources.builtin || {}
  256   if (builtin && builtin.data) {
  257     var bconf = builtin.data
  258     var bpath = builtin.path
  259     var bconfKeys = getKeys(bconf)
  260     if (bconfKeys.length) {
  261       msg += '; builtin config ' + bpath + '\n'
  262       bconfKeys.forEach(function (k) {
  263         var val = (k.charAt(0) === '_')
  264                 ? '---sekretz---'
  265                 : JSON.stringify(bconf[k])
  266         if (bconf[k] !== npm.config.get(k)) {
  267           if (!long) return
  268           msg += '; ' + k + ' = ' + val + ' (overridden)\n'
  269         } else msg += k + ' = ' + val + '\n'
  270       })
  271       msg += '\n'
  272     }
  273   }
  274 
  275   // only show defaults if --long
  276   if (!long) {
  277     msg += '; node bin location = ' + process.execPath + '\n' +
  278            '; cwd = ' + process.cwd() + '\n' +
  279            '; HOME = ' + process.env.HOME + '\n' +
  280            '; "npm config ls -l" to show all defaults.\n'
  281 
  282     output(msg)
  283     return cb()
  284   }
  285 
  286   var defaults = npmconf.defaults
  287   var defKeys = getKeys(defaults)
  288   msg += '; default values\n'
  289   defKeys.forEach(function (k) {
  290     if (defaults[k] && typeof defaults[k] === 'object') return
  291     var val = JSON.stringify(defaults[k])
  292     if (defaults[k] !== npm.config.get(k)) {
  293       msg += '; ' + k + ' = ' + val + ' (overridden)\n'
  294     } else msg += k + ' = ' + val + '\n'
  295   })
  296   msg += '\n'
  297 
  298   output(msg)
  299   return cb()
  300 }
  301 
  302 function unknown (action, cb) {
  303   cb('Usage:\n' + config.usage)
  304 }