@@ -7,10 +7,12 @@ import {
77 collectChoiceRequirements ,
88 getUnresolvedRequirements ,
99} from "../preflight/collectChoiceRequirements" ;
10+ import { flattenChoicesWithPath } from "../utils/choiceUtils" ;
11+ import { toError } from "../utils/errorUtils" ;
1012import type IChoice from "../types/choices/IChoice" ;
11- import type IMultiChoice from "../types/choices/IMultiChoice" ;
1213
1314type ChoiceType = IChoice [ "type" ] ;
15+ type DebugAction = "get" | "reset" | "flush" ;
1416
1517interface CliChoiceSummary {
1618 id : string ;
@@ -37,12 +39,14 @@ interface RegisterCliHandlerTarget {
3739}
3840
3941interface DebugCliTarget {
40- getDebugStats ?: ( ) => {
41- persistence : unknown ;
42- ui : unknown ;
42+ getDebugApi ?: ( ) => {
43+ getStats : ( ) => {
44+ persistence : unknown ;
45+ ui : unknown ;
46+ } ;
47+ resetStats : ( ) => void ;
48+ flushNow : ( ) => Promise < unknown > ;
4349 } | null ;
44- resetDebugStats ?: ( ) => boolean ;
45- flushDebugPersistence ?: ( ) => Promise < unknown > ;
4650}
4751
4852const RUN_FLAGS : CliFlags = {
@@ -95,6 +99,12 @@ const DEBUG_FLAGS: CliFlags = {
9599 } ,
96100} ;
97101
102+ const DEBUG_ACTIONS = {
103+ get : "get" ,
104+ reset : "reset" ,
105+ flush : "flush" ,
106+ } as const satisfies Record < DebugAction , DebugAction > ;
107+
98108const RESERVED_RUN_PARAMS = new Set < string > ( [ "choice" , "id" , "vars" , "ui" ] ) ;
99109const RESERVED_CHECK_PARAMS = new Set < string > ( [ "choice" , "id" , "vars" ] ) ;
100110
@@ -128,11 +138,7 @@ function parseVarsJson(value: string): Record<string, unknown> {
128138 try {
129139 parsed = JSON . parse ( value ) ;
130140 } catch ( error ) {
131- throw new Error (
132- `Invalid vars JSON: ${
133- error instanceof Error ? error . message : String ( error )
134- } `,
135- ) ;
141+ throw toError ( error , "Invalid vars JSON" ) ;
136142 }
137143
138144 if ( ! parsed || typeof parsed !== "object" || Array . isArray ( parsed ) ) {
@@ -166,34 +172,6 @@ function extractVariables(
166172 return variables ;
167173}
168174
169- function flattenChoices (
170- choices : IChoice [ ] ,
171- segments : string [ ] = [ ] ,
172- ) : CliChoiceSummary [ ] {
173- const flattened : CliChoiceSummary [ ] = [ ] ;
174-
175- for ( const choice of choices ) {
176- const pathSegments = [ ...segments , choice . name ] ;
177- const path = pathSegments . join ( " / " ) ;
178- const isMulti = choice . type === "Multi" ;
179- flattened . push ( {
180- id : choice . id ,
181- name : choice . name ,
182- type : choice . type ,
183- command : choice . command ,
184- path,
185- runnable : ! isMulti ,
186- } ) ;
187-
188- if ( isMulti ) {
189- const multiChoice = choice as IMultiChoice ;
190- flattened . push ( ...flattenChoices ( multiChoice . choices , pathSegments ) ) ;
191- }
192- }
193-
194- return flattened ;
195- }
196-
197175function resolveChoiceFromParams ( plugin : QuickAdd , params : CliData ) : IChoice {
198176 if ( typeof params . id === "string" && params . id . trim ( ) . length > 0 ) {
199177 return plugin . getChoiceById ( params . id ) ;
@@ -321,7 +299,7 @@ async function runChoiceHandler(
321299 return serialize ( {
322300 ok : false ,
323301 command,
324- error : error instanceof Error ? error . message : String ( error ) ,
302+ error : toError ( error ) . message ,
325303 } ) ;
326304 }
327305}
@@ -340,7 +318,20 @@ function listChoicesHandler(plugin: QuickAdd, params: CliData): string {
340318 } ) ;
341319 }
342320
343- const allChoices = flattenChoices ( plugin . settings . choices ) ;
321+ const allChoices = flattenChoicesWithPath ( plugin . settings . choices ) . map (
322+ ( { choice, pathSegments } ) => {
323+ const isMulti = choice . type === "Multi" ;
324+ const summary : CliChoiceSummary = {
325+ id : choice . id ,
326+ name : choice . name ,
327+ type : choice . type ,
328+ command : choice . command ,
329+ path : pathSegments . join ( " / " ) ,
330+ runnable : ! isMulti ,
331+ } ;
332+ return summary ;
333+ } ,
334+ ) ;
344335 const choices = allChoices . filter ( ( choice ) => {
345336 if (
346337 requestedType &&
@@ -362,7 +353,7 @@ function listChoicesHandler(plugin: QuickAdd, params: CliData): string {
362353 return serialize ( {
363354 ok : false ,
364355 command : CLI_COMMANDS . list ,
365- error : error instanceof Error ? error . message : String ( error ) ,
356+ error : toError ( error ) . message ,
366357 } ) ;
367358 }
368359}
@@ -415,15 +406,24 @@ async function checkChoiceHandler(
415406 return serialize ( {
416407 ok : false ,
417408 command : CLI_COMMANDS . check ,
418- error : error instanceof Error ? error . message : String ( error ) ,
409+ error : toError ( error ) . message ,
419410 } ) ;
420411 }
421412}
422413
414+ function parseDebugAction ( actionRaw : string | undefined ) : DebugAction | null {
415+ const normalized = ( actionRaw ?? DEBUG_ACTIONS . get ) . trim ( ) . toLowerCase ( ) ;
416+ if ( normalized === DEBUG_ACTIONS . get ) return DEBUG_ACTIONS . get ;
417+ if ( normalized === DEBUG_ACTIONS . reset ) return DEBUG_ACTIONS . reset ;
418+ if ( normalized === DEBUG_ACTIONS . flush ) return DEBUG_ACTIONS . flush ;
419+ return null ;
420+ }
421+
423422async function debugHandler ( plugin : QuickAdd , params : CliData ) : Promise < string > {
424- const actionRaw = typeof params . action === "string" ? params . action : "get" ;
425- const action = actionRaw . trim ( ) . toLowerCase ( ) ;
423+ const actionRaw = typeof params . action === "string" ? params . action : undefined ;
424+ const action = parseDebugAction ( actionRaw ) ;
426425 const debugTarget = plugin as unknown as DebugCliTarget ;
426+ const debugApi = debugTarget . getDebugApi ?.( ) ?? null ;
427427
428428 if ( ! plugin . settings . devMode ) {
429429 return serialize ( {
@@ -433,48 +433,56 @@ async function debugHandler(plugin: QuickAdd, params: CliData): Promise<string>
433433 } ) ;
434434 }
435435
436- if ( action === "get" ) {
436+ if ( ! debugApi ) {
437+ return serialize ( {
438+ ok : false ,
439+ command : CLI_COMMANDS . debug ,
440+ error : "Debug API is unavailable." ,
441+ } ) ;
442+ }
443+
444+ if ( action === DEBUG_ACTIONS . get ) {
437445 return serialize ( {
438446 ok : true ,
439447 command : CLI_COMMANDS . debug ,
440- action : " get" ,
441- stats : debugTarget . getDebugStats ?. ( ) ?? null ,
448+ action : DEBUG_ACTIONS . get ,
449+ stats : debugApi . getStats ( ) ,
442450 } ) ;
443451 }
444452
445- if ( action === " reset" ) {
446- const reset = debugTarget . resetDebugStats ?. ( ) ?? false ;
453+ if ( action === DEBUG_ACTIONS . reset ) {
454+ debugApi . resetStats ( ) ;
447455 return serialize ( {
448- ok : reset ,
456+ ok : true ,
449457 command : CLI_COMMANDS . debug ,
450- action : " reset" ,
451- stats : debugTarget . getDebugStats ?. ( ) ?? null ,
458+ action : DEBUG_ACTIONS . reset ,
459+ stats : debugApi . getStats ( ) ,
452460 } ) ;
453461 }
454462
455- if ( action === " flush" ) {
463+ if ( action === DEBUG_ACTIONS . flush ) {
456464 try {
457- const persistence = await debugTarget . flushDebugPersistence ?. ( ) ;
465+ const persistence = await debugApi . flushNow ( ) ;
458466 return serialize ( {
459467 ok : true ,
460468 command : CLI_COMMANDS . debug ,
461- action : " flush" ,
469+ action : DEBUG_ACTIONS . flush ,
462470 persistence : persistence ?? null ,
463471 } ) ;
464472 } catch ( error ) {
465473 return serialize ( {
466474 ok : false ,
467475 command : CLI_COMMANDS . debug ,
468- action : " flush" ,
469- error : error instanceof Error ? error . message : String ( error ) ,
476+ action : DEBUG_ACTIONS . flush ,
477+ error : toError ( error ) . message ,
470478 } ) ;
471479 }
472480 }
473481
474482 return serialize ( {
475483 ok : false ,
476484 command : CLI_COMMANDS . debug ,
477- error : `Invalid action '${ actionRaw } '. Use get, reset, or flush.` ,
485+ error : `Invalid action '${ actionRaw ?? "" } '. Use get, reset, or flush.` ,
478486 } ) ;
479487}
480488
0 commit comments