Skip to content

Commit

Permalink
update dependencies, refactor config loading to async
Browse files Browse the repository at this point in the history
This removes a lot of very outdated dependencies, updates many to
their modern (usually promisified) versions, and updates (or removes)
code to account for the change.

Several dependencies have been completely removed, and others a bit
shuffled around, so that the node_modules folder can be bundled somewhat
more optimally than it would have otherwise.
  • Loading branch information
isaacs committed Jul 29, 2020
1 parent 73657ae commit e46400c
Show file tree
Hide file tree
Showing 711 changed files with 8,602 additions and 94,844 deletions.
1 change: 1 addition & 0 deletions bin/npm-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ npm.load(conf, function (er) {
// XXX move update notifier stuff into separate module
const pkg = require('../package.json')
let notifier = require('update-notifier')({pkg})
// XXX should use @npmcli/ci-detect
const isCI = require('ci-info').isCI
if (
notifier.update &&
Expand Down
33 changes: 16 additions & 17 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
/* eslint-disable standard/no-callback-literal */
module.exports = config

var log = require('npmlog')
var npm = require('./npm.js')
var npmconf = require('./config/core.js')
var fs = require('graceful-fs')
var writeFileAtomic = require('write-file-atomic')
var types = npmconf.defs.types
var ini = require('ini')
var editor = require('editor')
var os = require('os')
var path = require('path')
var mkdirp = require('gentle-fs').mkdir
var umask = require('./utils/umask')
var usage = require('./utils/usage')
var output = require('./utils/output')
var noProgressTillDone = require('./utils/no-progress-while-running').tillDone
const log = require('npmlog')
const npm = require('./npm.js')
const npmconf = require('./config/core.js')
const fs = require('graceful-fs')
const writeFileAtomic = require('write-file-atomic')
const types = npmconf.defs.types
const ini = require('ini')
const editor = require('editor')
const os = require('os')
const path = require('path')
const mkdirp = require('mkdirp-infer-owner')
const umask = require('./utils/umask')
const usage = require('./utils/usage')
const output = require('./utils/output')
const noProgressTillDone = require('./utils/no-progress-while-running').tillDone

config.usage = usage(
'config',
Expand Down Expand Up @@ -115,8 +115,7 @@ function edit (cb) {
}, []))
.concat([''])
.join(os.EOL)
mkdirp(path.dirname(f), function (er) {
if (er) return cb(er)
mkdirp(path.dirname(f)).catch(cb).then(() => {
writeFileAtomic(
f,
data,
Expand Down
187 changes: 68 additions & 119 deletions lib/config/core.js
Original file line number Diff line number Diff line change
@@ -1,112 +1,50 @@
var CC = require('config-chain').ConfigChain
var inherits = require('inherits')
var configDefs = require('./defaults.js')
var types = configDefs.types
var once = require('once')
var fs = require('fs')
var path = require('path')
var nopt = require('nopt')
var ini = require('ini')
var Umask = configDefs.Umask
var mkdirp = require('gentle-fs').mkdir
var umask = require('../utils/umask')
var isWindows = require('../utils/is-windows.js')

exports.load = load
exports.Conf = Conf
exports.loaded = false
exports.rootConf = null
exports.usingBuiltin = false
exports.defs = configDefs

Object.defineProperty(exports, 'defaults', { get: function () {
return configDefs.defaults
},
enumerable: true })

Object.defineProperty(exports, 'types', { get: function () {
return configDefs.types
},
enumerable: true })

exports.validate = validate

var myUid = process.getuid && process.getuid()
var myGid = process.getgid && process.getgid()

var loading = false
var loadCbs = []
function load () {
var cli, builtin, cb
for (var i = 0; i < arguments.length; i++) {
switch (typeof arguments[i]) {
case 'string': builtin = arguments[i]; break
case 'object': cli = arguments[i]; break
case 'function': cb = arguments[i]; break
}
}

if (!cb) cb = function () {}

if (exports.loaded) {
var ret = exports.loaded
if (cli) {
ret = new Conf(ret)
ret.unshift(cli)
}
return process.nextTick(cb.bind(null, null, ret))
}

// either a fresh object, or a clone of the passed in obj
if (!cli) {
cli = {}
} else {
cli = Object.keys(cli).reduce(function (c, k) {
c[k] = cli[k]
return c
}, {})
}

loadCbs.push(cb)
if (loading) return

loading = true

cb = once(function (er, conf) {
if (!er) {
exports.loaded = conf
loading = false
}
loadCbs.forEach(function (fn) {
fn(er, conf)
})
loadCbs.length = 0
})
const CC = require('config-chain').ConfigChain
const inherits = require('inherits')
const configDefs = require('./defaults.js')
const types = configDefs.types
const once = require('once')
const fs = require('fs')
const path = require('path')
const nopt = require('nopt')
const ini = require('ini')
const Umask = configDefs.Umask
const mkdirp = require('mkdirp-infer-owner')
const umask = require('../utils/umask')
const isWindows = require('../utils/is-windows.js')


const myUid = process.getuid && process.getuid()
const myGid = process.getgid && process.getgid()

const loadCbs = []

const { promisify } = require('util')
const load = async (cli, builtin) => {
// clone the cli object that's passed in, so we don't mutate it
cli = { ...cli }

// check for a builtin if provided.
exports.usingBuiltin = !!builtin
var rc = exports.rootConf = new Conf()
const rc = exports.rootConf = new Conf()
if (builtin) {
rc.addFile(builtin, 'builtin')
} else {
rc.add({}, 'builtin')
}

rc.on('load', function () {
load_(builtin, rc, cli, cb)
})
rc.on('error', cb)
return load_(builtin, rc, cli)
}

function load_ (builtin, rc, cli, cb) {
var defaults = configDefs.defaults
var conf = new Conf(rc)
// XXX promisify this the rest of the way, and all the Conf class methods
const load_ = promisify(function load_ (builtin, rc, cli, cb) {
const defaults = configDefs.defaults
const conf = new Conf(rc)

conf.usingBuiltin = !!builtin
conf.add(cli, 'cli')
conf.addEnv()

conf.loadPrefix(function (er) {
conf.loadPrefix(er => {
if (er) return cb(er)

// If you're doing `npm --userconfig=~/foo.npmrc` then you'd expect
Expand All @@ -125,9 +63,9 @@ function load_ (builtin, rc, cli, cb) {
// the default or resolved userconfig value. npm will log a "verbose"
// message about this when it happens, but it is a rare enough edge case
// that we don't have to be super concerned about it.
var projectConf = path.resolve(conf.localPrefix, '.npmrc')
var defaultUserConfig = rc.get('userconfig')
var resolvedUserConfig = conf.get('userconfig')
const projectConf = path.resolve(conf.localPrefix, '.npmrc')
const defaultUserConfig = rc.get('userconfig')
const resolvedUserConfig = conf.get('userconfig')
if (!conf.get('global') &&
projectConf !== defaultUserConfig &&
projectConf !== resolvedUserConfig) {
Expand All @@ -152,7 +90,7 @@ function load_ (builtin, rc, cli, cb) {
// return `~/local/etc/npmrc`
// annoying humans and their expectations!
if (conf.get('prefix')) {
var etc = path.resolve(conf.get('prefix'), 'etc')
const etc = path.resolve(conf.get('prefix'), 'etc')
defaults.globalconfig = path.resolve(etc, 'npmrc')
defaults.globalignorefile = path.resolve(etc, 'npmignore')
}
Expand All @@ -173,7 +111,7 @@ function load_ (builtin, rc, cli, cb) {
// warn about invalid bits.
validate(conf)

var cafile = conf.get('cafile')
const cafile = conf.get('cafile')

if (cafile) {
return conf.loadCAFile(cafile, finalize)
Expand All @@ -190,7 +128,7 @@ function load_ (builtin, rc, cli, cb) {
exports.loaded = conf
cb(er, conf)
}
}
})

// Basically the same as CC, but:
// 1. Always ini
Expand Down Expand Up @@ -225,40 +163,39 @@ Conf.prototype.loadExtras = function (cb) {
this.setUser(function (er) {
if (er) return cb(er)
// Without prefix, nothing will ever work
mkdirp(this.prefix, cb)
mkdirp(this.prefix).then(() => cb()).catch(cb)
}.bind(this))
}

Conf.prototype.save = function (where, cb) {
var target = this.sources[where]
const target = this.sources[where]
if (!target || !(target.path || target.source) || !target.data) {
var er
let er
if (where !== 'builtin') er = new Error('bad save target: ' + where)
if (cb) {
process.nextTick(cb.bind(null, er))
process.nextTick(() => cb(er))
return this
}
return this.emit('error', er)
}

if (target.source) {
var pref = target.prefix || ''
const pref = target.prefix || ''
Object.keys(target.data).forEach(function (k) {
target.source[pref + k] = target.data[k]
})
if (cb) process.nextTick(cb)
return this
}

var data = ini.stringify(target.data)
const data = ini.stringify(target.data)

var then = function then (er) {
const then = er => {
if (er) return done(er)

fs.chmod(target.path, mode, done)
}

var done = function done (er) {
const done = er => {
if (er) {
if (cb) return cb(er)
else return this.emit('error', er)
Expand All @@ -270,22 +207,15 @@ Conf.prototype.save = function (where, cb) {
}
}

then = then.bind(this)
done = done.bind(this)
this._saving++

var mode = where === 'user' ? '0600' : '0666'
const mode = where === 'user' ? 0o600 : 0o666
if (!data.trim()) {
fs.unlink(target.path, function () {
// ignore the possible error (e.g. the file doesn't exist)
done(null)
})
// ignore the possible error (e.g. the file doesn't exist)
fs.unlink(target.path, () => done())
} else {
// we don't have to use inferOwner here, because gentle-fs will
// mkdir with the correctly inferred ownership. Just preserve it.
const dir = path.dirname(target.path)
mkdirp(dir, function (er) {
if (er) return then(er)
mkdirp(dir).catch(then).then(() => {
fs.stat(dir, (er, st) => {
if (er) return then(er)
fs.writeFile(target.path, data, 'utf8', function (er) {
Expand Down Expand Up @@ -427,3 +357,22 @@ function validate (cl) {

nopt.clean(cl.root, configDefs.types)
}

exports.load = load
exports.Conf = Conf
exports.loaded = false
exports.rootConf = null
exports.usingBuiltin = false
exports.defs = configDefs

Object.defineProperty(exports, 'defaults', { get: function () {
return configDefs.defaults
},
enumerable: true })

Object.defineProperty(exports, 'types', { get: function () {
return configDefs.types
},
enumerable: true })

exports.validate = validate
13 changes: 6 additions & 7 deletions lib/config/set-user.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module.exports = setUser

var assert = require('assert')
var path = require('path')
var fs = require('fs')
var mkdirp = require('gentle-fs').mkdir
const assert = require('assert')
const path = require('path')
const fs = require('fs')
const mkdirp = require('mkdirp-infer-owner')

function setUser (cb) {
var defaultConf = this.root
Expand All @@ -18,9 +18,8 @@ function setUser (cb) {
return cb()
}

var prefix = path.resolve(this.get('prefix'))
mkdirp(prefix, function (er) {
if (er) return cb(er)
const prefix = path.resolve(this.get('prefix'))
mkdirp(prefix).catch(cb).then(() => {
fs.stat(prefix, function (er, st) {
defaultConf.user = st && st.uid
return cb(er)
Expand Down
Loading

0 comments on commit e46400c

Please sign in to comment.