From f8cda616f371bf83ba5ce3e648417e294e0546f5 Mon Sep 17 00:00:00 2001 From: dpilafian Date: Sat, 19 Nov 2022 02:20:03 -0800 Subject: [PATCH] Use cli-argv-util to parse flags --- bin/cli.js | 38 ++++++++++++++++---------------------- package.json | 9 +++++---- 2 files changed, 21 insertions(+), 26 deletions(-) diff --git a/bin/cli.js b/bin/cli.js index de2a15c..1d7412f 100644 --- a/bin/cli.js +++ b/bin/cli.js @@ -20,56 +20,50 @@ // $ node bin/cli.js --cd=spec/fixtures source target/cd --summary // Imports +import { cliArgvUtil } from 'cli-argv-util'; import { imgSrcPlaceholder } from '../dist/img-src-placeholder.js'; import chalk from 'chalk'; import fs from 'fs'; import log from 'fancy-log'; import path from 'path'; -// Parameters -const validFlags = ['cd', 'ext', 'note', 'quiet', 'summary']; -const args = process.argv.slice(2); -const flags = args.filter(arg => /^--/.test(arg)); -const flagMap = Object.fromEntries(flags.map(flag => flag.replace(/^--/, '').split('='))); -const flagOn = Object.fromEntries(validFlags.map(flag => [flag, flag in flagMap])); -const invalidFlag = Object.keys(flagMap).find(key => !validFlags.includes(key)); -const params = args.filter(arg => !/^--/.test(arg)); - -// Data -const source = params[0]; //origin file or folder -const target = params[1]; //destination folder +// Parameters and flags +const validFlags = ['cd', 'ext', 'note', 'quiet', 'summary']; +const cli = cliArgvUtil.parse(validFlags); +const source = cli.params[0]; //origin file or folder +const target = cli.params[1]; //destination folder // Reporting const printReport = (results) => { const name = chalk.gray('img-src-placeholder'); const source = chalk.blue.bold(results.source); const target = chalk.magenta(results.target); - const arrow = { big: chalk.gray.bold('➤➤➤'), little: chalk.gray.bold(' ⟹ ') }; //extra space for alignment + const arrow = { big: chalk.gray.bold(' ⟹ '), little: chalk.gray.bold('→') }; const infoColor = results.count ? chalk.white : chalk.red.bold; const info = infoColor(`(files: ${results.count}, ${results.duration}ms)`); const logFile = (file) => log(name, chalk.white(file.origin), arrow.little, chalk.green(file.dest)); log(name, source, arrow.big, target, info); - if (!flagOn.summary) + if (!cli.flagOn.summary) results.files.forEach(logFile); }; // Transform Files const error = - invalidFlag ? 'Invalid flag: ' + invalidFlag : - !source ? 'Missing source folder.' : - !target ? 'Missing target folder.' : - params.length > 2 ? 'Extraneous parameter: ' + params[2] : + cli.invalidFlag ? cli.invalidFlagMsg : + !source ? 'Missing source folder.' : + !target ? 'Missing target folder.' : + cli.paramsCount > 2 ? 'Extraneous parameter: ' + cli.params[2] : null; if (error) throw Error('[img-src-placeholder] ' + error); -const sourceFile = path.join(flagMap.cd ?? '', source); +const sourceFile = path.join(cli.flagMap.cd ?? '', source); const isFile = fs.existsSync(sourceFile) && fs.statSync(sourceFile).isFile(); const sourceFolder = isFile ? path.dirname(source) : source; const options = { - cd: flagMap.cd ?? null, - extensions: flagMap.ext?.split(',') ?? [], + cd: cli.flagMap.cd ?? null, + extensions: cli.flagMap.ext?.split(',') ?? [], filename: isFile ? path.basename(source) : null, }; const results = imgSrcPlaceholder.transform(sourceFolder, target, options); -if (!flagOn.quiet) +if (!cli.flagOn.quiet) printReport(results); diff --git a/package.json b/package.json index e44f1aa..979546a 100644 --- a/package.json +++ b/package.json @@ -83,6 +83,7 @@ }, "dependencies": { "chalk": "~5.1", + "cli-argv-util": "~0.1", "fancy-log": "~2.0", "glob": "~8.0", "istextorbinary": "~6.0", @@ -92,18 +93,18 @@ "devDependencies": { "@types/fancy-log": "~2.0", "@types/node": "~18.11", - "@typescript-eslint/eslint-plugin": "~5.42", - "@typescript-eslint/parser": "~5.42", + "@typescript-eslint/eslint-plugin": "~5.43", + "@typescript-eslint/parser": "~5.43", "add-dist-header": "~0.3", "assert-deep-strict-equal": "~1.0", "copy-file-util": "~0.1", - "eslint": "~8.27", + "eslint": "~8.28", "jshint": "~2.13", "mocha": "~10.1", "rev-web-assets": "~0.1", "rimraf": "~3.0", "run-scripts-util": "~0.1", - "typescript": "~4.8", + "typescript": "~4.9", "w3c-html-validator": "~1.2" } }