From 91073742faa81a14bc890777fe3dc048f7916c4b Mon Sep 17 00:00:00 2001 From: Will Boyd Date: Mon, 27 Jan 2025 14:57:04 -0500 Subject: [PATCH] Fix undefined answer bug, start on example path --- src/questions.js | 21 +++++++-------------- src/wizard.js | 31 +++++++++++++++++++++++++++---- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/src/questions.js b/src/questions.js index 1eb50df..8c96b69 100644 --- a/src/questions.js +++ b/src/questions.js @@ -22,13 +22,11 @@ export const all = [ choices: [ { name: 'Yes', - value: true, - description: '/my-post/index.md' + value: true }, { name: 'No', - value: false, - description: '/my-post.md' + value: false } ], prompt: inquirer.select @@ -41,13 +39,11 @@ export const all = [ choices: [ { name: 'Yes', - value: true, - description: '' + value: true }, { name: 'No', - value: false, - description: '' + value: false } ], prompt: inquirer.select @@ -60,18 +56,15 @@ export const all = [ choices: [ { name: 'Year folders', - value: 'year', - description: '' + value: 'year' }, { name: 'Year and month folders', - value: 'year-month', - description: '' + value: 'year-month' }, { name: 'No', - value: 'none', - description: '' + value: 'none' } ], prompt: inquirer.select diff --git a/src/wizard.js b/src/wizard.js index 3e7ce28..e2654a0 100644 --- a/src/wizard.js +++ b/src/wizard.js @@ -84,7 +84,6 @@ function getCommandLineAnswers(questions) { export async function getWizardAnswers(questions) { const answers = {}; for (const question of questions) { - // this will be set to the normalized answer during validation let normalizedAnswer; const promptConfig = { @@ -106,8 +105,7 @@ export async function getWizardAnswers(questions) { } } - // don't care about the return value of prompt() because normalizedAnswer will be used - await question.prompt(promptConfig).catch((ex) => { + const answer = await question.prompt(promptConfig).catch((ex) => { // exit gracefully if user hits ctrl + c during wizard if (ex instanceof Error && ex.name === 'ExitPromptError') { console.log('\nUser quit wizard early.'); @@ -117,7 +115,7 @@ export async function getWizardAnswers(questions) { } }); - answers[camelcase(question.name)] = normalizedAnswer; + answers[camelcase(question.name)] = normalizedAnswer ?? answer; } return answers; @@ -135,3 +133,28 @@ function normalize(value, type, onError) { onError(ex.message); } } + +function buildSamplePath(config) { + const pathSegments = []; + + if (config.dateFolders === 'year' || config.dateFolders === 'year-month') { + pathSegments.push('2025'); + } + + if (config.dateFolders === 'year-month') { + pathSegments.push('01'); + } + + let slugFragment = 'my-post'; + if (config.prefixDate) { + slugFragment = '2025-01-31-' + slugFragment; + } + + if (config.postFolders) { + pathSegments.push(slugFragment, 'index.md'); + } else { + pathSegments.push(slugFragment + '.md'); + } + + return '/' + pathSegments.join('/'); +}