@@ -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,12 +56,14 @@ const completion = (opts, cb) => {
4956 case 'edit' :
5057 case 'list' :
5158 case 'ls' :
52- return cb ( null , [ ] )
5359 default :
5460 return cb ( null , [ ] )
5561 }
5662}
5763
64+ const UsageError = ( ) =>
65+ Object . assign ( new Error ( usage ) , { code : 'EUSAGE' } )
66+
5867const config = async ( [ action , key , val ] ) => {
5968 npm . log . disableProgress ( )
6069 try {
@@ -72,13 +81,13 @@ const config = async ([action, key, val]) => {
7281 break
7382 case 'list' :
7483 case 'ls' :
75- await ( npm . config . get ( ' json' ) ? listJson ( ) : list ( ) )
84+ await ( npm . flatOptions . json ? listJson ( ) : list ( ) )
7685 break
7786 case 'edit' :
7887 await edit ( )
7988 break
8089 default :
81- throw usage
90+ throw UsageError ( )
8291 }
8392 } finally {
8493 npm . log . enableProgress ( )
@@ -87,7 +96,7 @@ const config = async ([action, key, val]) => {
8796
8897const set = async ( key , val ) => {
8998 if ( key === undefined ) {
90- throw usage
99+ throw UsageError ( )
91100 }
92101
93102 if ( val === undefined ) {
@@ -103,9 +112,7 @@ const set = async (key, val) => {
103112 key = key . trim ( )
104113 val = val . trim ( )
105114 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 )
115+ const where = npm . flatOptions . global ? 'global' : 'user'
109116 npm . config . set ( key , val , where )
110117 if ( ! npm . config . validate ( where ) ) {
111118 npm . log . warn ( 'config' , 'omitting invalid config values' )
@@ -127,18 +134,18 @@ const get = async key => {
127134
128135const del = async key => {
129136 if ( ! key ) {
130- throw usage
137+ throw UsageError ( )
131138 }
132139
133- const where = npm . config . get ( ' global' ) ? 'global' : 'user'
140+ const where = npm . flatOptions . global ? 'global' : 'user'
134141 npm . config . delete ( key , where )
135142 await npm . config . save ( where )
136143}
137144
138145const edit = async ( ) => {
139146 const { editor : e , global } = npm . flatOptions
140147 if ( ! e ) {
141- throw new Error ( 'No `editor` config or EDITOR envionment variable set' )
148+ throw new Error ( 'No `editor` config or EDITOR environment variable set' )
142149 }
143150
144151 const where = global ? 'global' : 'user'
@@ -147,10 +154,14 @@ const edit = async () => {
147154 // save first, just to make sure it's synced up
148155 // this also removes all the comments from the last time we edited it.
149156 await npm . config . save ( where )
150- const data = ( await readFile ( file , 'utf8' ) . catch ( ( ) => '' ) ) . replace ( / \r \n / g, '\n' )
157+
158+ const data = (
159+ await readFile ( file , 'utf8' ) . catch ( ( ) => '' )
160+ ) . replace ( / \r \n / g, '\n' )
151161 const defData = Object . entries ( defaults ) . reduce ( ( str , [ key , val ] ) => {
152162 const obj = { [ key ] : val }
153163 const i = ini . stringify ( obj )
164+ . replace ( / \r \n / g, '\n' ) // normalizes output from ini.stringify
154165 . replace ( / \n $ / m, '' )
155166 . replace ( / ^ / g, '; ' )
156167 . replace ( / \n / g, '\n; ' )
@@ -179,9 +190,7 @@ ${defData}
179190` . split ( '\n' ) . join ( EOL )
180191 await mkdirp ( dirname ( file ) )
181192 await writeFile ( file , tmpData , 'utf8' )
182- await new Promise ( ( res , rej ) => {
183- editor ( file , { editor : e } , ( er ) => er ? rej ( er ) : res ( ) )
184- } )
193+ await editor ( file , { editor : e } )
185194}
186195
187196const publicVar = k => ! / ^ ( \/ \/ [ ^ : ] + : ) ? _ / . test ( k )
0 commit comments