@@ -19,32 +19,33 @@ const x = `${quotedExecPath} ${quotedProcessExecArgs.join(" ")} ${quotedProcessA
1919const completion = new Completion ( ) ;
2020
2121export default async function tab ( instance : CommandDef ) {
22- const meta =
23- typeof instance . meta === "function" ? await instance . meta ( ) : await instance . meta ;
22+ const meta = await resolve ( instance . meta ) ;
2423
2524 if ( ! meta ) {
26- throw new Error ( )
25+ throw new Error ( "Invalid meta." ) ;
2726 }
28- const name = meta . name
27+ const name = meta . name ;
2928 if ( ! name ) {
30- throw new Error ( )
29+ throw new Error ( "Invalid meta or missing name." ) ;
3130 }
3231
33- const subCommands = typeof instance . subCommands === "function" ? await instance . subCommands ( ) : await instance . subCommands
32+ const subCommands = await resolve ( instance . subCommands ) ;
3433 if ( ! subCommands ) {
35- throw new Error ( )
34+ throw new Error ( "Invalid or missing subCommands." ) ;
3635 }
3736
3837 completion . addCommand ( meta . name ! , meta ?. description ?? "" , ( ) => { } ) ;
39- // Object.values(subCommands).forEach((cmd) => {
40- //TODO: resolver function
38+
4139 for ( const [ cmd , resolvableConfig ] of Object . entries ( subCommands ) ) {
42- const config = typeof resolvableConfig === "function" ? await resolvableConfig ( ) : await resolvableConfig
43- const meta = typeof config . meta === "function" ? await config . meta ( ) : await config . meta ;
40+ const config = await resolve ( resolvableConfig ) ;
41+ const meta = await resolve ( config . meta ) ;
42+
4443 if ( ! meta || typeof meta ?. description !== "string" ) {
4544 throw new Error ( "Invalid meta or missing description." ) ;
4645 }
46+
4747 completion . addCommand ( cmd , meta . description , config . run ?? ( ( ) => { } ) ) ;
48+
4849 if ( config . args ) {
4950 for ( const [ argName , argConfig ] of Object . entries ( config . args ) ) {
5051 const conf = argConfig as ArgDef ;
@@ -57,7 +58,6 @@ export default async function tab(instance: CommandDef) {
5758 }
5859 }
5960 }
60- // });
6161
6262 if ( instance . args ) {
6363 for ( const [ argName , argConfig ] of Object . entries ( instance . args ) ) {
@@ -114,12 +114,14 @@ export default async function tab(instance: CommandDef) {
114114 }
115115 default : {
116116 const extra = ctx . args . _ || [ ] ;
117- // We simply call parse (which prints completions directly)
118117 await completion . parse ( extra , instance ) ;
119- // parse() does its own console.logs, so no return object to destructure
120118 return ;
121119 }
122120 }
123121 } ,
124122 } ) ;
125123}
124+
125+ async function resolve < T > ( resolvable : T | ( ( ) => T | Promise < T > ) ) : Promise < T > {
126+ return typeof resolvable === "function" ? await ( resolvable as ( ) => T | Promise < T > ) ( ) : resolvable ;
127+ }
0 commit comments