Show example paths during wizard

This commit is contained in:
Will Boyd
2025-01-27 17:12:17 -05:00
parent 91073742fa
commit 8d4e7a03fc
2 changed files with 25 additions and 10 deletions
+4
View File
@@ -29,6 +29,7 @@ export const all = [
value: false value: false
} }
], ],
isPathQuestion: true,
prompt: inquirer.select prompt: inquirer.select
}, },
{ {
@@ -46,6 +47,7 @@ export const all = [
value: false value: false
} }
], ],
isPathQuestion: true,
prompt: inquirer.select prompt: inquirer.select
}, },
{ {
@@ -67,6 +69,7 @@ export const all = [
value: 'none' value: 'none'
} }
], ],
isPathQuestion: true,
prompt: inquirer.select prompt: inquirer.select
}, },
{ {
@@ -92,6 +95,7 @@ export const all = [
value: 'none' value: 'none'
} }
], ],
isPathQuestion: true,
prompt: inquirer.select prompt: inquirer.select
} }
]; ];
+21 -10
View File
@@ -16,23 +16,22 @@ const promptTheme = {
}; };
export async function getConfig() { export async function getConfig() {
const config = {};
// check command line for any config options // check command line for any config options
const commandLineQuestions = questions.all; 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...'); console.log('\nStarting wizard...');
// run wizard for remaining config options // run wizard for remaining config options
const wizardQuestions = questions.all.filter((question) => !(camelcase(question.name) in config)); const wizardQuestions = questions.all.filter((question) => !(camelcase(question.name) in commandLineAnswers));
Object.assign(config, await getWizardAnswers(wizardQuestions)); wizardAnswers = await getWizardAnswers(wizardQuestions, commandLineAnswers);
} else { } else {
console.log('\nSkipping wizard...'); console.log('\nSkipping wizard...');
} }
return config; return { ...commandLineAnswers, ...wizardAnswers };
} }
function getCommandLineAnswers(questions) { function getCommandLineAnswers(questions) {
@@ -81,9 +80,10 @@ function getCommandLineAnswers(questions) {
return answers; return answers;
} }
export async function getWizardAnswers(questions) { export async function getWizardAnswers(questions, commandLineAnswers) {
const answers = {}; const answers = {};
for (const question of questions) { for (const question of questions) {
let answerKey = camelcase(question.name);
let normalizedAnswer; let normalizedAnswer;
const promptConfig = { const promptConfig = {
@@ -95,6 +95,17 @@ export async function getWizardAnswers(questions) {
if (question.choices) { if (question.choices) {
promptConfig.choices = question.choices; promptConfig.choices = question.choices;
promptConfig.loop = false; 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 { } else {
promptConfig.validate = (value) => { promptConfig.validate = (value) => {
let validationErrorMessage; let validationErrorMessage;
@@ -115,7 +126,7 @@ export async function getWizardAnswers(questions) {
} }
}); });
answers[camelcase(question.name)] = normalizedAnswer ?? answer; answers[answerKey] = normalizedAnswer ?? answer;
} }
return answers; return answers;
@@ -134,7 +145,7 @@ function normalize(value, type, onError) {
} }
} }
function buildSamplePath(config) { function buildExamplePath(config) {
const pathSegments = []; const pathSegments = [];
if (config.dateFolders === 'year' || config.dateFolders === 'year-month') { if (config.dateFolders === 'year' || config.dateFolders === 'year-month') {