Skip to content

Commit

Permalink
use @npmcli/config for configuration
Browse files Browse the repository at this point in the history
This also refactors the completion.js command, still pending proper tests.
  • Loading branch information
isaacs committed Aug 18, 2020
1 parent e929b28 commit 5cb9a1d
Show file tree
Hide file tree
Showing 13 changed files with 501 additions and 532 deletions.
2 changes: 1 addition & 1 deletion bin/npx-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const removed = new Set([
...removedOpts
])

const { types, shorthands } = require('../lib/config/defaults.js')
const { types, shorthands } = require('../lib/utils/config.js')
const npmSwitches = Object.entries(types)
.filter(([key, type]) => type === Boolean ||
(Array.isArray(type) && type.includes(Boolean)))
Expand Down
6 changes: 1 addition & 5 deletions lib/adduser.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ const getAuthType = ({ authType }) => {
return type
}

const saveConfig = () => new Promise((resolve, reject) => {
npm.config.save('user', er => er ? reject(er) : resolve())
})

const updateConfig = async ({ newCreds, registry, scope }) => {
npm.config.del('_token', 'user') // prevent legacy pollution

Expand All @@ -53,7 +49,7 @@ const updateConfig = async ({ newCreds, registry, scope }) => {
}

npm.config.setCredentialsByURI(registry, newCreds)
await saveConfig()
await npm.config.save('user')
}

const adduser = async (args) => {
Expand Down
27 changes: 10 additions & 17 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ module.exports = (process) => {
checkForUnsupportedNode()

const npm = require('../lib/npm.js')
const { defs: { shorthands, types } } = require('../lib/config/core.js')
const errorHandler = require('../lib/utils/error-handler.js')
const nopt = require('nopt')

// if npm is called as "npmg" or "npm_g", then
// run in global mode.
Expand All @@ -29,19 +27,6 @@ module.exports = (process) => {

log.verbose('cli', process.argv)

const conf = nopt(types, shorthands, process.argv)
npm.argv = conf.argv.remain

if (conf.version) {
console.log(npm.version)
return errorHandler.exit(0)
}

if (conf.versions) {
npm.argv = ['version']
conf.usage = false
}

log.info('using', 'npm@%s', npm.version)
log.info('using', 'node@%s', process.version)

Expand All @@ -50,10 +35,18 @@ module.exports = (process) => {

// now actually fire up npm and run the command.
// this is how to use npm programmatically:
conf._exit = true
const updateNotifier = require('../lib/utils/update-notifier.js')
npm.load(conf, async er => {
npm.load(async er => {
if (er) return errorHandler(er)
if (npm.config.get('version', 'cli')) {
console.log(npm.version)
return errorHandler.exit(0)
}

if (npm.config.get('versions', 'cli')) {
npm.argv = ['version']
npm.config.set('usage', false, 'cli')
}

npm.updateNotification = await updateNotifier(npm)

Expand Down
Loading

0 comments on commit 5cb9a1d

Please sign in to comment.