Skip to content

Commit 5cb9a1d

Browse files
committed
use @npmcli/config for configuration
This also refactors the completion.js command, still pending proper tests.
1 parent e929b28 commit 5cb9a1d

13 files changed

+501
-532
lines changed

bin/npx-cli.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const removed = new Set([
2424
...removedOpts
2525
])
2626

27-
const { types, shorthands } = require('../lib/config/defaults.js')
27+
const { types, shorthands } = require('../lib/utils/config.js')
2828
const npmSwitches = Object.entries(types)
2929
.filter(([key, type]) => type === Boolean ||
3030
(Array.isArray(type) && type.includes(Boolean)))

lib/adduser.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@ const getAuthType = ({ authType }) => {
4141
return type
4242
}
4343

44-
const saveConfig = () => new Promise((resolve, reject) => {
45-
npm.config.save('user', er => er ? reject(er) : resolve())
46-
})
47-
4844
const updateConfig = async ({ newCreds, registry, scope }) => {
4945
npm.config.del('_token', 'user') // prevent legacy pollution
5046

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

5551
npm.config.setCredentialsByURI(registry, newCreds)
56-
await saveConfig()
52+
await npm.config.save('user')
5753
}
5854

5955
const adduser = async (args) => {

lib/cli.js

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ module.exports = (process) => {
1717
checkForUnsupportedNode()
1818

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

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

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

32-
const conf = nopt(types, shorthands, process.argv)
33-
npm.argv = conf.argv.remain
34-
35-
if (conf.version) {
36-
console.log(npm.version)
37-
return errorHandler.exit(0)
38-
}
39-
40-
if (conf.versions) {
41-
npm.argv = ['version']
42-
conf.usage = false
43-
}
44-
4530
log.info('using', 'npm@%s', npm.version)
4631
log.info('using', 'node@%s', process.version)
4732

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

5136
// now actually fire up npm and run the command.
5237
// this is how to use npm programmatically:
53-
conf._exit = true
5438
const updateNotifier = require('../lib/utils/update-notifier.js')
55-
npm.load(conf, async er => {
39+
npm.load(async er => {
5640
if (er) return errorHandler(er)
41+
if (npm.config.get('version', 'cli')) {
42+
console.log(npm.version)
43+
return errorHandler.exit(0)
44+
}
45+
46+
if (npm.config.get('versions', 'cli')) {
47+
npm.argv = ['version']
48+
npm.config.set('usage', false, 'cli')
49+
}
5750

5851
npm.updateNotification = await updateNotifier(npm)
5952

0 commit comments

Comments
 (0)