Common normalize logic

This commit is contained in:
Will Boyd
2025-01-27 08:42:08 -05:00
parent 7cd5722195
commit 19c65b21e4
2 changed files with 28 additions and 28 deletions
+2 -2
View File
@@ -10,7 +10,7 @@ export function boolean(value) {
return false; return false;
} }
throw 'Must be true or false.'; throw new Error('Must be true or false.');
} }
export function filePath(value) { export function filePath(value) {
@@ -27,6 +27,6 @@ export function filePath(value) {
if (fileExists) { if (fileExists) {
return absolute; return absolute;
} else { } else {
throw 'File not found at ' + absolute + '.'; throw new Error('File not found at ' + absolute + '.');
} }
} }
+26 -26
View File
@@ -4,6 +4,16 @@ import chalk from 'chalk';
import * as commander from 'commander'; import * as commander from 'commander';
import * as normalizers from './normalizers.js'; import * as normalizers from './normalizers.js';
const promptTheme = {
prefix: {
idle: chalk.gray('\n?'),
done: chalk.green('✓')
},
style: {
description: (text) => chalk.gray('example: ' + text)
}
};
// all user options for command line and wizard are declared here // all user options for command line and wizard are declared here
const options = [ const options = [
{ {
@@ -121,35 +131,21 @@ export async function getConfig(argv) {
let normalizedAnswer = undefined; let normalizedAnswer = undefined;
const promptConfig = { const promptConfig = {
theme: promptTheme,
message: question.description + '?', message: question.description + '?',
default: question.default, default: question.default,
theme: {
prefix: {
idle: chalk.gray('\n?'),
done: chalk.green('✓')
},
style: {
description: (text) => chalk.gray('example: ' + text)
}
}
}; };
if (question.choices) { if (question.choices) {
promptConfig.choices = question.choices; promptConfig.choices = question.choices;
promptConfig.loop = false; promptConfig.loop = false;
} else { } else {
const normalizer = normalizers[camelcase(question.type)]; promptConfig.validate = (value) => {
if (normalizer) { let validationResult;
promptConfig.validate = (value) => { normalizedAnswer = normalize(value, question.type, (errorMessage) => {
try { validationResult = errorMessage;
normalizedAnswer = normalizer(value); });
} catch (ex) { return validationResult ?? true;
return ex.toString();
}
return true;
}
} }
} }
@@ -189,7 +185,9 @@ function parseCommandLine() {
if (input.choices && input.type !== 'boolean') { if (input.choices && input.type !== 'boolean') {
option.choices(input.choices.map((choice) => choice.value)); option.choices(input.choices.map((choice) => choice.value));
} else { } else {
option.argParser((value) => normalizeCommandLineArg(input.type, input.name, value)); option.argParser((value) => normalize(value, input.type, (errorMessage) => {
throw new commander.InvalidArgumentError(errorMessage);
}));
} }
commander.program.addOption(option); commander.program.addOption(option);
@@ -198,6 +196,7 @@ function parseCommandLine() {
const opts = commander.program.parse().opts(); const opts = commander.program.parse().opts();
for (const [key, value] of Object.entries(opts)) { for (const [key, value] of Object.entries(opts)) {
console.log(key, value);
if (key === 'wizard' || commander.program.getOptionValueSource(key) !== 'default') { if (key === 'wizard' || commander.program.getOptionValueSource(key) !== 'default') {
continue; continue;
} }
@@ -206,14 +205,16 @@ function parseCommandLine() {
delete opts[key]; delete opts[key];
} else { } else {
const option = options.find((option) => camelcase(option.name) === key); const option = options.find((option) => camelcase(option.name) === key);
opts[key] = normalizeCommandLineArg(option.type, option.name, value); opts[key] = normalize(value, option.type, (errorMessage) => {
commander.program.error(`error: option '--${option.name} <${option.type}>' argument '${value}' is invalid. ${errorMessage}`);
});
} }
} }
return opts; return opts;
} }
function normalizeCommandLineArg(type, name, value) { function normalize(value, type, onError) {
const normalizer = normalizers[camelcase(type)]; const normalizer = normalizers[camelcase(type)];
if (!normalizer) { if (!normalizer) {
return value; return value;
@@ -222,7 +223,6 @@ function normalizeCommandLineArg(type, name, value) {
try { try {
return normalizer(value); return normalizer(value);
} catch (ex) { } catch (ex) {
commander.program.error(`error: option '--${name} <${type}>' argument '${value}' is invalid. ${ex.toString()}`); onError(ex.message);
// throw new commander.InvalidArgumentError('potato');
} }
} }