@@ -9,14 +9,13 @@ import { setupCompletionForPackageManager } from './completion-handlers';
99const packageManagers = [ 'npm' , 'pnpm' , 'yarn' , 'bun' ] ;
1010const shells = [ 'zsh' , 'bash' , 'fish' , 'powershell' ] ;
1111
12- const cli = cac ( 'tab' ) ;
12+ async function main ( ) {
13+ const cli = cac ( 'tab' ) ;
14+
15+ const args = process . argv . slice ( 2 ) ;
16+ if ( args . length >= 2 && args [ 1 ] === 'complete' ) {
17+ const packageManager = args [ 0 ] ;
1318
14- cli
15- . command (
16- '<packageManager> complete' ,
17- 'Process completion requests from shell'
18- )
19- . action ( async ( packageManager ) => {
2019 if ( ! packageManagers . includes ( packageManager ) ) {
2120 console . error ( `Error: Unsupported package manager "${ packageManager } "` ) ;
2221 console . error (
3938 ) ;
4039 process . exit ( 1 ) ;
4140 }
42- } ) ;
41+ }
4342
44- cli
45- . command (
46- '<packageManager> <shell>' ,
47- 'Generate shell completion script for a package manager'
48- )
49- . action ( async ( packageManager , shell ) => {
50- if ( shell === 'complete' ) {
51- const dashIndex = process . argv . indexOf ( '--' ) ;
52- if ( dashIndex !== - 1 ) {
53- const completion = new Completion ( ) ;
54- setupCompletionForPackageManager ( packageManager , completion ) ;
55- const toComplete = process . argv . slice ( dashIndex + 1 ) ;
56- await completion . parse ( toComplete ) ;
57- process . exit ( 0 ) ;
58- } else {
59- console . error ( `Error: Expected '--' followed by command to complete` ) ;
43+ cli
44+ . command (
45+ '<packageManager> <shell>' ,
46+ 'Generate shell completion script for a package manager'
47+ )
48+ . action ( async ( packageManager , shell ) => {
49+ if ( ! packageManagers . includes ( packageManager ) ) {
50+ console . error ( `Error: Unsupported package manager "${ packageManager } "` ) ;
6051 console . error (
61- `Example: ${ packageManager } exec @bombsh/tab ${ packageManager } complete -- command-to-complete `
52+ `Supported package managers: ${ packageManagers . join ( ', ' ) } `
6253 ) ;
6354 process . exit ( 1 ) ;
6455 }
65- return ;
66- }
67-
68- if ( ! packageManagers . includes ( packageManager ) ) {
69- console . error ( `Error: Unsupported package manager "${ packageManager } "` ) ;
70- console . error (
71- `Supported package managers: ${ packageManagers . join ( ', ' ) } `
72- ) ;
73- process . exit ( 1 ) ;
74- }
7556
76- if ( ! shells . includes ( shell ) ) {
77- console . error ( `Error: Unsupported shell "${ shell } "` ) ;
78- console . error ( `Supported shells: ${ shells . join ( ', ' ) } ` ) ;
79- process . exit ( 1 ) ;
80- }
57+ if ( ! shells . includes ( shell ) ) {
58+ console . error ( `Error: Unsupported shell "${ shell } "` ) ;
59+ console . error ( `Supported shells: ${ shells . join ( ', ' ) } ` ) ;
60+ process . exit ( 1 ) ;
61+ }
8162
82- generateCompletionScript ( packageManager , shell ) ;
83- } ) ;
63+ generateCompletionScript ( packageManager , shell ) ;
64+ } ) ;
8465
85- const completion = tab ( cli ) ;
66+ const completion = tab ( cli ) ;
8667
87- cli . parse ( ) ;
68+ cli . parse ( ) ;
69+ }
8870
8971function generateCompletionScript ( packageManager : string , shell : string ) {
9072 const name = packageManager ;
@@ -93,3 +75,5 @@ function generateCompletionScript(packageManager: string, shell: string) {
9375 : `node ${ process . argv [ 1 ] } ${ packageManager } ` ;
9476 script ( shell as any , name , executable ) ;
9577}
78+
79+ main ( ) . catch ( console . error ) ;
0 commit comments