2828// one per line for the shell completion method to consume in IFS=$'\n' mode
2929// as an array.
3030//
31- // TODO: make all the implementation completion methods promise-returning
32- // instead of callback-taking.
3331
3432const npm = require ( './npm.js' )
3533const { types, shorthands } = require ( './utils/config.js' )
@@ -52,9 +50,9 @@ const { promisify } = require('util')
5250const cmd = ( args , cb ) => compl ( args ) . then ( ( ) => cb ( ) ) . catch ( cb )
5351
5452// completion for the completion command
55- const completion = async ( opts , cb ) => {
53+ const completion = async ( opts ) => {
5654 if ( opts . w > 2 )
57- return cb ( )
55+ return
5856
5957 const { resolve } = require ( 'path' )
6058 const [ bashExists , zshExists ] = await Promise . all ( [
@@ -68,7 +66,7 @@ const completion = async (opts, cb) => {
6866 if ( bashExists )
6967 out . push ( [ '>>' , '~/.bashrc' ] )
7068
71- cb ( null , out )
69+ return out
7270}
7371
7472const compl = async args => {
@@ -121,18 +119,16 @@ const compl = async args => {
121119 raw : args ,
122120 }
123121
124- const wrap = getWrap ( opts )
125-
126122 if ( partialWords . slice ( 0 , - 1 ) . indexOf ( '--' ) === - 1 ) {
127123 if ( word . charAt ( 0 ) === '-' )
128- return wrap ( configCompl ( opts ) )
124+ return wrap ( opts , configCompl ( opts ) )
129125
130126 if ( words [ w - 1 ] &&
131127 words [ w - 1 ] . charAt ( 0 ) === '-' &&
132128 ! isFlag ( words [ w - 1 ] ) ) {
133129 // awaiting a value for a non-bool config.
134130 // don't even try to do this for now
135- return wrap ( configValueCompl ( opts ) )
131+ return wrap ( opts , configValueCompl ( opts ) )
136132 }
137133 }
138134
@@ -146,7 +142,7 @@ const compl = async args => {
146142 // check if there's a command already.
147143 const cmd = parsed . argv . remain [ 1 ]
148144 if ( ! cmd )
149- return wrap ( cmdCompl ( opts ) )
145+ return wrap ( opts , cmdCompl ( opts ) )
150146
151147 Object . keys ( parsed ) . forEach ( k => npm . config . set ( k , parsed [ k ] ) )
152148
@@ -155,10 +151,8 @@ const compl = async args => {
155151 // otherwise, do nothing
156152 const impl = npm . commands [ cmd ]
157153 if ( impl && impl . completion ) {
158- // XXX promisify all the cmd.completion functions
159- return await new Promise ( ( res , rej ) => {
160- impl . completion ( opts , ( er , comps ) => er ? rej ( er ) : res ( wrap ( comps ) ) )
161- } )
154+ const comps = await impl . completion ( opts )
155+ return wrap ( opts , comps )
162156 }
163157}
164158
@@ -215,7 +209,7 @@ const escape = w => !/\s+/.test(w) ? w
215209// If any of the items are arrays, then join them with a space.
216210// Ie, returning ['a', 'b c', ['d', 'e']] would allow it to expand
217211// to: 'a', 'b c', or 'd' 'e'
218- const getWrap = opts => compls => {
212+ const wrap = ( opts , compls ) => {
219213 if ( ! Array . isArray ( compls ) )
220214 compls = compls ? [ compls ] : [ ]
221215
0 commit comments