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 +17 -9
- package/bin/cli.js +15 -12
- package/dist/copy-file.d.ts +1 -1
- package/dist/copy-file.js +1 -1
- package/dist/copy-file.umd.cjs +1 -1
- package/package.json +7 -6
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
|
[](https://github.com/center-key/copy-file-util/blob/main/LICENSE.txt)
|
|
7
7
|
[](https://www.npmjs.com/package/copy-file-util)
|
|
8
8
|
[](https://snyk.io/test/github/center-key/copy-file-util)
|
|
9
9
|
[](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
|
|
11
|
+
**copy-file-util** takes a source file and copies it to a new destination.
|
|
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
|
-
|
|
27
|
-
The **
|
|
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` Displays no output.
|
|
57
58
|
- `copy-file app.js --folder dist` 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` 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): _Prepend a one-line banner comment (with license notice) to distribution files_
|
|
76
|
-
- 📄 [copy-file-util](https://github.com/center-key/copy-file-util): _Copy or rename a file
|
|
77
|
-
- 📂 [copy-folder-
|
|
83
|
+
- 📄 [copy-file-util](https://github.com/center-key/copy-file-util): _Copy or rename a file with optional package version number_
|
|
84
|
+
- 📂 [copy-folder-util](https://github.com/center-key/copy-folder-util): _Recursively copy files from one folder to another folder_
|
|
85
|
+
- 🔍 [replacer-util](https://github.com/center-key/replacer-util): _Find and replace strings or template outputs in text files_
|
|
78
86
|
- 🔢 [rev-web-assets](https://github.com/center-key/rev-web-assets): _Revision web asset filenames with cache busting content hash fingerprints_
|
|
79
87
|
- 🚦 [w3c-html-validator](https://github.com/center-key/w3c-html-validator): _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.
|
|
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
|
|
43
|
-
|
|
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 ?
|
|
58
|
-
params.length > 2 ?
|
|
59
|
-
!source ?
|
|
60
|
-
!target &&
|
|
61
|
-
!target ?
|
|
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 =
|
|
68
|
+
const targetKey = flagOn.folder ? 'targetFolder' : 'targetFile';
|
|
66
69
|
const options = {
|
|
67
70
|
cd: flagMap.cd ?? null,
|
|
68
|
-
[targetKey]: target.replace(/{{{
|
|
71
|
+
[targetKey]: target.replace(/{{[^{}]*}}/g, getPackageField),
|
|
69
72
|
};
|
|
70
73
|
const result = copyFile.cp(source, options);
|
|
71
|
-
if (!
|
|
74
|
+
if (!flagOn.quiet)
|
|
72
75
|
printReport(result);
|
package/dist/copy-file.d.ts
CHANGED
package/dist/copy-file.js
CHANGED
package/dist/copy-file.umd.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//! copy-file-util v0.1.
|
|
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.
|
|
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.
|
|
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.
|
|
86
|
-
"@typescript-eslint/parser": "~5.
|
|
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.
|
|
91
|
+
"eslint": "~8.25",
|
|
91
92
|
"jshint": "~2.13",
|
|
92
93
|
"mocha": "~10.0",
|
|
93
94
|
"npm-run-all2": "~6.0",
|