copy-file-util 1.1.2 → 1.2.0
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/LICENSE.txt +1 -1
- package/README.md +19 -10
- package/bin/cli.js +2 -1
- package/dist/copy-file.d.ts +3 -1
- package/dist/copy-file.js +9 -4
- package/package.json +12 -18
package/LICENSE.txt
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2022-
|
|
3
|
+
Copyright (c) 2022-2024 Individual contributors to copy-file-util
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
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)
|
|
@@ -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>
|
|
@@ -60,9 +61,17 @@ Examples:
|
|
|
60
61
|
- `copy-file app.js --folder dist`<br>
|
|
61
62
|
Copies **app.js** into the **dist** folder.
|
|
62
63
|
|
|
64
|
+
- `copy-file 'src/Legal Notice.md' --folder dist`<br>
|
|
65
|
+
Copies a file that has a space in its filename.
|
|
66
|
+
|
|
63
67
|
- `copy-file app.js --move --folder dist`<br>
|
|
64
68
|
Like the `mv` Unix command.
|
|
65
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
|
+
|
|
73
|
+
_**Note:** Single quotes in commands are normalized so they work cross-platform and avoid the errors often encountered on Microsoft Windows._
|
|
74
|
+
|
|
66
75
|
### 4. Template variables
|
|
67
76
|
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.
|
|
68
77
|
|
|
@@ -93,7 +102,7 @@ See the **TypeScript Declarations** at the top of [copy-file.ts](copy-file.ts) f
|
|
|
93
102
|
- 🪺 [recursive-exec](https://github.com/center-key/recursive-exec): _Run a command on each file in a folder and its subfolders_
|
|
94
103
|
- 🔍 [replacer-util](https://github.com/center-key/replacer-util): _Find and replace strings or template outputs in text files_
|
|
95
104
|
- 🔢 [rev-web-assets](https://github.com/center-key/rev-web-assets): _Revision web asset filenames with cache busting content hash fingerprints_
|
|
96
|
-
- 🚆 [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_
|
|
97
106
|
- 🚦 [w3c-html-validator](https://github.com/center-key/w3c-html-validator): _Check the markup validity of HTML files using the W3C validator_
|
|
98
107
|
|
|
99
108
|
Feel free to submit questions at:<br>
|
package/bin/cli.js
CHANGED
|
@@ -27,7 +27,7 @@ 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
33
|
const target = cli.params[1];
|
|
@@ -51,6 +51,7 @@ const targetKey = cli.flagOn.folder ? 'targetFolder' : 'targetFile';
|
|
|
51
51
|
const options = {
|
|
52
52
|
cd: cli.flagMap.cd ?? null,
|
|
53
53
|
move: cli.flagOn.move,
|
|
54
|
+
overwrite: !cli.flagOn.noOverwrite,
|
|
54
55
|
[targetKey]: target.replace(/{{[^{}]*}}/g, getPackageField),
|
|
55
56
|
};
|
|
56
57
|
const result = copyFile.cp(source, options);
|
package/dist/copy-file.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//! copy-file-util v1.
|
|
1
|
+
//! copy-file-util v1.2.0 ~~ 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
|
+
//! copy-file-util v1.2.0 ~~ 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.
|
|
4
|
-
"description": "Copy or rename a file with optional package version number (CLI tool designed for use in npm scripts)",
|
|
3
|
+
"version": "1.2.0",
|
|
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"
|
|
@@ -85,22 +79,22 @@
|
|
|
85
79
|
"dependencies": {
|
|
86
80
|
"chalk": "~5.3",
|
|
87
81
|
"cli-argv-util": "~1.2",
|
|
88
|
-
"dna-engine": "~3.
|
|
82
|
+
"dna-engine": "~3.1",
|
|
89
83
|
"fancy-log": "~2.0",
|
|
90
84
|
"slash": "~5.1"
|
|
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": "~8.
|
|
88
|
+
"@types/node": "~20.11",
|
|
89
|
+
"@typescript-eslint/eslint-plugin": "~7.2",
|
|
90
|
+
"@typescript-eslint/parser": "~7.2",
|
|
91
|
+
"add-dist-header": "~1.4",
|
|
92
|
+
"assert-deep-strict-equal": "~1.2",
|
|
93
|
+
"eslint": "~8.57",
|
|
100
94
|
"jshint": "~2.13",
|
|
101
|
-
"mocha": "~10.
|
|
95
|
+
"mocha": "~10.3",
|
|
102
96
|
"rimraf": "~5.0",
|
|
103
97
|
"run-scripts-util": "~1.2",
|
|
104
|
-
"typescript": "~5.
|
|
98
|
+
"typescript": "~5.4"
|
|
105
99
|
}
|
|
106
100
|
}
|