Fixed validation/normalization for wizard

This commit is contained in:
Will Boyd
2025-01-25 13:46:47 -05:00
parent 1f890b4079
commit 6a6e5074f0
+26 -10
View File
@@ -148,23 +148,41 @@ export async function getConfig(argv) {
console.log('\nStarting wizard...');
const questions = options.filter(option => (option.name !== 'wizard' && !option.isProvided));
for (const question of questions) {
let answer = await question.prompt({
const promptConfig = {
message: question.description + '?',
choices: question.choices,
loop: false,
default: question.default,
validate: question.validate, // not all questions have this, which is fine
theme: {
prefix: {
idle: chalk.cyan('\n?'),
idle: chalk.gray('\n?'),
done: chalk.green('✓')
},
style: {
description: (text) => chalk.gray('example: ' + text)
}
}
}).catch((ex) => {
};
if (question.choices) {
promptConfig.choices = question.choices;
promptConfig.loop = false;
} else {
const validator = validators[question.type];
if (validator) {
promptConfig.validate = (value) => {
try {
normalized = validator(value);
} catch (ex) {
return ex.toString();
}
return true;
}
}
}
let normalized = undefined;
let answer = await question.prompt(promptConfig).catch((ex) => {
if (ex instanceof Error && ex.name === 'ExitPromptError') {
console.log('\nUser quit wizard early.');
process.exit(0);
@@ -173,11 +191,9 @@ export async function getConfig(argv) {
}
});
if (question.normalize) {
answer = question.normalize(answer);
}
console.log(normalized, answer);
answers[camelcase(question.name)] = answer;
answers[camelcase(question.name)] = normalized ?? answer;
}
} else {
console.log('\nSkipping wizard...');