Skip to content

Commit

Permalink
Move reporter into module for better reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
dpilafian committed Sep 7, 2023
1 parent 22147d6 commit fec418a
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 2 additions & 14 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand All @@ -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 :
Expand All @@ -67,4 +55,4 @@ const options = {
};
const result = copyFile.cp(source, options);
if (!cli.flagOn.quiet)
printReport(result);
copyFile.reporter(result);
12 changes: 12 additions & 0 deletions copy-file.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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 };
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
10 changes: 7 additions & 3 deletions spec/mocha.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down

0 comments on commit fec418a

Please sign in to comment.