@@ -4,6 +4,7 @@ import { Readable, Writable } from 'stream';
44
55import { KEY_CODES , TerminalKeypress } from './keypress' ;
66import { AutocompleteQuestion , CheckboxQuestion , ConfirmQuestion , ListQuestion , NumberQuestion , OptionValue , Question , TextQuestion , Validation , Value } from './question' ;
7+ import { DefaultResolverRegistry , globalResolverRegistry } from './resolvers' ;
78// import { writeFileSync } from 'fs';
89
910// const debuglog = (obj: any) => {
@@ -250,6 +251,7 @@ export interface InquirererOptions {
250251 useDefaults ?: boolean ;
251252 globalMaxLines ?: number ;
252253 mutateArgs ?: boolean ;
254+ resolverRegistry ?: DefaultResolverRegistry ;
253255
254256}
255257export class Inquirerer {
@@ -261,6 +263,7 @@ export class Inquirerer {
261263 private useDefaults : boolean ;
262264 private globalMaxLines : number ;
263265 private mutateArgs : boolean ;
266+ private resolverRegistry : DefaultResolverRegistry ;
264267
265268 private handledKeys : Set < string > = new Set ( ) ;
266269
@@ -273,7 +276,8 @@ export class Inquirerer {
273276 output = process . stdout ,
274277 useDefaults = false ,
275278 globalMaxLines = 10 ,
276- mutateArgs = true
279+ mutateArgs = true ,
280+ resolverRegistry = globalResolverRegistry
277281 } = options ?? { }
278282
279283 this . useDefaults = useDefaults ;
@@ -282,6 +286,7 @@ export class Inquirerer {
282286 this . mutateArgs = mutateArgs ;
283287 this . input = input ;
284288 this . globalMaxLines = globalMaxLines ;
289+ this . resolverRegistry = resolverRegistry ;
285290
286291 if ( ! noTty ) {
287292 this . rl = readline . createInterface ( {
@@ -461,6 +466,9 @@ export class Inquirerer {
461466 const shouldMutate = options ?. mutateArgs !== undefined ? options . mutateArgs : this . mutateArgs ;
462467 let obj : any = shouldMutate ? argv : { ...argv } ;
463468
469+ // Resolve dynamic defaults before processing questions
470+ await this . resolveDynamicDefaults ( questions ) ;
471+
464472 // first loop through the question, and set any overrides in case other questions use objs for validation
465473 this . applyOverrides ( argv , obj , questions ) ;
466474
@@ -543,6 +551,43 @@ export class Inquirerer {
543551 return questions . some ( question => question . required && this . isEmptyAnswer ( argv [ question . name ] ) ) ;
544552 }
545553
554+ /**
555+ * Resolves the default value for a question using the resolver system.
556+ * Priority: defaultFrom > default > undefined
557+ */
558+ private async resolveQuestionDefault ( question : Question ) : Promise < any > {
559+ // Try to resolve from defaultFrom first
560+ if ( 'defaultFrom' in question && question . defaultFrom ) {
561+ const resolved = await this . resolverRegistry . resolve ( question . defaultFrom ) ;
562+ if ( resolved !== undefined ) {
563+ return resolved ;
564+ }
565+ }
566+
567+ // Fallback to static default
568+ if ( 'default' in question ) {
569+ return question . default ;
570+ }
571+
572+ return undefined ;
573+ }
574+
575+ /**
576+ * Resolves dynamic defaults for all questions that have defaultFrom specified.
577+ * Updates the question.default property with the resolved value.
578+ */
579+ private async resolveDynamicDefaults ( questions : Question [ ] ) : Promise < void > {
580+ for ( const question of questions ) {
581+ if ( 'defaultFrom' in question && question . defaultFrom ) {
582+ const resolved = await this . resolveQuestionDefault ( question ) ;
583+ if ( resolved !== undefined ) {
584+ // Update question.default with resolved value
585+ ( question as any ) . default = resolved ;
586+ }
587+ }
588+ }
589+ }
590+
546591 private applyDefaultValues ( questions : Question [ ] , obj : any ) : void {
547592 questions . forEach ( question => {
548593 if ( 'default' in question ) {
0 commit comments