diff --git a/README.md b/README.md index 26fec3f..5e30014 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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`
+ - `copy-file build/app.js dist/app-v{{package.version}}.js`
Creates a copy of **app.js** named something like **app-v1.2.3.js** based on the version of your project. ## C) Application Code diff --git a/bin/cli.js b/bin/cli.js index 4dc47ee..0748204 100644 --- a/bin/cli.js +++ b/bin/cli.js @@ -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'; @@ -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 = @@ -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) diff --git a/spec/mocha.spec.js b/spec/mocha.spec.js index a3371b9..bedec5d 100644 --- a/spec/mocha.spec.js +++ b/spec/mocha.spec.js @@ -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);