@@ -39,7 +39,6 @@ const configNames = Object.keys(types)
3939const shorthandNames = Object . keys ( shorthands )
4040const allConfs = configNames . concat ( shorthandNames )
4141const isWindowsShell = require ( './utils/is-windows-shell.js' )
42- const output = require ( './utils/output.js' )
4342const fileExists = require ( './utils/file-exists.js' )
4443
4544const usageUtil = require ( './utils/usage.js' )
@@ -131,14 +130,14 @@ class Completion {
131130
132131 if ( partialWords . slice ( 0 , - 1 ) . indexOf ( '--' ) === - 1 ) {
133132 if ( word . charAt ( 0 ) === '-' )
134- return wrap ( opts , configCompl ( opts ) )
133+ return this . wrap ( opts , configCompl ( opts ) )
135134
136135 if ( words [ w - 1 ] &&
137136 words [ w - 1 ] . charAt ( 0 ) === '-' &&
138137 ! isFlag ( words [ w - 1 ] ) ) {
139138 // awaiting a value for a non-bool config.
140139 // don't even try to do this for now
141- return wrap ( opts , configValueCompl ( opts ) )
140+ return this . wrap ( opts , configValueCompl ( opts ) )
142141 }
143142 }
144143
@@ -152,7 +151,7 @@ class Completion {
152151 // check if there's a command already.
153152 const cmd = parsed . argv . remain [ 1 ]
154153 if ( ! cmd )
155- return wrap ( opts , cmdCompl ( opts ) )
154+ return this . wrap ( opts , cmdCompl ( opts ) )
156155
157156 Object . keys ( parsed ) . forEach ( k => this . npm . config . set ( k , parsed [ k ] ) )
158157
@@ -162,9 +161,29 @@ class Completion {
162161 const impl = this . npm . commands [ cmd ]
163162 if ( impl && impl . completion ) {
164163 const comps = await impl . completion ( opts )
165- return wrap ( opts , comps )
164+ return this . wrap ( opts , comps )
166165 }
167166 }
167+
168+ // The command should respond with an array. Loop over that,
169+ // wrapping quotes around any that have spaces, and writing
170+ // them to stdout.
171+ // If any of the items are arrays, then join them with a space.
172+ // Ie, returning ['a', 'b c', ['d', 'e']] would allow it to expand
173+ // to: 'a', 'b c', or 'd' 'e'
174+ wrap ( opts , compls ) {
175+ if ( ! Array . isArray ( compls ) )
176+ compls = compls ? [ compls ] : [ ]
177+
178+ compls = compls . map ( c =>
179+ Array . isArray ( c ) ? c . map ( escape ) . join ( ' ' ) : escape ( c ) )
180+
181+ if ( opts . partialWord )
182+ compls = compls . filter ( c => c . startsWith ( opts . partialWord ) )
183+
184+ if ( compls . length > 0 )
185+ this . npm . output ( compls . join ( '\n' ) )
186+ }
168187}
169188
170189const dumpScript = async ( ) => {
@@ -214,26 +233,6 @@ const unescape = w => w.charAt(0) === '\'' ? w.replace(/^'|'$/g, '')
214233const escape = w => ! / \s + / . test ( w ) ? w
215234 : '\'' + w + '\''
216235
217- // The command should respond with an array. Loop over that,
218- // wrapping quotes around any that have spaces, and writing
219- // them to stdout.
220- // If any of the items are arrays, then join them with a space.
221- // Ie, returning ['a', 'b c', ['d', 'e']] would allow it to expand
222- // to: 'a', 'b c', or 'd' 'e'
223- const wrap = ( opts , compls ) => {
224- if ( ! Array . isArray ( compls ) )
225- compls = compls ? [ compls ] : [ ]
226-
227- compls = compls . map ( c =>
228- Array . isArray ( c ) ? c . map ( escape ) . join ( ' ' ) : escape ( c ) )
229-
230- if ( opts . partialWord )
231- compls = compls . filter ( c => c . startsWith ( opts . partialWord ) )
232-
233- if ( compls . length > 0 )
234- output ( compls . join ( '\n' ) )
235- }
236-
237236// the current word has a dash. Return the config names,
238237// with the same number of dashes as the current word has.
239238const configCompl = opts => {
0 commit comments