@@ -3,13 +3,13 @@ const { defaults, types } = require('./utils/config.js')
33const usageUtil = require ( './utils/usage.js' )
44const output = require ( './utils/output.js' )
55
6- const editor = require ( 'editor' )
76const mkdirp = require ( 'mkdirp-infer-owner' )
87const { dirname } = require ( 'path' )
98const { promisify } = require ( 'util' )
109const fs = require ( 'fs' )
1110const readFile = promisify ( fs . readFile )
1211const writeFile = promisify ( fs . writeFile )
12+ const editor = promisify ( require ( 'editor' ) )
1313const { EOL } = require ( 'os' )
1414const ini = require ( 'ini' )
1515
@@ -28,18 +28,25 @@ const cmd = (args, cb) => config(args).then(() => cb()).catch(cb)
2828
2929const completion = ( opts , cb ) => {
3030 const argv = opts . conf . argv . remain
31- if ( argv [ 1 ] !== 'config' ) argv . unshift ( 'config' )
31+ if ( argv [ 1 ] !== 'config' ) {
32+ argv . unshift ( 'config' )
33+ }
34+
3235 if ( argv . length === 2 ) {
3336 const cmds = [ 'get' , 'set' , 'delete' , 'ls' , 'rm' , 'edit' ]
34- if ( opts . partialWord !== 'l' ) cmds . push ( 'list' )
37+ if ( opts . partialWord !== 'l' ) {
38+ cmds . push ( 'list' )
39+ }
3540 return cb ( null , cmds )
3641 }
3742
3843 const action = argv [ 2 ]
3944 switch ( action ) {
4045 case 'set' :
4146 // todo: complete with valid values, if possible.
42- if ( argv . length > 3 ) return cb ( null , [ ] )
47+ if ( argv . length > 3 ) {
48+ return cb ( null , [ ] )
49+ }
4350 // fallthrough
4451 /* eslint no-fallthrough:0 */
4552 case 'get' :
@@ -49,7 +56,6 @@ const completion = (opts, cb) => {
4956 case 'edit' :
5057 case 'list' :
5158 case 'ls' :
52- return cb ( null , [ ] )
5359 default :
5460 return cb ( null , [ ] )
5561 }
@@ -72,7 +78,7 @@ const config = async ([action, key, val]) => {
7278 break
7379 case 'list' :
7480 case 'ls' :
75- await ( npm . config . get ( ' json' ) ? listJson ( ) : list ( ) )
81+ await ( npm . flatOptions . json ? listJson ( ) : list ( ) )
7682 break
7783 case 'edit' :
7884 await edit ( )
@@ -103,9 +109,7 @@ const set = async (key, val) => {
103109 key = key . trim ( )
104110 val = val . trim ( )
105111 npm . log . info ( 'config' , 'set %j %j' , key , val )
106- const where = npm . config . get ( 'global' ) ? 'global' : 'user'
107- const validBefore = npm . config . data . get ( where ) . valid
108- console . error ( 'validBefore?' , validBefore )
112+ const where = npm . flatOptions . global ? 'global' : 'user'
109113 npm . config . set ( key , val , where )
110114 if ( ! npm . config . validate ( where ) ) {
111115 npm . log . warn ( 'config' , 'omitting invalid config values' )
@@ -130,15 +134,15 @@ const del = async key => {
130134 throw usage
131135 }
132136
133- const where = npm . config . get ( ' global' ) ? 'global' : 'user'
137+ const where = npm . flatOptions . global ? 'global' : 'user'
134138 npm . config . delete ( key , where )
135139 await npm . config . save ( where )
136140}
137141
138142const edit = async ( ) => {
139143 const { editor : e , global } = npm . flatOptions
140144 if ( ! e ) {
141- throw new Error ( 'No `editor` config or EDITOR envionment variable set' )
145+ throw new Error ( 'No `editor` config or EDITOR environment variable set' )
142146 }
143147
144148 const where = global ? 'global' : 'user'
@@ -147,7 +151,10 @@ const edit = async () => {
147151 // save first, just to make sure it's synced up
148152 // this also removes all the comments from the last time we edited it.
149153 await npm . config . save ( where )
150- const data = ( await readFile ( file , 'utf8' ) . catch ( ( ) => '' ) ) . replace ( / \r \n / g, '\n' )
154+
155+ const data = (
156+ await readFile ( file , 'utf8' ) . catch ( ( ) => '' )
157+ ) . replace ( / \r \n / g, '\n' )
151158 const defData = Object . entries ( defaults ) . reduce ( ( str , [ key , val ] ) => {
152159 const obj = { [ key ] : val }
153160 const i = ini . stringify ( obj )
@@ -179,9 +186,7 @@ ${defData}
179186` . split ( '\n' ) . join ( EOL )
180187 await mkdirp ( dirname ( file ) )
181188 await writeFile ( file , tmpData , 'utf8' )
182- await new Promise ( ( res , rej ) => {
183- editor ( file , { editor : e } , ( er ) => er ? rej ( er ) : res ( ) )
184- } )
189+ await editor ( file , { editor : e } )
185190}
186191
187192const publicVar = k => ! / ^ ( \/ \/ [ ^ : ] + : ) ? _ / . test ( k )
0 commit comments