From fec418ab9a98fc99c4aa80338014a227e0d985a4 Mon Sep 17 00:00:00 2001 From: dpilafian Date: Wed, 6 Sep 2023 17:29:24 -0700 Subject: [PATCH] Move reporter into module for better reuse --- README.md | 2 +- bin/cli.js | 16 ++-------------- copy-file.ts | 12 ++++++++++++ package.json | 10 +++++----- spec/mocha.spec.js | 10 +++++++--- 5 files changed, 27 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index c7e42a6..d931139 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ Example: Creates a copy of **app.js** named something like **app-v1.2.3.js** based on the version of your project. ## C) Application Code -Even though **copy-file-util** is primarily intended for build scripts, the package can easily be used programmatically in ESM and TypeScript projects. +Even though **copy-file-util** is primarily intended for build scripts, the package can be used programmatically in ESM and TypeScript projects. Example: ``` typescript diff --git a/bin/cli.js b/bin/cli.js index 254f734..ff8b28a 100644 --- a/bin/cli.js +++ b/bin/cli.js @@ -24,9 +24,7 @@ import { cliArgvUtil } from 'cli-argv-util'; import { copyFile } from '../dist/copy-file.js'; import { dna } from 'dna-engine'; -import chalk from 'chalk'; -import fs from 'fs'; -import log from 'fancy-log'; +import fs from 'fs'; // Parameters and flags const validFlags = ['cd', 'folder', 'move', 'note', 'quiet']; @@ -39,16 +37,6 @@ const readPackage = () => JSON.parse(fs.readFileSync('package.json', 'utf-8')); const getPackageField = (match) => dna.util.value({ pkg: readPackage() }, match.replace(/[{}]/g, '')) ?? 'MISSING-FIELD-ERROR'; -// Reporting -const printReport = (result) => { - const name = chalk.gray('copy-file'); - const origin = chalk.blue.bold(result.origin); - const dest = chalk.magenta(result.dest); - const arrow = chalk.gray.bold('→'); - const info = chalk.white(`(${result.duration}ms${result.moved ? ', move' : ''})`); - log(name, origin, arrow, dest, info); - }; - // Copy File const error = cli.invalidFlag ? cli.invalidFlagMsg : @@ -67,4 +55,4 @@ const options = { }; const result = copyFile.cp(source, options); if (!cli.flagOn.quiet) - printReport(result); + copyFile.reporter(result); diff --git a/copy-file.ts b/copy-file.ts index 59c33a7..24a0f5e 100644 --- a/copy-file.ts +++ b/copy-file.ts @@ -1,7 +1,9 @@ // copy-file-util ~~ MIT License // Imports +import chalk from 'chalk'; import fs from 'fs'; +import log from 'fancy-log'; import path from 'path'; import slash from 'slash'; @@ -71,6 +73,16 @@ const copyFile = { }; }, + reporter(result: Result): Result { + const name = chalk.gray('copy-file'); + const origin = chalk.blue.bold(result.origin); + const dest = chalk.magenta(result.dest); + const arrow = chalk.gray.bold('→'); + const info = chalk.white(`(${result.duration}ms${result.moved ? ', move' : ''})`); + log(name, origin, arrow, dest, info); + return result; + }, + }; export { copyFile }; diff --git a/package.json b/package.json index 2635c8e..8424125 100644 --- a/package.json +++ b/package.json @@ -89,15 +89,15 @@ "devDependencies": { "@types/fancy-log": "~2.0", "@types/node": "~20.5", - "@typescript-eslint/eslint-plugin": "~6.4", - "@typescript-eslint/parser": "~6.4", - "add-dist-header": "~1.2", + "@typescript-eslint/eslint-plugin": "~6.6", + "@typescript-eslint/parser": "~6.6", + "add-dist-header": "~1.3", "assert-deep-strict-equal": "~1.1", - "eslint": "~8.47", + "eslint": "~8.48", "jshint": "~2.13", "mocha": "~10.2", "rimraf": "~5.0", "run-scripts-util": "~1.2", - "typescript": "~5.1" + "typescript": "~5.2" } } diff --git a/spec/mocha.spec.js b/spec/mocha.spec.js index 8f01229..e9e5cd2 100644 --- a/spec/mocha.spec.js +++ b/spec/mocha.spec.js @@ -34,9 +34,13 @@ describe('Library module', () => { assertDeepStrictEqual(actual, expected); }); - it('has a cp() function', () => { - const actual = { validate: typeof copyFile.cp }; - const expected = { validate: 'function' }; + it('has functions named cp() and reporter()', () => { + const module = copyFile; + const actual = Object.keys(module).sort().map(key => [key, typeof module[key]]); + const expected = [ + ['cp', 'function'], + ['reporter', 'function'], + ]; assertDeepStrictEqual(actual, expected); });