copy-file-util 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,14 +1,15 @@
1
1
  # copy-file-util
2
2
  <img src=https://centerkey.com/graphics/center-key-logo.svg align=right width=200 alt=logo>
3
3
 
4
- _Copy or rename a file (CLI tool designed for use in npm scripts)_
4
+ _Copy or rename a file with optional package version number (CLI tool designed for use in npm scripts)_
5
5
 
6
6
  [![License:MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/center-key/copy-file-util/blob/main/LICENSE.txt)
7
7
  [![npm](https://img.shields.io/npm/v/copy-file-util.svg)](https://www.npmjs.com/package/copy-file-util)
8
8
  [![Vulnerabilities](https://snyk.io/test/github/center-key/copy-file-util/badge.svg)](https://snyk.io/test/github/center-key/copy-file-util)
9
9
  [![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)
10
10
 
11
- **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.
11
+ **copy-file-util** takes a source file and copies it to a new destination.&nbsp;
12
+ The command's console output includes a timestamp and formatting helpful in build systems.
12
13
 
13
14
  <img src=https://raw.githubusercontent.com/center-key/copy-file-util/main/screenshot.png
14
15
  width=800 alt=screenshot>
@@ -23,8 +24,9 @@ $ npm install --save-dev copy-file-util
23
24
  ### 1. npm scripts
24
25
  Run `copy-file` from the `"scripts"` section of your **package.json** file.
25
26
 
26
- The **first** parameter is the *source* file.
27
- The **second** parameter is the *target* file or folder (use the `--folder` flag).
27
+ Parameters:
28
+ * The **first** parameter is the *source* file.
29
+ * The **second** parameter is the *target* file or folder (use the `--folder` flag).
28
30
 
29
31
  Example **package.json** scripts:
30
32
  ```json
@@ -33,7 +35,6 @@ Example **package.json** scripts:
33
35
  "backup-license": "copy-file src/LICENSE --folder backup",
34
36
  },
35
37
  ```
36
- Try out the first script with the command: `npm run pub-license`
37
38
 
38
39
  ### 2. Global
39
40
  You can install **copy-file-util** globally and then run it anywhere directly from the terminal.
@@ -56,8 +57,14 @@ Examples:
56
57
  - `copy-file app.js app.mjs --quite` &nbsp; Displays no output.
57
58
  - `copy-file app.js --folder dist` &nbsp; Copies **app.js** into the **dist** folder.
58
59
 
60
+ ### 4. Template Variables
61
+ 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.
62
+
63
+ Example:
64
+ - `copy-file build/app.js dist/app-v{{pkg.version}}.js` &nbsp; Creates a copy of **app.js** named something like **app-v1.2.3.js** based on the version of your project.
65
+
59
66
  ## C) Application Code
60
- Even though **copy-file-util** is primarily intended for build scripts, the package can easily be used in ESM and TypeScript projects.
67
+ Even though **copy-file-util** is primarily intended for build scripts, the package can easily be used programmatically in ESM and TypeScript projects.
61
68
 
62
69
  Example:
63
70
  ``` typescript
@@ -71,10 +78,11 @@ See the **TypeScript Declarations** at the top of [copy-file.ts](copy-file.ts) f
71
78
  <br>
72
79
 
73
80
  ---
74
- **Build Tools**
81
+ **CLI Build Tools**
75
82
  - 🎋 [add-dist-header](https://github.com/center-key/add-dist-header):&nbsp; _Prepend a one-line banner comment (with license notice) to distribution files_
76
- - 📄 [copy-file-util](https://github.com/center-key/copy-file-util):&nbsp; _Copy or rename a file (CLI tool designed for use in npm scripts)_
77
- - 📂 [copy-folder-cli](https://github.com/center-key/copy-folder-cli):&nbsp; _Recursively copy a folder (CLI tool designed for use in npm scripts)_
83
+ - 📄 [copy-file-util](https://github.com/center-key/copy-file-util):&nbsp; _Copy or rename a file with optional package version number_
84
+ - 📂 [copy-folder-util](https://github.com/center-key/copy-folder-util):&nbsp; _Recursively copy files from one folder to another folder_
85
+ - 🔍 [replacer-util](https://github.com/center-key/replacer-util):&nbsp; _Find and replace strings or template outputs in text files_
78
86
  - 🔢 [rev-web-assets](https://github.com/center-key/rev-web-assets):&nbsp; _Revision web asset filenames with cache busting content hash fingerprints_
79
87
  - 🚦 [w3c-html-validator](https://github.com/center-key/w3c-html-validator):&nbsp; _Check the markup validity of HTML files using the W3C validator_
80
88
 
package/bin/cli.js CHANGED
@@ -17,10 +17,12 @@
17
17
  // $ cd copy-file-util
18
18
  // $ npm install
19
19
  // $ npm test
20
- // $ node bin/cli.js --cd=spec/fixtures source/mock.txt --folder target/to-folder
20
+ // $ node bin/cli.js --cd=spec/fixtures source/mock.html --folder target/to-folder
21
+ // $ node bin/cli.js --cd=spec/fixtures source/mock.html target/{{pkg.type}}/{{pkg.name}}-v{{pkg.version}}.html
21
22
 
22
23
  // Imports
23
24
  import { copyFile } from '../dist/copy-file.js';
25
+ import { dna } from 'dna-engine';
24
26
  import chalk from 'chalk';
25
27
  import fs from 'fs';
26
28
  import log from 'fancy-log';
@@ -30,17 +32,18 @@ const validFlags = ['cd', 'folder', 'quiet'];
30
32
  const args = process.argv.slice(2);
31
33
  const flags = args.filter(arg => /^--/.test(arg));
32
34
  const flagMap = Object.fromEntries(flags.map(flag => flag.replace(/^--/, '').split('=')));
35
+ const flagOn = Object.fromEntries(validFlags.map(flag => [flag, flag in flagMap]));
33
36
  const invalidFlag = Object.keys(flagMap).find(key => !validFlags.includes(key));
34
37
  const params = args.filter(arg => !/^--/.test(arg));
35
38
 
36
39
  // Data
37
40
  const source = params[0];
38
41
  const target = params[1];
39
- const mode = { folder: 'folder' in flagMap, quiet: 'quiet' in flagMap };
40
42
 
41
43
  // Utilities
42
- const getPackageVersion = () => !fs.existsSync('package.json') ? 'ERROR' :
43
- JSON.parse(fs.readFileSync('package.json', 'utf-8')).version;
44
+ const readPackage = () => JSON.parse(fs.readFileSync('package.json', 'utf-8'));
45
+ const getPackageField = (match) =>
46
+ dna.util.value({ pkg: readPackage() }, match.replace(/[{}]/g, '')) ?? 'MISSING-FIELD-ERROR';
44
47
 
45
48
  // Reporting
46
49
  const printReport = (result) => {
@@ -54,19 +57,19 @@ const printReport = (result) => {
54
57
 
55
58
  // Copy File
56
59
  const error =
57
- invalidFlag ? 'Invalid flag: ' + invalidFlag :
58
- params.length > 2 ? 'Extraneous parameter: ' + params[2] :
59
- !source ? 'Missing source file.' :
60
- !target && mode.folder ? 'Missing target folder.' :
61
- !target ? 'Missing target file.' :
60
+ invalidFlag ? 'Invalid flag: ' + invalidFlag :
61
+ params.length > 2 ? 'Extraneous parameter: ' + params[2] :
62
+ !source ? 'Missing source file.' :
63
+ !target && flagOn.folder ? 'Missing target folder.' :
64
+ !target ? 'Missing target file.' :
62
65
  null;
63
66
  if (error)
64
67
  throw Error('[copy-file-util] ' + error);
65
- const targetKey = mode.folder ? 'targetFolder' : 'targetFile';
68
+ const targetKey = flagOn.folder ? 'targetFolder' : 'targetFile';
66
69
  const options = {
67
70
  cd: flagMap.cd ?? null,
68
- [targetKey]: target.replace(/{{{pkg.version}}}/, getPackageVersion),
71
+ [targetKey]: target.replace(/{{[^{}]*}}/g, getPackageField),
69
72
  };
70
73
  const result = copyFile.cp(source, options);
71
- if (!mode.quiet)
74
+ if (!flagOn.quiet)
72
75
  printReport(result);
@@ -1,4 +1,4 @@
1
- //! copy-file-util v0.1.3 ~~ https://github.com/center-key/copy-file-util ~~ MIT License
1
+ //! copy-file-util v0.1.4 ~~ https://github.com/center-key/copy-file-util ~~ MIT License
2
2
 
3
3
  export declare type Settings = {
4
4
  cd: string;
package/dist/copy-file.js CHANGED
@@ -1,4 +1,4 @@
1
- //! copy-file-util v0.1.3 ~~ https://github.com/center-key/copy-file-util ~~ MIT License
1
+ //! copy-file-util v0.1.4 ~~ https://github.com/center-key/copy-file-util ~~ MIT License
2
2
 
3
3
  import fs from 'fs';
4
4
  import path from 'path';
@@ -1,4 +1,4 @@
1
- //! copy-file-util v0.1.3 ~~ https://github.com/center-key/copy-file-util ~~ MIT License
1
+ //! copy-file-util v0.1.4 ~~ https://github.com/center-key/copy-file-util ~~ MIT License
2
2
 
3
3
  var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  return (mod && mod.__esModule) ? mod : { "default": mod };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "copy-file-util",
3
- "version": "0.1.3",
4
- "description": "Copy or rename a file (CLI tool designed for use in npm scripts)",
3
+ "version": "0.1.4",
4
+ "description": "Copy or rename a file with optional package version number (CLI tool designed for use in npm scripts)",
5
5
  "license": "MIT",
6
6
  "type": "module",
7
7
  "module": "dist/copy-file.js",
@@ -75,19 +75,20 @@
75
75
  "test": "mocha spec/*.spec.js"
76
76
  },
77
77
  "dependencies": {
78
- "chalk": "~5.0",
78
+ "chalk": "~5.1",
79
+ "dna-engine": "~2.2",
79
80
  "fancy-log": "~2.0",
80
81
  "slash": "~5.0"
81
82
  },
82
83
  "devDependencies": {
83
84
  "@types/fancy-log": "~2.0",
84
85
  "@types/node": "~18.8",
85
- "@typescript-eslint/eslint-plugin": "~5.39",
86
- "@typescript-eslint/parser": "~5.39",
86
+ "@typescript-eslint/eslint-plugin": "~5.40",
87
+ "@typescript-eslint/parser": "~5.40",
87
88
  "add-dist-header": "~0.3",
88
89
  "assert-deep-strict-equal": "~1.0",
89
90
  "cpy-cli": "~4.2",
90
- "eslint": "~8.24",
91
+ "eslint": "~8.25",
91
92
  "jshint": "~2.13",
92
93
  "mocha": "~10.0",
93
94
  "npm-run-all2": "~6.0",