From 8841c607e7cacdf3751a4212614c285bc332f2f4 Mon Sep 17 00:00:00 2001 From: Will Boyd Date: Sat, 25 Jan 2025 16:23:45 -0500 Subject: [PATCH] Separate normalizers out, fix config flag checks --- src/normalizers.js | 32 ++++++++++++++++++++++++++++ src/parser.js | 4 ++-- src/wizard.js | 53 +++++++++------------------------------------- src/writer.js | 4 ++-- 4 files changed, 46 insertions(+), 47 deletions(-) create mode 100644 src/normalizers.js diff --git a/src/normalizers.js b/src/normalizers.js new file mode 100644 index 0000000..44f4121 --- /dev/null +++ b/src/normalizers.js @@ -0,0 +1,32 @@ +import fs from 'fs'; +import path from 'path'; + +export function boolean(value) { + if (typeof value === 'boolean') { + return value; + } else if (value === 'true') { + return true; + } else if (value === 'false') { + return false; + } + + throw 'Must be true or false.'; +} + +export function filePath(value) { + const unwrapped = value.replace(/"(.*?)"/, '$1'); + const absolute = path.resolve(unwrapped); + + let fileExists; + try { + fileExists = fs.existsSync(absolute) && fs.statSync(absolute).isFile(); + } catch (ex) { + fileExists = false; + } + + if (fileExists) { + return absolute; + } else { + throw 'File not found at ' + absolute + '.'; + } +} diff --git a/src/parser.js b/src/parser.js index a4cbadc..08775ad 100644 --- a/src/parser.js +++ b/src/parser.js @@ -18,10 +18,10 @@ export async function parseFilePromise(config) { const posts = collectPosts(channelData, postTypes, config); const images = []; - if (config.saveAttachedImages) { + if (config.saveImages === 'attached' || config.saveImages === 'all') { images.push(...collectAttachedImages(channelData)); } - if (config.saveScrapedImages) { + if (config.saveImages === 'scraped' || config.saveImages === 'all') { images.push(...collectScrapedImages(channelData, postTypes)); } diff --git a/src/wizard.js b/src/wizard.js index 9e4cfd4..eb24b54 100644 --- a/src/wizard.js +++ b/src/wizard.js @@ -2,8 +2,7 @@ import * as inquirer from '@inquirer/prompts'; import camelcase from 'camelcase'; import chalk from 'chalk'; import * as commander from 'commander'; -import fs from 'fs'; -import path from 'path'; +import * as normalizers from './normalizers.js'; // all user options for command line and wizard are declared here const options = [ @@ -109,37 +108,6 @@ const options = [ } ]; -const validators = { - 'boolean': (value) => { - if (typeof value === 'boolean') { - return value; - } else if (value === 'true') { - return true; - } else if (value === 'false') { - return false; - } - - throw 'Must be true or false.'; - }, - 'file-path': (value) => { - const unwrapped = value.replace(/"(.*?)"/, '$1'); - const absolute = path.resolve(unwrapped); - - let fileExists; - try { - fileExists = fs.existsSync(absolute) && fs.statSync(absolute).isFile(); - } catch (ex) { - fileExists = false; - } - - if (fileExists) { - return absolute; - } else { - throw 'File not found at ' + absolute + '.'; - } - } -}; - export async function getConfig(argv) { const opts = parseCommandLine(argv); @@ -148,6 +116,8 @@ 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 normalizedAnswer = undefined; + const promptConfig = { message: question.description + '?', default: question.default, @@ -167,11 +137,11 @@ export async function getConfig(argv) { promptConfig.choices = question.choices; promptConfig.loop = false; } else { - const validator = validators[question.type]; - if (validator) { + const normalizer = normalizers[camelcase(question.type)]; + if (normalizer) { promptConfig.validate = (value) => { try { - normalized = validator(value); + normalizedAnswer = normalizer(value); } catch (ex) { return ex.toString(); } @@ -181,7 +151,6 @@ export async function getConfig(argv) { } } - let normalized = undefined; let answer = await question.prompt(promptConfig).catch((ex) => { if (ex instanceof Error && ex.name === 'ExitPromptError') { console.log('\nUser quit wizard early.'); @@ -191,9 +160,7 @@ export async function getConfig(argv) { } }); - console.log(normalized, answer); - - answers[camelcase(question.name)] = normalized ?? answer; + answers[camelcase(question.name)] = normalizedAnswer ?? answer; } } else { console.log('\nSkipping wizard...'); @@ -222,13 +189,13 @@ function parseCommandLine() { option.choices(input.choices.map((choice) => choice.value)); } else { option.argParser((value) => { - const validator = validators[input.type]; - if (!validator) { + const normalizer = normalizers[camelcase(input.type)]; + if (!normalizer) { return value; } try { - return validator(value); + return normalizer(value); } catch (ex) { commander.program.error(`error: option '${flag}' argument '${value}' is invalid. ${ex.toString()}`); } diff --git a/src/writer.js b/src/writer.js index 0b4ea54..61d4fcf 100644 --- a/src/writer.js +++ b/src/writer.js @@ -182,11 +182,11 @@ function getPostPath(post, config) { // start with base output dir and post type const pathSegments = [settings.output_directory, post.meta.type]; - if (config.yearFolders) { + if (config.dateFolders === 'year' || config.dateFolders === 'year-month') { pathSegments.push(dt.toFormat('yyyy')); } - if (config.monthFolders) { + if (config.dateFolders === 'year-month') { pathSegments.push(dt.toFormat('LL')); }