copy-file-util 1.1.3 → 1.2.1
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 -13
- package/bin/cli.js +9 -6
- package/dist/copy-file.d.ts +3 -1
- package/dist/copy-file.js +9 -4
- package/package.json +11 -17
package/README.md
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
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 with optional package version number (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 package.json 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
|
-
[](https://github.com/center-key/copy-file-util/actions/workflows/run-spec-on-push.yaml)
|
|
9
9
|
|
|
10
10
|
**copy-file-util** takes a source file and copies it to a new destination.
|
|
11
11
|
The command's console output includes a timestamp and formatting helpful in build systems.
|
|
@@ -20,7 +20,7 @@ $ npm install --save-dev copy-file-util
|
|
|
20
20
|
```
|
|
21
21
|
|
|
22
22
|
## B) Usage
|
|
23
|
-
### 1. npm scripts
|
|
23
|
+
### 1. npm package.json scripts
|
|
24
24
|
Run `copy-file` from the `"scripts"` section of your **package.json** file.
|
|
25
25
|
|
|
26
26
|
Parameters:
|
|
@@ -45,13 +45,14 @@ You can also install **copy-file-util** globally (`--global`) and then run it an
|
|
|
45
45
|
|
|
46
46
|
### 3. CLI flags
|
|
47
47
|
Command-line flags:
|
|
48
|
-
| Flag
|
|
49
|
-
|
|
|
50
|
-
| `--cd`
|
|
51
|
-
| `--folder`
|
|
52
|
-
| `--move`
|
|
53
|
-
| `--note`
|
|
54
|
-
| `--quiet`
|
|
48
|
+
| Flag | Description | Values |
|
|
49
|
+
| ---------------- | ---------------------------------------------- | ---------- |
|
|
50
|
+
| `--cd` | Change working directory before starting copy. | **string** |
|
|
51
|
+
| `--folder` | Indicates the target is a folder. | N/A |
|
|
52
|
+
| `--move` | Delete the source file after copying it. | N/A |
|
|
53
|
+
| `--note` | Place to add a comment only for humans. | **string** |
|
|
54
|
+
| `--quiet` | Suppress informational messages. | N/A |
|
|
55
|
+
| `--no-overwrite` | Abort if target file already exists. | N/A |
|
|
55
56
|
|
|
56
57
|
Examples:
|
|
57
58
|
- `copy-file app.js app.mjs --quiet`<br>
|
|
@@ -66,13 +67,16 @@ Examples:
|
|
|
66
67
|
- `copy-file app.js --move --folder dist`<br>
|
|
67
68
|
Like the `mv` Unix command.
|
|
68
69
|
|
|
70
|
+
- `copy-file default-config.json settings/config.json --no-overwrite`<br>
|
|
71
|
+
Performs a safe copy that aborts if the **settings/config.json** file already exists.
|
|
72
|
+
|
|
69
73
|
_**Note:** Single quotes in commands are normalized so they work cross-platform and avoid the errors often encountered on Microsoft Windows._
|
|
70
74
|
|
|
71
75
|
### 4. Template variables
|
|
72
|
-
The *target* parameter can contain template variables, like `{{
|
|
76
|
+
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.
|
|
73
77
|
|
|
74
78
|
Example:
|
|
75
|
-
- `copy-file build/app.js dist/app-v{{
|
|
79
|
+
- `copy-file build/app.js dist/app-v{{package.version}}.js`<br>
|
|
76
80
|
Creates a copy of **app.js** named something like **app-v1.2.3.js** based on the version of your project.
|
|
77
81
|
|
|
78
82
|
## C) Application Code
|
|
@@ -98,7 +102,7 @@ See the **TypeScript Declarations** at the top of [copy-file.ts](copy-file.ts) f
|
|
|
98
102
|
- 🪺 [recursive-exec](https://github.com/center-key/recursive-exec): _Run a command on each file in a folder and its subfolders_
|
|
99
103
|
- 🔍 [replacer-util](https://github.com/center-key/replacer-util): _Find and replace strings or template outputs in text files_
|
|
100
104
|
- 🔢 [rev-web-assets](https://github.com/center-key/rev-web-assets): _Revision web asset filenames with cache busting content hash fingerprints_
|
|
101
|
-
- 🚆 [run-scripts-util](https://github.com/center-key/run-scripts-util): _Organize npm scripts into named groups of easy to manage commands_
|
|
105
|
+
- 🚆 [run-scripts-util](https://github.com/center-key/run-scripts-util): _Organize npm package.json scripts into named groups of easy to manage commands_
|
|
102
106
|
- 🚦 [w3c-html-validator](https://github.com/center-key/w3c-html-validator): _Check the markup validity of HTML files using the W3C validator_
|
|
103
107
|
|
|
104
108
|
Feel free to submit questions at:<br>
|
package/bin/cli.js
CHANGED
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
// $ npm install
|
|
19
19
|
// $ npm test
|
|
20
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/{{
|
|
21
|
+
// $ node bin/cli.js --cd=spec/fixtures source/mock.html target/{{package.type}}/{{package.name}}-v{{package.version}}.html
|
|
22
22
|
|
|
23
23
|
// Imports
|
|
24
24
|
import { cliArgvUtil } from 'cli-argv-util';
|
|
@@ -27,15 +27,16 @@ import { dna } from 'dna-engine';
|
|
|
27
27
|
import fs from 'fs';
|
|
28
28
|
|
|
29
29
|
// Parameters and flags
|
|
30
|
-
const validFlags = ['cd', 'folder', 'move', 'note', 'quiet'];
|
|
30
|
+
const validFlags = ['cd', 'folder', 'move', 'no-overwrite', 'note', 'quiet'];
|
|
31
31
|
const cli = cliArgvUtil.parse(validFlags);
|
|
32
32
|
const source = cli.params[0];
|
|
33
|
-
const target = cli.params[1];
|
|
33
|
+
//const target = cli.params[1];
|
|
34
|
+
const target = cli.params[1].replaceAll('{{pkg.', '{{package.'); //name "pkg" deprecated in favor of "package" for clarity
|
|
34
35
|
|
|
35
36
|
// Utilities
|
|
36
37
|
const readPackage = () => JSON.parse(fs.readFileSync('package.json', 'utf-8'));
|
|
37
|
-
const getPackageField = (match) =>
|
|
38
|
-
dna.util.value({
|
|
38
|
+
const getPackageField = (match) => //example: '{{package.version}}' --> '3.1.4'
|
|
39
|
+
dna.util.value({ package: readPackage() }, match.replace(/[{}]/g, '')) ?? 'MISSING-FIELD-ERROR';
|
|
39
40
|
|
|
40
41
|
// Copy File
|
|
41
42
|
const error =
|
|
@@ -48,10 +49,12 @@ const error =
|
|
|
48
49
|
if (error)
|
|
49
50
|
throw Error('[copy-file-util] ' + error);
|
|
50
51
|
const targetKey = cli.flagOn.folder ? 'targetFolder' : 'targetFile';
|
|
52
|
+
const templateVariables = /{{[^{}]*}}/g; //example match: "{{package.version}}"
|
|
51
53
|
const options = {
|
|
52
54
|
cd: cli.flagMap.cd ?? null,
|
|
53
55
|
move: cli.flagOn.move,
|
|
54
|
-
|
|
56
|
+
overwrite: !cli.flagOn.noOverwrite,
|
|
57
|
+
[targetKey]: target.replace(templateVariables, getPackageField),
|
|
55
58
|
};
|
|
56
59
|
const result = copyFile.cp(source, options);
|
|
57
60
|
if (!cli.flagOn.quiet)
|
package/dist/copy-file.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//! copy-file-util v1.1
|
|
1
|
+
//! copy-file-util v1.2.1 ~~ https://github.com/center-key/copy-file-util ~~ MIT License
|
|
2
2
|
|
|
3
3
|
export type Settings = {
|
|
4
4
|
cd: string;
|
|
@@ -6,12 +6,14 @@ export type Settings = {
|
|
|
6
6
|
targetFolder: string;
|
|
7
7
|
fileExtension: string;
|
|
8
8
|
move: boolean;
|
|
9
|
+
overwrite: boolean;
|
|
9
10
|
};
|
|
10
11
|
export type Result = {
|
|
11
12
|
origin: string;
|
|
12
13
|
dest: string;
|
|
13
14
|
duration: number;
|
|
14
15
|
moved: boolean;
|
|
16
|
+
skipped: boolean;
|
|
15
17
|
};
|
|
16
18
|
declare const copyFile: {
|
|
17
19
|
cp(sourceFile: string, options?: Partial<Settings>): Result;
|
package/dist/copy-file.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//! copy-file-util v1.1
|
|
1
|
+
//! copy-file-util v1.2.1 ~~ https://github.com/center-key/copy-file-util ~~ MIT License
|
|
2
2
|
|
|
3
3
|
import chalk from 'chalk';
|
|
4
4
|
import fs from 'fs';
|
|
@@ -13,6 +13,7 @@ const copyFile = {
|
|
|
13
13
|
targetFolder: null,
|
|
14
14
|
fileExtension: null,
|
|
15
15
|
move: false,
|
|
16
|
+
overwrite: true,
|
|
16
17
|
};
|
|
17
18
|
const settings = { ...defaults, ...options };
|
|
18
19
|
const startTime = Date.now();
|
|
@@ -28,6 +29,8 @@ const copyFile = {
|
|
|
28
29
|
const targetFolder = targetPath ? normalize(startFolder + targetPath) : null;
|
|
29
30
|
const targetFile = settings.targetFile ?? settings.targetFolder + '/' + sourceFilename;
|
|
30
31
|
const target = normalize(startFolder + targetFile);
|
|
32
|
+
const targetExists = !missingTarget && fs.existsSync(target);
|
|
33
|
+
const skip = targetExists && !settings.overwrite;
|
|
31
34
|
if (targetFolder)
|
|
32
35
|
fs.mkdirSync(targetFolder, { recursive: true });
|
|
33
36
|
const badTargetFolder = !targetFolder || !fs.existsSync(targetFolder);
|
|
@@ -41,14 +44,15 @@ const copyFile = {
|
|
|
41
44
|
null;
|
|
42
45
|
if (errorMessage)
|
|
43
46
|
throw Error('[copy-file-util] ' + errorMessage);
|
|
44
|
-
if (settings.move)
|
|
47
|
+
if (!skip && settings.move)
|
|
45
48
|
fs.renameSync(source, target);
|
|
46
|
-
else
|
|
49
|
+
else if (!skip)
|
|
47
50
|
fs.copyFileSync(source, target);
|
|
48
51
|
return {
|
|
49
52
|
origin: source,
|
|
50
53
|
dest: target,
|
|
51
54
|
moved: settings.move,
|
|
55
|
+
skipped: skip,
|
|
52
56
|
duration: Date.now() - startTime,
|
|
53
57
|
};
|
|
54
58
|
},
|
|
@@ -57,7 +61,8 @@ const copyFile = {
|
|
|
57
61
|
const origin = chalk.blue.bold(result.origin);
|
|
58
62
|
const dest = chalk.magenta(result.dest);
|
|
59
63
|
const arrow = chalk.gray.bold('→');
|
|
60
|
-
const
|
|
64
|
+
const status = result.skipped ? ', skip -- target exists' : result.moved ? ', move' : '';
|
|
65
|
+
const info = chalk.white(`(${result.duration}ms${status})`);
|
|
61
66
|
log(name, origin, arrow, dest, info);
|
|
62
67
|
return result;
|
|
63
68
|
},
|
package/package.json
CHANGED
|
@@ -1,21 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "copy-file-util",
|
|
3
|
-
"version": "1.1
|
|
4
|
-
"description": "Copy or rename a file with optional package version number (CLI tool designed for use in npm scripts)",
|
|
3
|
+
"version": "1.2.1",
|
|
4
|
+
"description": "Copy or rename a file with optional package version number (CLI tool designed for use in npm package.json scripts)",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"module": "dist/copy-file.js",
|
|
8
|
-
"main": "dist/copy-file.js",
|
|
9
8
|
"types": "dist/copy-file.d.ts",
|
|
9
|
+
"exports": "./dist/copy-file.js",
|
|
10
10
|
"files": [
|
|
11
11
|
"dist"
|
|
12
12
|
],
|
|
13
|
-
"exports": {
|
|
14
|
-
".": {
|
|
15
|
-
"import": "./dist/copy-file.js"
|
|
16
|
-
},
|
|
17
|
-
"./": "./dist/"
|
|
18
|
-
},
|
|
19
13
|
"bin": {
|
|
20
14
|
"copy-file": "bin/cli.js",
|
|
21
15
|
"copy-file-util": "bin/cli.js"
|
|
@@ -91,16 +85,16 @@
|
|
|
91
85
|
},
|
|
92
86
|
"devDependencies": {
|
|
93
87
|
"@types/fancy-log": "~2.0",
|
|
94
|
-
"@types/node": "~20.
|
|
95
|
-
"@typescript-eslint/eslint-plugin": "~
|
|
96
|
-
"@typescript-eslint/parser": "~
|
|
97
|
-
"add-dist-header": "~1.
|
|
98
|
-
"assert-deep-strict-equal": "~1.
|
|
99
|
-
"eslint": "
|
|
88
|
+
"@types/node": "~20.12",
|
|
89
|
+
"@typescript-eslint/eslint-plugin": "~7.7",
|
|
90
|
+
"@typescript-eslint/parser": "~7.7",
|
|
91
|
+
"add-dist-header": "~1.4",
|
|
92
|
+
"assert-deep-strict-equal": "~1.2",
|
|
93
|
+
"eslint": "8.57.0",
|
|
100
94
|
"jshint": "~2.13",
|
|
101
|
-
"mocha": "~10.
|
|
95
|
+
"mocha": "~10.4",
|
|
102
96
|
"rimraf": "~5.0",
|
|
103
97
|
"run-scripts-util": "~1.2",
|
|
104
|
-
"typescript": "~5.
|
|
98
|
+
"typescript": "~5.4"
|
|
105
99
|
}
|
|
106
100
|
}
|