Skip to content

Commit 9c77e90

Browse files
committed
when()
1 parent 7287f9c commit 9c77e90

3 files changed

Lines changed: 59 additions & 42 deletions

File tree

packages/inquirerer/dev/index.ts

Lines changed: 12 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ console.log(argv);
1919

2020
// argv.checkbox = ['RBanana'];
2121
// argv.checkbox = ['Banana'];
22-
argv.checkbox = ['Banana', 'Cherry', 'Blos'];
22+
// argv.checkbox = ['Banana', 'Cherry', 'Blos'];
2323

2424
if (argv.version) {
2525
displayVersion();
@@ -30,6 +30,7 @@ const prompter = new Inquirerer({
3030
noTty: !argv.tty
3131
});
3232

33+
let after = {};
3334
const main = async () => {
3435

3536
const massive = await prompter.prompt(argv, [
@@ -59,47 +60,18 @@ const main = async () => {
5960
'Grape'
6061
]
6162
},
62-
// {
63-
// name: 'checkbox',
64-
// type: 'checkbox',
65-
// required: true,
66-
// default: ['RBanana', 'RCherry'],
67-
// options: [
68-
// { name: 'RApple', value: 'Fruit01' },
69-
// { name: 'RBanana', value: 'Fruit02' },
70-
// { name: 'RCherry', value: 'Fruit03' },
71-
// { name: 'RGrape', value: 'Fruit04' },
72-
// { name: 'RMango', value: 'Fruit05' }
73-
// ]
74-
// },
75-
// {
76-
// name: 'list',
77-
// type: 'list',
78-
// required: true,
79-
// default: ['RCherry'],
80-
// options: [
81-
// { name: 'RApple', value: 'Fruit01' },
82-
// { name: 'RBanana', value: 'Fruit02' },
83-
// { name: 'RCherry', value: 'Fruit03' },
84-
// { name: 'RGrape', value: 'Fruit04' },
85-
// { name: 'RMango', value: 'Fruit05' }
86-
// ]
87-
// },
88-
// {
89-
// name: 'autocomplete',
90-
// type: 'autocomplete',
91-
// required: true,
92-
// default: ['RGrape'],
93-
// options: [
94-
// { name: 'RApple', value: 'Fruit01' },
95-
// { name: 'RBanana', value: 'Fruit02' },
96-
// { name: 'RCherry', value: 'Fruit03' },
97-
// { name: 'RGrape', value: 'Fruit04' },
98-
// { name: 'RMango', value: 'Fruit05' }
99-
// ]
100-
// },
63+
{
64+
name: 'testme',
65+
type: 'text',
66+
dependsOn: ['checkbox'],
67+
when: (answers) => {
68+
const res = answers.checkbox?.find((a: any) => a.name === 'Banana');
69+
return !!(res && res.selected);
70+
}
71+
}
10172
])
10273
console.log(JSON.stringify(massive, null, 2))
74+
console.log(JSON.stringify(after, null, 2))
10375
prompter.close();
10476
};
10577

packages/inquirerer/src/prompt.ts

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,40 @@ export interface PromptOptions {
2222
mutateArgs?: boolean;
2323
}
2424

25+
export function reorderQuestionsByDeps(questions: Question[]): Question[] {
26+
const nameToIndex = new Map<string, number>();
27+
questions.forEach((q, idx) => nameToIndex.set(q.name, idx));
28+
29+
const resolved = new Set<string>();
30+
const result: Question[] = [];
31+
32+
function addQuestion(q: Question) {
33+
// If this question depends on others, ensure those are added first
34+
if (q.dependsOn && q.dependsOn.length > 0) {
35+
for (const dep of q.dependsOn) {
36+
if (!resolved.has(dep)) {
37+
const depIdx = nameToIndex.get(dep);
38+
if (depIdx === undefined) {
39+
throw new Error(`Unknown dependency: ${dep}`);
40+
}
41+
addQuestion(questions[depIdx]);
42+
}
43+
}
44+
}
45+
if (!resolved.has(q.name)) {
46+
resolved.add(q.name);
47+
result.push(q);
48+
}
49+
}
50+
51+
for (const q of questions) {
52+
addQuestion(q);
53+
}
54+
55+
return result;
56+
}
57+
58+
2559
const validationMessage = (question: Question, ctx: PromptContext): string => {
2660
if (ctx.numTries === 0 || ctx.validation.success) {
2761
return ''; // No message if first attempt or validation was successful
@@ -447,8 +481,12 @@ export class Inquirerer {
447481
throw new Error('Missing required arguments. Please provide all required parameters.');
448482
}
449483

450-
for (let index = 0; index < questions.length; index++) {
451-
const question = questions[index];
484+
485+
486+
const ordered = reorderQuestionsByDeps(questions);
487+
488+
for (let index = 0; index < ordered.length; index++) {
489+
const question = ordered[index];
452490
const ctx: PromptContext = new PromptContext();
453491

454492
// obj is already either argv itself, or a clone, but let's check if it has the property
@@ -458,6 +496,11 @@ export class Inquirerer {
458496
continue;
459497
}
460498

499+
if (question.when && !question.when(obj)) {
500+
ctx.nextQuestion();
501+
continue;
502+
}
503+
461504
// Apply default value if applicable
462505
// this is if useDefault is set, rare! not typical defaults which happen AFTER
463506
// this is mostly to avoid a prompt for "hidden" options

packages/inquirerer/src/question/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ export interface BaseQuestion {
2525
validate?: (input: any, obj: any) => Validation | boolean;
2626
sanitize?: (input: any, obj: any) => any;
2727
pattern?: string;
28+
dependsOn?: string[];
29+
when?: (answers: any) => boolean;
2830
}
2931

3032
export interface ConfirmQuestion extends BaseQuestion {

0 commit comments

Comments
 (0)