copy-file-util 1.1.0 → 1.1.2

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
@@ -5,7 +5,6 @@ _Copy or rename a file with optional package version number (CLI tool designed f
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
- [![Vulnerabilities](https://snyk.io/test/github/center-key/copy-file-util/badge.svg)](https://snyk.io/test/github/center-key/copy-file-util)
9
8
  [![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
9
 
11
10
  **copy-file-util** takes a source file and copies it to a new destination. 
@@ -36,14 +35,13 @@ Example **package.json** scripts:
36
35
  },
37
36
  ```
38
37
 
39
- ### 2. Global
40
- You can install **copy-file-util** globally and then run it anywhere directly from the terminal.
41
-
38
+ ### 2. Command-line npx
42
39
  Example terminal commands:
43
40
  ```shell
44
- $ npm install --global copy-file-util
41
+ $ npm install --save-dev copy-file-util
45
42
  $ copy-file src/web/api.html docs/api-manual.html
46
43
  ```
44
+ You can also install **copy-file-util** globally (`--global`) and then run it anywhere directly from the terminal.
47
45
 
48
46
  ### 3. CLI flags
49
47
  Command-line flags:
@@ -51,6 +49,7 @@ Command-line flags:
51
49
  | ---------- | ---------------------------------------------- | ---------- |
52
50
  | `--cd` | Change working directory before starting copy. | **string** |
53
51
  | `--folder` | Indicates the target is a folder. | N/A |
52
+ | `--move` | Delete the source file after copying it. | N/A |
54
53
  | `--note` | Place to add a comment only for humans. | **string** |
55
54
  | `--quiet` | Suppress informational messages. | N/A |
56
55
 
@@ -61,14 +60,18 @@ Examples:
61
60
  - `copy-file app.js --folder dist`<br>
62
61
  Copies **app.js** into the **dist** folder.
63
62
 
63
+ - `copy-file app.js --move --folder dist`<br>
64
+ Like the `mv` Unix command.
65
+
64
66
  ### 4. Template variables
65
67
  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.
66
68
 
67
69
  Example:
68
- - `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.
70
+ - `copy-file build/app.js dist/app-v{{pkg.version}}.js`<br>
71
+ Creates a copy of **app.js** named something like **app-v1.2.3.js** based on the version of your project.
69
72
 
70
73
  ## C) Application Code
71
- Even though **copy-file-util** is primarily intended for build scripts, the package can easily be used programmatically in ESM and TypeScript projects.
74
+ Even though **copy-file-util** is primarily intended for build scripts, the package can be used programmatically in ESM and TypeScript projects.
72
75
 
73
76
  Example:
74
77
  ``` typescript
@@ -83,10 +86,11 @@ See the **TypeScript Declarations** at the top of [copy-file.ts](copy-file.ts) f
83
86
  <br>
84
87
 
85
88
  ---
86
- **CLI Build Tools**
89
+ **CLI Build Tools for package.json**
87
90
  - 🎋 [add-dist-header](https://github.com/center-key/add-dist-header):&nbsp; _Prepend a one-line banner comment (with license notice) to distribution files_
88
91
  - 📄 [copy-file-util](https://github.com/center-key/copy-file-util):&nbsp; _Copy or rename a file with optional package version number_
89
92
  - 📂 [copy-folder-util](https://github.com/center-key/copy-folder-util):&nbsp; _Recursively copy files from one folder to another folder_
93
+ - 🪺 [recursive-exec](https://github.com/center-key/recursive-exec):&nbsp; _Run a command on each file in a folder and its subfolders_
90
94
  - 🔍 [replacer-util](https://github.com/center-key/replacer-util):&nbsp; _Find and replace strings or template outputs in text files_
91
95
  - 🔢 [rev-web-assets](https://github.com/center-key/rev-web-assets):&nbsp; _Revision web asset filenames with cache busting content hash fingerprints_
92
96
  - 🚆 [run-scripts-util](https://github.com/center-key/run-scripts-util):&nbsp; _Organize npm scripts into named groups of easy to manage commands_
package/bin/cli.js CHANGED
@@ -10,7 +10,7 @@
10
10
  // },
11
11
  //
12
12
  // Usage from command line:
13
- // $ npm install --global copy-file-util
13
+ // $ npm install --save-dev copy-file-util
14
14
  // $ copy-file src/LICENSE doc/license.txt
15
15
  //
16
16
  // Contributors to this project:
@@ -24,12 +24,10 @@
24
24
  import { cliArgvUtil } from 'cli-argv-util';
25
25
  import { copyFile } from '../dist/copy-file.js';
26
26
  import { dna } from 'dna-engine';
27
- import chalk from 'chalk';
28
- import fs from 'fs';
29
- import log from 'fancy-log';
27
+ import fs from 'fs';
30
28
 
31
29
  // Parameters and flags
32
- const validFlags = ['cd', 'folder', 'note', 'quiet'];
30
+ const validFlags = ['cd', 'folder', 'move', 'note', 'quiet'];
33
31
  const cli = cliArgvUtil.parse(validFlags);
34
32
  const source = cli.params[0];
35
33
  const target = cli.params[1];
@@ -39,16 +37,6 @@ const readPackage = () => JSON.parse(fs.readFileSync('package.json', 'utf-8'));
39
37
  const getPackageField = (match) =>
40
38
  dna.util.value({ pkg: readPackage() }, match.replace(/[{}]/g, '')) ?? 'MISSING-FIELD-ERROR';
41
39
 
42
- // Reporting
43
- const printReport = (result) => {
44
- const name = chalk.gray('copy-file');
45
- const origin = chalk.blue.bold(result.origin);
46
- const dest = chalk.magenta(result.dest);
47
- const arrow = chalk.gray.bold('→');
48
- const info = chalk.white(`(${result.duration}ms)`);
49
- log(name, origin, arrow, dest, info);
50
- };
51
-
52
40
  // Copy File
53
41
  const error =
54
42
  cli.invalidFlag ? cli.invalidFlagMsg :
@@ -62,8 +50,9 @@ if (error)
62
50
  const targetKey = cli.flagOn.folder ? 'targetFolder' : 'targetFile';
63
51
  const options = {
64
52
  cd: cli.flagMap.cd ?? null,
53
+ move: cli.flagOn.move,
65
54
  [targetKey]: target.replace(/{{[^{}]*}}/g, getPackageField),
66
55
  };
67
56
  const result = copyFile.cp(source, options);
68
57
  if (!cli.flagOn.quiet)
69
- printReport(result);
58
+ copyFile.reporter(result);
@@ -1,18 +1,20 @@
1
- //! copy-file-util v1.1.0 ~~ https://github.com/center-key/copy-file-util ~~ MIT License
1
+ //! copy-file-util v1.1.2 ~~ https://github.com/center-key/copy-file-util ~~ MIT License
2
2
 
3
3
  export type Settings = {
4
4
  cd: string;
5
5
  targetFile: string;
6
6
  targetFolder: string;
7
7
  fileExtension: string;
8
+ move: boolean;
8
9
  };
9
- export type Options = Partial<Settings>;
10
10
  export type Result = {
11
11
  origin: string;
12
12
  dest: string;
13
13
  duration: number;
14
+ moved: boolean;
14
15
  };
15
16
  declare const copyFile: {
16
- cp(sourceFile: string, options: Options): Result;
17
+ cp(sourceFile: string, options?: Partial<Settings>): Result;
18
+ reporter(result: Result): Result;
17
19
  };
18
20
  export { copyFile };
package/dist/copy-file.js CHANGED
@@ -1,18 +1,20 @@
1
- //! copy-file-util v1.1.0 ~~ https://github.com/center-key/copy-file-util ~~ MIT License
1
+ //! copy-file-util v1.1.2 ~~ https://github.com/center-key/copy-file-util ~~ MIT License
2
2
 
3
+ import chalk from 'chalk';
3
4
  import fs from 'fs';
5
+ import log from 'fancy-log';
4
6
  import path from 'path';
5
7
  import slash from 'slash';
6
8
  const copyFile = {
7
9
  cp(sourceFile, options) {
8
- var _a;
9
10
  const defaults = {
10
11
  cd: null,
11
12
  targetFile: null,
12
13
  targetFolder: null,
13
14
  fileExtension: null,
15
+ move: false,
14
16
  };
15
- const settings = Object.assign(Object.assign({}, defaults), options);
17
+ const settings = { ...defaults, ...options };
16
18
  const startTime = Date.now();
17
19
  const missingTarget = !settings.targetFile && !settings.targetFolder;
18
20
  const ambiguousTarget = !!settings.targetFile && !!settings.targetFolder;
@@ -24,7 +26,7 @@ const copyFile = {
24
26
  const sourceFilename = sourceIsFile ? path.basename(source) : null;
25
27
  const targetPath = settings.targetFile ? path.dirname(settings.targetFile) : settings.targetFolder;
26
28
  const targetFolder = targetPath ? normalize(startFolder + targetPath) : null;
27
- const targetFile = (_a = settings.targetFile) !== null && _a !== void 0 ? _a : settings.targetFolder + '/' + sourceFilename;
29
+ const targetFile = settings.targetFile ?? settings.targetFolder + '/' + sourceFilename;
28
30
  const target = normalize(startFolder + targetFile);
29
31
  if (targetFolder)
30
32
  fs.mkdirSync(targetFolder, { recursive: true });
@@ -39,12 +41,25 @@ const copyFile = {
39
41
  null;
40
42
  if (errorMessage)
41
43
  throw Error('[copy-file-util] ' + errorMessage);
42
- fs.copyFileSync(source, target);
44
+ if (settings.move)
45
+ fs.renameSync(source, target);
46
+ else
47
+ fs.copyFileSync(source, target);
43
48
  return {
44
49
  origin: source,
45
50
  dest: target,
51
+ moved: settings.move,
46
52
  duration: Date.now() - startTime,
47
53
  };
48
54
  },
55
+ reporter(result) {
56
+ const name = chalk.gray('copy-file');
57
+ const origin = chalk.blue.bold(result.origin);
58
+ const dest = chalk.magenta(result.dest);
59
+ const arrow = chalk.gray.bold('→');
60
+ const info = chalk.white(`(${result.duration}ms${result.moved ? ', move' : ''})`);
61
+ log(name, origin, arrow, dest, info);
62
+ return result;
63
+ },
49
64
  };
50
65
  export { copyFile };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "copy-file-util",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
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",
@@ -20,7 +20,10 @@
20
20
  "copy-file": "bin/cli.js",
21
21
  "copy-file-util": "bin/cli.js"
22
22
  },
23
- "repository": "github:center-key/copy-file-util",
23
+ "repository": {
24
+ "type": "git",
25
+ "url": "git+https://github.com/center-key/copy-file-util.git"
26
+ },
24
27
  "homepage": "https://github.com/center-key/copy-file-util",
25
28
  "bugs": "https://github.com/center-key/copy-file-util/issues",
26
29
  "docs": "https://github.com/center-key/copy-file-util#readme",
@@ -66,37 +69,38 @@
66
69
  "clean": [
67
70
  "rimraf build dist spec/fixtures/target"
68
71
  ],
69
- "build": [
72
+ "lint": [
70
73
  "jshint . --exclude-path .gitignore",
71
- "eslint --max-warnings 0 . --ext .ts",
74
+ "eslint --max-warnings 0 . --ext .ts"
75
+ ],
76
+ "build": [
72
77
  "tsc",
73
78
  "add-dist-header build dist"
74
79
  ]
75
80
  },
76
81
  "scripts": {
77
- "pretest": "run-scripts clean build",
82
+ "pretest": "run-scripts clean lint build",
78
83
  "test": "mocha spec/*.spec.js"
79
84
  },
80
85
  "dependencies": {
81
- "chalk": "~5.2",
82
- "cli-argv-util": "~1.0",
86
+ "chalk": "~5.3",
87
+ "cli-argv-util": "~1.2",
83
88
  "dna-engine": "~3.0",
84
89
  "fancy-log": "~2.0",
85
90
  "slash": "~5.1"
86
91
  },
87
92
  "devDependencies": {
88
93
  "@types/fancy-log": "~2.0",
89
- "@types/node": "~20.3",
90
- "@typescript-eslint/eslint-plugin": "~5.60",
91
- "@typescript-eslint/parser": "~5.60",
92
- "add-dist-header": "~1.0",
93
- "assert-deep-strict-equal": "~1.0",
94
- "cpy-cli": "~4.2",
95
- "eslint": "~8.43",
94
+ "@types/node": "~20.6",
95
+ "@typescript-eslint/eslint-plugin": "~6.7",
96
+ "@typescript-eslint/parser": "~6.7",
97
+ "add-dist-header": "~1.3",
98
+ "assert-deep-strict-equal": "~1.1",
99
+ "eslint": "~8.50",
96
100
  "jshint": "~2.13",
97
101
  "mocha": "~10.2",
98
102
  "rimraf": "~5.0",
99
- "run-scripts-util": "~1.1",
100
- "typescript": "~5.1"
103
+ "run-scripts-util": "~1.2",
104
+ "typescript": "~5.2"
101
105
  }
102
106
  }