Skip to content

Commit

Permalink
Change "pkg" template to "package" for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
dpilafian committed Apr 18, 2024
1 parent 5fc834c commit e17b4b3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ _Copy or rename a file with optional package version number (CLI tool designed f

[![License:MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/center-key/copy-file-util/blob/main/LICENSE.txt)
[![npm](https://img.shields.io/npm/v/copy-file-util.svg)](https://www.npmjs.com/package/copy-file-util)
[![Build](https://github.com/center-key/copy-file-util/workflows/build/badge.svg)](https://github.com/center-key/copy-file-util/actions/workflows/run-spec-on-push.yaml)
[![Build](https://github.com/center-key/copy-file-util/actions/workflows/run-spec-on-push.yaml/badge.svg)](https://github.com/center-key/copy-file-util/actions/workflows/run-spec-on-push.yaml)

**copy-file-util** takes a source file and copies it to a new destination. 
The command's console output includes a timestamp and formatting helpful in build systems.
Expand Down Expand Up @@ -73,10 +73,10 @@ Examples:
_**Note:** Single quotes in commands are normalized so they work cross-platform and avoid the errors often encountered on Microsoft Windows._

### 4. Template variables
The *target* parameter can contain template variables, like `{{pkg.version}}` and `{{pkg.name}}`, which will be replaced with values with values from your project's **package.json** file.
The *target* parameter can contain template variables, like `{{package.version}}` and `{{package.name}}`, which will be replaced with values with values from your project's **package.json** file.

Example:
- `copy-file build/app.js dist/app-v{{pkg.version}}.js`<br>
- `copy-file build/app.js dist/app-v{{package.version}}.js`<br>
Creates a copy of **app.js** named something like **app-v1.2.3.js** based on the version of your project.

## C) Application Code
Expand Down
12 changes: 7 additions & 5 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// $ npm install
// $ npm test
// $ node bin/cli.js --cd=spec/fixtures source/mock.html --folder target/to-folder
// $ node bin/cli.js --cd=spec/fixtures source/mock.html target/{{pkg.type}}/{{pkg.name}}-v{{pkg.version}}.html
// $ node bin/cli.js --cd=spec/fixtures source/mock.html target/{{package.type}}/{{package.name}}-v{{package.version}}.html

// Imports
import { cliArgvUtil } from 'cli-argv-util';
Expand All @@ -30,12 +30,13 @@ import fs from 'fs';
const validFlags = ['cd', 'folder', 'move', 'no-overwrite', 'note', 'quiet'];
const cli = cliArgvUtil.parse(validFlags);
const source = cli.params[0];
const target = cli.params[1];
//const target = cli.params[1];
const target = cli.params[1].replaceAll('{{pkg.', '{{package.'); //name "pkg" deprecated in favor of "package" for clarity

// Utilities
const readPackage = () => JSON.parse(fs.readFileSync('package.json', 'utf-8'));
const getPackageField = (match) =>
dna.util.value({ pkg: readPackage() }, match.replace(/[{}]/g, '')) ?? 'MISSING-FIELD-ERROR';
const getPackageField = (match) => //example: '{{package.version}}' --> '3.1.4'
dna.util.value({ package: readPackage() }, match.replace(/[{}]/g, '')) ?? 'MISSING-FIELD-ERROR';

// Copy File
const error =
Expand All @@ -48,11 +49,12 @@ const error =
if (error)
throw Error('[copy-file-util] ' + error);
const targetKey = cli.flagOn.folder ? 'targetFolder' : 'targetFile';
const templateVariables = /{{[^{}]*}}/g; //example match: "{{package.version}}"
const options = {
cd: cli.flagMap.cd ?? null,
move: cli.flagOn.move,
overwrite: !cli.flagOn.noOverwrite,
[targetKey]: target.replace(/{{[^{}]*}}/g, getPackageField),
[targetKey]: target.replace(templateVariables, getPackageField),
};
const result = copyFile.cp(source, options);
if (!cli.flagOn.quiet)
Expand Down
2 changes: 1 addition & 1 deletion spec/mocha.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ describe('Executing the CLI', () => {
const run = (posix) => cliArgvUtil.run(pkg, posix);

it('with template variables correctly inserts values from "package.json"', () => {
run('copy-file --cd=spec/fixtures source/mock.html target/{{pkg.type}}/{{pkg.name}}-v{{pkg.version}}.html');
run('copy-file --cd=spec/fixtures source/mock.html target/{{package.type}}/{{package.name}}-v{{package.version}}.html');
const actual = fs.readdirSync('spec/fixtures/target/module');
const expected = ['copy-file-util-v' + pkg.version + '.html'];
assertDeepStrictEqual(actual, expected);
Expand Down

0 comments on commit e17b4b3

Please sign in to comment.