@@ -11,9 +11,22 @@ import {
1111 ShellCompDirective ,
1212} from "./shared" ;
1313
14+ function quoteIfNeeded ( path : string ) : string {
15+ return path . includes ( " " ) ? `'${ path } '` : path ;
16+ }
17+
1418const execPath = process . execPath ;
1519const processArgs = process . argv . slice ( 1 ) ;
16- const x = `${ execPath } ${ process . execArgv . join ( " " ) } ${ processArgs [ 0 ] } ` ;
20+
21+ // Apply the quoting function to each part of x
22+ // This ensures that paths like "Program Files" are quoted for PowerShell execution.
23+ const quotedExecPath = quoteIfNeeded ( execPath ) ;
24+ const quotedProcessArgs = processArgs . map ( quoteIfNeeded ) ;
25+ const quotedProcessExecArgs = process . execArgv . map ( quoteIfNeeded ) ;
26+
27+ const x = `${ quotedExecPath } ${ quotedProcessExecArgs . join ( " " ) } ${
28+ quotedProcessArgs [ 0 ]
29+ } `;
1730
1831export default function tab ( instance : CAC ) : void {
1932 instance . command ( "complete [shell]" ) . action ( async ( shell , extra ) => {
@@ -75,7 +88,7 @@ export default function tab(instance: CAC): void {
7588
7689 function processOption ( ) {
7790 const matchedOption = options . find ( ( o ) =>
78- o . names . some ( ( name ) => name === flagName ) ,
91+ o . names . some ( ( name ) => name === flagName )
7992 ) ;
8093
8194 if ( matchedOption && ! matchedOption . isBoolean ) {
@@ -120,21 +133,27 @@ export default function tab(instance: CAC): void {
120133 } else if ( lastArg ?. startsWith ( "--" ) && ! endsWithSpace ) {
121134 flagName = lastArg . slice ( 2 ) ;
122135 processOption ( ) ;
123- } else if ( lastArg ?. startsWith ( "-" ) && lastArg . length > 1 && ! endsWithSpace ) {
136+ } else if (
137+ lastArg ?. startsWith ( "-" ) &&
138+ lastArg . length > 1 &&
139+ ! endsWithSpace
140+ ) {
124141 flagName = lastArg . slice ( 2 ) ;
125142 processOption ( ) ;
126143 }
127144
128145 if ( isCompletingFlagValue ) {
129146 const flagCompletionFn = flagMap . get (
130- `${ command . name } ${ option ?. name } ` ,
147+ `${ command . name } ${ option ?. name } `
131148 ) ;
132149
133150 if ( flagCompletionFn ) {
134151 // Call custom completion function for the flag
135152 const comps = await flagCompletionFn ( previousArgs , toComplete ) ;
136153 completions . push (
137- ...comps . map ( ( comp ) => `${ comp . action } \t${ comp . description ?? '' } ` ) ,
154+ ...comps . map (
155+ ( comp ) => `${ comp . action } \t${ comp . description ?? "" } `
156+ )
138157 ) ;
139158 directive = ShellCompDirective . ShellCompDirectiveNoFileComp ;
140159 } else {
@@ -164,7 +183,9 @@ export default function tab(instance: CAC): void {
164183
165184 if ( optionsToSuggest . length > 0 ) {
166185 completions . push (
167- ...optionsToSuggest . map ( ( o ) => `--${ o . name } \t${ o . description ?? '' } ` ) ,
186+ ...optionsToSuggest . map (
187+ ( o ) => `--${ o . name } \t${ o . description ?? "" } `
188+ )
168189 ) ;
169190 }
170191
@@ -174,7 +195,7 @@ export default function tab(instance: CAC): void {
174195 [ execPath , processArgs [ 0 ] , ...previousArgs , toComplete ] ,
175196 {
176197 run : false ,
177- } ,
198+ }
178199 ) ;
179200 const fullCommandName = args
180201 . filter ( ( arg ) => ! arg . startsWith ( "-" ) )
@@ -225,12 +246,12 @@ export default function tab(instance: CAC): void {
225246 if ( part . value . variadic ) {
226247 const comps = await positional . completion (
227248 previousArgs ,
228- toComplete ,
249+ toComplete
229250 ) ;
230251 completions . push (
231252 ...comps . map (
232- ( comp ) => `${ comp . action } \t${ comp . description ?? "" } ` ,
233- ) ,
253+ ( comp ) => `${ comp . action } \t${ comp . description ?? "" } `
254+ )
234255 ) ;
235256 break ;
236257 }
@@ -240,12 +261,12 @@ export default function tab(instance: CAC): void {
240261 // User is still typing this positional argument, provide completions
241262 const comps = await positional . completion (
242263 previousArgs ,
243- toComplete ,
264+ toComplete
244265 ) ;
245266 completions . push (
246267 ...comps . map (
247- ( comp ) => `${ comp . action } \t${ comp . description ?? "" } ` ,
248- ) ,
268+ ( comp ) => `${ comp . action } \t${ comp . description ?? "" } `
269+ )
249270 ) ;
250271 break ;
251272 } else {
@@ -257,12 +278,12 @@ export default function tab(instance: CAC): void {
257278 // User has not provided input for this positional argument
258279 const comps = await positional . completion (
259280 previousArgs ,
260- toComplete ,
281+ toComplete
261282 ) ;
262283 completions . push (
263284 ...comps . map (
264- ( comp ) => `${ comp . action } \t${ comp . description ?? "" } ` ,
265- ) ,
285+ ( comp ) => `${ comp . action } \t${ comp . description ?? "" } `
286+ )
266287 ) ;
267288 break ;
268289 }
0 commit comments