diff options
author | isaacs <i@izs.me> | 2020-12-16 03:32:32 +0300 |
---|---|---|
committer | isaacs <i@izs.me> | 2020-12-18 22:38:41 +0300 |
commit | a9b8bf2634c627fbb16ca3a6bb2c2f1058c3e586 (patch) | |
tree | 8e51be0a095d76b489a2af43f1d8cc781699be40 /lib | |
parent | a92d310b7e9e4c48b08f52785c2e3a6d52a82ad7 (diff) |
Support multiple set/get/deletes in npm configisaacs/config-set-multi
While digging into #2300, I realized it would be a lot easier if we
could do this:
npm config set email=me@example.com _auth=xxxx
and avoid the whole issue of what gets set first.
Also, why not let `npm config get foo bar baz` return just the keys
specified?
Also updates the docs, including the statement that `npm config set foo`
with no value sets it to `true`, when as far as I can tell, that has
never been the case.
PR-URL: https://github.com/npm/cli/pull/2362
Credit: @isaacs
Close: #2362
Reviewed-by: @nlf
Diffstat (limited to 'lib')
-rw-r--r-- | lib/config.js | 80 | ||||
-rw-r--r-- | lib/get.js | 2 | ||||
-rw-r--r-- | lib/set.js | 2 |
3 files changed, 48 insertions, 36 deletions
diff --git a/lib/config.js b/lib/config.js index 561c31e0b..b32cf3359 100644 --- a/lib/config.js +++ b/lib/config.js @@ -15,13 +15,13 @@ const ini = require('ini') const usage = usageUtil( 'config', - 'npm config set <key> <value>' + - '\nnpm config get [<key>]' + - '\nnpm config delete <key>' + + 'npm config set <key>=<value> [<key>=<value> ...]' + + '\nnpm config get [<key> [<key> ...]]' + + '\nnpm config delete <key> [<key> ...]' + '\nnpm config list [--json]' + '\nnpm config edit' + - '\nnpm set <key> <value>' + - '\nnpm get [<key>]' + '\nnpm set <key>=<value> [<key>=<value> ...]' + + '\nnpm get [<key> [<key> ...]]' ) const cmd = (args, cb) => config(args).then(() => cb()).catch(cb) @@ -63,20 +63,20 @@ const completion = (opts, cb) => { const UsageError = () => Object.assign(new Error(usage), { code: 'EUSAGE' }) -const config = async ([action, key, val]) => { +const config = async ([action, ...args]) => { npm.log.disableProgress() try { switch (action) { case 'set': - await set(key, val) + await set(args) break case 'get': - await get(key) + await get(args) break case 'delete': case 'rm': case 'del': - await del(key) + await del(args) break case 'list': case 'ls': @@ -93,46 +93,58 @@ const config = async ([action, key, val]) => { } } -const set = async (key, val) => { - if (key === undefined) - throw UsageError() - - if (val === undefined) { - if (key.indexOf('=') !== -1) { - const k = key.split('=') - key = k.shift() - val = k.join('=') - } else - val = '' +// take an array of `[key, value, k2=v2, k3, v3, ...]` and turn into +// { key: value, k2: v2, k3: v3 } +const keyValues = args => { + const kv = {} + for (let i = 0; i < args.length; i++) { + const arg = args[i].split('=') + const key = arg.shift() + const val = arg.length ? arg.join('=') + : i < args.length - 1 ? args[++i] + : '' + kv[key.trim()] = val.trim() } + return kv +} + +const set = async (args) => { + if (!args.length) + throw UsageError() - key = key.trim() - val = val.trim() - npm.log.info('config', 'set %j %j', key, val) const where = npm.flatOptions.global ? 'global' : 'user' - npm.config.set(key, val, where) - if (!npm.config.validate(where)) - npm.log.warn('config', 'omitting invalid config values') + for (const [key, val] of Object.entries(keyValues(args))) { + npm.log.info('config', 'set %j %j', key, val) + npm.config.set(key, val || '', where) + if (!npm.config.validate(where)) + npm.log.warn('config', 'omitting invalid config values') + } await npm.config.save(where) } -const get = async key => { - if (!key) +const get = async keys => { + if (!keys.length) return list() - if (!publicVar(key)) - throw `The ${key} option is protected, and cannot be retrieved in this way` + const out = [] + for (const key of keys) { + if (!publicVar(key)) + throw `The ${key} option is protected, and cannot be retrieved in this way` - output(npm.config.get(key)) + const pref = keys.length > 1 ? `${key}=` : '' + out.push(pref + npm.config.get(key)) + } + output(out.join('\n')) } -const del = async key => { - if (!key) +const del = async keys => { + if (!keys.length) throw UsageError() const where = npm.flatOptions.global ? 'global' : 'user' - npm.config.delete(key, where) + for (const key of keys) + npm.config.delete(key, where) await npm.config.save(where) } diff --git a/lib/get.js b/lib/get.js index bccdc7212..ab2141e35 100644 --- a/lib/get.js +++ b/lib/get.js @@ -3,7 +3,7 @@ const usageUtil = require('./utils/usage.js') const usage = usageUtil( 'get', - 'npm get <key> <value> (See `npm config`)' + 'npm get [<key> ...] (See `npm config`)' ) const completion = npm.commands.config.completion diff --git a/lib/set.js b/lib/set.js index 62860e53f..fd6076293 100644 --- a/lib/set.js +++ b/lib/set.js @@ -1,7 +1,7 @@ module.exports = set -set.usage = 'npm set <key> <value> (See `npm config`)' +set.usage = 'npm set <key>=<value> [<key>=<value> ...] (See `npm config`)' var npm = require('./npm.js') |