@@ -15,13 +15,13 @@ const ini = require('ini')
1515
1616const usage = usageUtil (
1717 'config' ,
18- 'npm config set <key> < value>' +
19- '\nnpm config get [<key>]' +
20- '\nnpm config delete <key>' +
18+ 'npm config set <key>=<value> [<key>=< value> ...] ' +
19+ '\nnpm config get [<key> [<key> ...] ]' +
20+ '\nnpm config delete <key> [<key> ...] ' +
2121 '\nnpm config list [--json]' +
2222 '\nnpm config edit' +
23- '\nnpm set <key> < value>' +
24- '\nnpm get [<key>]'
23+ '\nnpm set <key>=<value> [<key>=< value> ...] ' +
24+ '\nnpm get [<key> [<key> ...] ]'
2525)
2626
2727const cmd = ( args , cb ) => config ( args ) . then ( ( ) => cb ( ) ) . catch ( cb )
@@ -63,20 +63,20 @@ const completion = (opts, cb) => {
6363const UsageError = ( ) =>
6464 Object . assign ( new Error ( usage ) , { code : 'EUSAGE' } )
6565
66- const config = async ( [ action , key , val ] ) => {
66+ const config = async ( [ action , ... args ] ) => {
6767 npm . log . disableProgress ( )
6868 try {
6969 switch ( action ) {
7070 case 'set' :
71- await set ( key , val )
71+ await set ( args )
7272 break
7373 case 'get' :
74- await get ( key )
74+ await get ( args )
7575 break
7676 case 'delete' :
7777 case 'rm' :
7878 case 'del' :
79- await del ( key )
79+ await del ( args )
8080 break
8181 case 'list' :
8282 case 'ls' :
@@ -93,46 +93,58 @@ const config = async ([action, key, val]) => {
9393 }
9494}
9595
96- const set = async ( key , val ) => {
97- if ( key === undefined )
98- throw UsageError ( )
99-
100- if ( val === undefined ) {
101- if ( key . indexOf ( '=' ) !== - 1 ) {
102- const k = key . split ( '=' )
103- key = k . shift ( )
104- val = k . join ( '=' )
105- } else
106- val = ''
96+ // take an array of `[ key, value, k2=v2, k3, v3, ...]` and turn into
97+ // { key: value, k2: v2, k3: v3 }
98+ const keyValues = args => {
99+ const kv = { }
100+ for ( let i = 0 ; i < args . length ; i ++ ) {
101+ const arg = args [ i ] . split ( '=' )
102+ const key = arg . shift ( )
103+ const val = arg . length ? arg . join ( '=' )
104+ : i < args . length - 1 ? args [ ++ i ]
105+ : ''
106+ kv [ key . trim ( ) ] = val . trim ( )
107107 }
108+ return kv
109+ }
110+
111+ const set = async ( args ) => {
112+ if ( ! args . length )
113+ throw UsageError ( )
108114
109- key = key . trim ( )
110- val = val . trim ( )
111- npm . log . info ( 'config' , 'set %j %j' , key , val )
112115 const where = npm . flatOptions . global ? 'global' : 'user'
113- npm . config . set ( key , val , where )
114- if ( ! npm . config . validate ( where ) )
115- npm . log . warn ( 'config' , 'omitting invalid config values' )
116+ for ( const [ key , val ] of Object . entries ( keyValues ( args ) ) ) {
117+ npm . log . info ( 'config' , 'set %j %j' , key , val )
118+ npm . config . set ( key , val || '' , where )
119+ if ( ! npm . config . validate ( where ) )
120+ npm . log . warn ( 'config' , 'omitting invalid config values' )
121+ }
116122
117123 await npm . config . save ( where )
118124}
119125
120- const get = async key => {
121- if ( ! key )
126+ const get = async keys => {
127+ if ( ! keys . length )
122128 return list ( )
123129
124- if ( ! publicVar ( key ) )
125- throw `The ${ key } option is protected, and cannot be retrieved in this way`
130+ const out = [ ]
131+ for ( const key of keys ) {
132+ if ( ! publicVar ( key ) )
133+ throw `The ${ key } option is protected, and cannot be retrieved in this way`
126134
127- output ( npm . config . get ( key ) )
135+ const pref = keys . length > 1 ? `${ key } =` : ''
136+ out . push ( pref + npm . config . get ( key ) )
137+ }
138+ output ( out . join ( '\n' ) )
128139}
129140
130- const del = async key => {
131- if ( ! key )
141+ const del = async keys => {
142+ if ( ! keys . length )
132143 throw UsageError ( )
133144
134145 const where = npm . flatOptions . global ? 'global' : 'user'
135- npm . config . delete ( key , where )
146+ for ( const key of keys )
147+ npm . config . delete ( key , where )
136148 await npm . config . save ( where )
137149}
138150
0 commit comments