Skip to content

Commit 21ea6dd

Browse files
feat(inquirerer): add optionsFrom resolver support for dynamic options
Adds a new 'optionsFrom' property to questions that allows resolving options dynamically from a resolver, similar to how 'defaultFrom' and 'setFrom' work for default values. This enables boilerplate templates to use dynamic option lists like: { "name": "license", "type": "list", "optionsFrom": "licenses" } The resolver should return an array of strings or OptionValue objects. Co-Authored-By: Dan Lynch <[email protected]>
1 parent 725145c commit 21ea6dd

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

packages/inquirerer/src/prompt.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,9 @@ export class Inquirerer {
469469
// Resolve dynamic defaults before processing questions
470470
await this.resolveDynamicDefaults(questions);
471471

472+
// Resolve dynamic options before processing questions
473+
await this.resolveOptionsFrom(questions);
474+
472475
// Resolve setFrom values - these bypass prompting entirely
473476
await this.resolveSetValues(questions, obj);
474477

@@ -609,6 +612,22 @@ export class Inquirerer {
609612
}
610613
}
611614

615+
/**
616+
* Resolves optionsFrom values for all questions that have optionsFrom specified.
617+
* Updates the question.options property with the resolved array.
618+
*/
619+
private async resolveOptionsFrom(questions: Question[]): Promise<void> {
620+
for (const question of questions) {
621+
if ('optionsFrom' in question && question.optionsFrom) {
622+
const resolved = await this.resolverRegistry.resolve(question.optionsFrom);
623+
if (resolved !== undefined && Array.isArray(resolved)) {
624+
// Update question.options with resolved array
625+
(question as any).options = resolved;
626+
}
627+
}
628+
}
629+
}
630+
612631
private applyDefaultValues(questions: Question[], obj: any): void {
613632
questions.forEach(question => {
614633
if ('default' in question) {
@@ -1192,4 +1211,4 @@ export class Inquirerer {
11921211
this.keypress.destroy();
11931212
}
11941213
}
1195-
}
1214+
}

packages/inquirerer/src/question/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export interface BaseQuestion {
2020
default?: any;
2121
defaultFrom?: string;
2222
setFrom?: string;
23+
optionsFrom?: string;
2324
useDefault?: boolean;
2425
required?: boolean;
2526
message?: string;
@@ -69,4 +70,4 @@ export interface BaseQuestion {
6970
default?: number;
7071
}
7172

72-
export type Question = ConfirmQuestion | ListQuestion | AutocompleteQuestion | CheckboxQuestion | TextQuestion | NumberQuestion;
73+
export type Question = ConfirmQuestion | ListQuestion | AutocompleteQuestion | CheckboxQuestion | TextQuestion | NumberQuestion;

0 commit comments

Comments
 (0)