Skip to content

Commit

Permalink
Use cli-argv-util to parse flags
Browse files Browse the repository at this point in the history
  • Loading branch information
dpilafian committed Nov 19, 2022
1 parent c91ac13 commit f8cda61
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 26 deletions.
38 changes: 16 additions & 22 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
},
"dependencies": {
"chalk": "~5.1",
"cli-argv-util": "~0.1",
"fancy-log": "~2.0",
"glob": "~8.0",
"istextorbinary": "~6.0",
Expand All @@ -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"
}
}

0 comments on commit f8cda61

Please sign in to comment.