From 8d4e7a03fc992c55c2698161cedd000103070f6c Mon Sep 17 00:00:00 2001 From: Will Boyd Date: Mon, 27 Jan 2025 17:12:17 -0500 Subject: [PATCH] Show example paths during wizard --- src/questions.js | 4 ++++ src/wizard.js | 31 +++++++++++++++++++++---------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/questions.js b/src/questions.js index 8c96b69..86da5b1 100644 --- a/src/questions.js +++ b/src/questions.js @@ -29,6 +29,7 @@ export const all = [ value: false } ], + isPathQuestion: true, prompt: inquirer.select }, { @@ -46,6 +47,7 @@ export const all = [ value: false } ], + isPathQuestion: true, prompt: inquirer.select }, { @@ -67,6 +69,7 @@ export const all = [ value: 'none' } ], + isPathQuestion: true, prompt: inquirer.select }, { @@ -92,6 +95,7 @@ export const all = [ value: 'none' } ], + isPathQuestion: true, prompt: inquirer.select } ]; diff --git a/src/wizard.js b/src/wizard.js index e2654a0..de22e55 100644 --- a/src/wizard.js +++ b/src/wizard.js @@ -16,23 +16,22 @@ const promptTheme = { }; export async function getConfig() { - const config = {}; - // check command line for any config options const commandLineQuestions = questions.all; - Object.assign(config, getCommandLineAnswers(commandLineQuestions)); + const commandLineAnswers = getCommandLineAnswers(commandLineQuestions); - if (config.wizard) { + let wizardAnswers; + if (commandLineAnswers.wizard) { console.log('\nStarting wizard...'); // run wizard for remaining config options - const wizardQuestions = questions.all.filter((question) => !(camelcase(question.name) in config)); - Object.assign(config, await getWizardAnswers(wizardQuestions)); + const wizardQuestions = questions.all.filter((question) => !(camelcase(question.name) in commandLineAnswers)); + wizardAnswers = await getWizardAnswers(wizardQuestions, commandLineAnswers); } else { console.log('\nSkipping wizard...'); } - return config; + return { ...commandLineAnswers, ...wizardAnswers }; } function getCommandLineAnswers(questions) { @@ -81,9 +80,10 @@ function getCommandLineAnswers(questions) { return answers; } -export async function getWizardAnswers(questions) { +export async function getWizardAnswers(questions, commandLineAnswers) { const answers = {}; for (const question of questions) { + let answerKey = camelcase(question.name); let normalizedAnswer; const promptConfig = { @@ -95,6 +95,17 @@ export async function getWizardAnswers(questions) { if (question.choices) { promptConfig.choices = question.choices; promptConfig.loop = false; + + if (question.isPathQuestion) { + // create a snapshot config of command line answers and wizard answers so far + const config = { ...commandLineAnswers, ...answers }; + + promptConfig.choices.forEach((choice) => { + // show example path if this choice is selected + config[answerKey] = choice.value; + choice.description = buildExamplePath(config); + }); + } } else { promptConfig.validate = (value) => { let validationErrorMessage; @@ -115,7 +126,7 @@ export async function getWizardAnswers(questions) { } }); - answers[camelcase(question.name)] = normalizedAnswer ?? answer; + answers[answerKey] = normalizedAnswer ?? answer; } return answers; @@ -134,7 +145,7 @@ function normalize(value, type, onError) { } } -function buildSamplePath(config) { +function buildExamplePath(config) { const pathSegments = []; if (config.dateFolders === 'year' || config.dateFolders === 'year-month') {