copy-file-util 0.0.2 → 0.1.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 CHANGED
@@ -1,39 +1,36 @@
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
- _A file copy and rename cli tool designed for use in npm scripts_
4
+ _Copy or rename a file (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** copies a folder and its subfolders.
11
+ **copy-file-util** takes a source file and copies it to a new destination. The console output includes a timestamp and formatting helpful in build systems.
12
12
 
13
13
  ## A) Setup
14
-
15
14
  Install package for node:
16
15
  ```shell
17
16
  $ npm install --save-dev copy-file-util
18
17
  ```
19
18
 
20
19
  ## B) Usage
21
-
22
20
  ### 1. npm scripts
23
- Call `copy-file` from the `"scripts"` section of your **package.json** file.
21
+ Run `copy-file` from the `"scripts"` section of your **package.json** file.
24
22
 
25
23
  The **first** parameter is the *source* file.
26
- The **second** parameter is the *target* file or folder (use the `--fodler` flag).
24
+ The **second** parameter is the *target* file or folder (use the `--folder` flag).
27
25
 
28
- Example **package.json** script:
26
+ Example **package.json** scripts:
29
27
  ```json
30
28
  "scripts": {
31
29
  "pub-license": "copy-file src/LICENSE doc/license.txt",
32
30
  "backup-license": "copy-file src/LICENSE --folder backup",
33
31
  },
34
32
  ```
35
-
36
- Try out the script with the command: `npm run pub-license`
33
+ Try out the first script with the command: `npm run pub-license`
37
34
 
38
35
  ### 2. Global
39
36
  You can install **copy-file-util** globally and then run it anywhere directly from the terminal.
@@ -44,34 +41,39 @@ $ npm install --global copy-file-util
44
41
  $ copy-file src/web/api.html docs/api-manual.html
45
42
  ```
46
43
 
47
- ### 3. ESM and TypeScript Code
44
+ ### 3. CLI Flags
45
+ Command-line flags:
46
+ | Flag | Description | Values | Default |
47
+ | ---------- | --------------------------------- | ------ | ------- |
48
+ | `--folder` | Indicates the target is a folder. | N/A | N/A |
49
+ | `--quiet` | Suppress informational messages. | N/A | N/A |
50
+
51
+ Examples:
52
+ - `copy-file app.js app.mjs --quite` &nbsp; Displays no output.
53
+ - `copy-file app.js --folder dist` &nbsp; Copies **app.js** into the **dist** folder.
54
+
55
+ ## C) Application Code
48
56
  Even though **copy-file-util** is primarily intended for build scripts, the package can easily be used in ESM and TypeScript projects.
49
57
 
58
+ Example:
50
59
  ``` typescript
51
60
  import { copyFile } from 'copy-file-util';
52
61
  const result = copyFile.cp('src/web/api.html' { targetFile: 'docs/api-manual.html' });
53
62
  console.log('Execution time:', result.duration, 'ms');
54
63
  ```
55
64
 
56
- See the **TypeScript Declaration File** file [copy-file.d.ts](dist/copy-file.d.ts) in the **dist** folder for documentation.
57
-
58
- ## C) CLI Flags
59
-
60
- | Flag | Description | Values | Default |
61
- | ---------- | --------------------------------- | ------ | ------- |
62
- | `--folder` | Indicates the target is a folder. | N/A | N/A |
63
- | `--quiet` | Suppress informational messages. | N/A | N/A |
64
-
65
- ### Examples
66
- - `copy-file app.js app.mjs --quite` &nbsp; Displays no output.
67
- - `copy-file app.js --folder dist` &nbsp; Copies **app.js** into the **dist** folder.
65
+ See the **TypeScript Declarations** at the top of [copy-file.ts](copy-file.ts) for documentation.
68
66
 
69
67
  <br>
70
68
 
71
69
  ---
72
70
  **Build Tools**
73
- - 🎋 [add-dist-header](https://github.com/center-key/add-dist-header):&nbsp; _Adds a header comment to a file and saves it to your distribution folder_
74
- - 📄 [copy-file-util](https://github.com/center-key/copy-file-util):&nbsp; _A file copy and rename cli tool designed for use in npm scripts_
75
- - 📂 [copy-folder-cli](https://github.com/center-key/copy-folder-cli):&nbsp; _A recursive directory file copy utility designed for use in npm scripts_
71
+ - 🎋 [add-dist-header](https://github.com/center-key/add-dist-header):&nbsp; _Prepend a one-line header comment (with license notice) to distribution files_
72
+ - 📄 [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)_
73
+ - 📂 [copy-folder-cli](https://github.com/center-key/copy-folder-cli):&nbsp; _Recursively copy a folder (CLI tool designed for use in npm scripts)_
74
+ - 🚦 [w3c-html-validator](https://github.com/center-key/w3c-html-validator):&nbsp; _Check the markup validity of HTML files using the W3C validator_
75
+
76
+ Feel free to submit questions at:<br>
77
+ [github.com/center-key/copy-file-util/issues](https://github.com/center-key/copy-file-util/issues)
76
78
 
77
79
  [MIT License](LICENSE.txt)
package/bin/cli.js CHANGED
@@ -17,7 +17,7 @@
17
17
  // $ cd copy-file-util
18
18
  // $ npm install
19
19
  // $ npm test
20
- // $ node bin/cli.js spec/fixtures/source/mock.txt --folder spec/fixtures/target/to-folder
20
+ // $ node bin/cli.js --cd=spec/fixtures source/mock.txt --folder target/to-folder
21
21
 
22
22
  // Imports
23
23
  import { copyFile } from '../dist/copy-file.js';
@@ -25,17 +25,21 @@ import chalk from 'chalk';
25
25
  import log from 'fancy-log';
26
26
 
27
27
  // Parameters
28
- const validFlags = ['folder', 'quiet'];
29
- const args = process.argv.slice(2);
30
- const flags = args.filter(arg => /^--/.test(arg));
31
- const flagMap = Object.fromEntries(flags.map(flag => flag.replace(/^--/, '').split('=')));
32
- const params = args.filter(arg => !/^--/.test(arg));
33
- const source = params[0];
34
- const target = params[1];
28
+ const validFlags = ['cd', 'folder', 'quiet'];
29
+ const args = process.argv.slice(2);
30
+ const flags = args.filter(arg => /^--/.test(arg));
31
+ const flagMap = Object.fromEntries(flags.map(flag => flag.replace(/^--/, '').split('=')));
32
+ const invalidFlag = Object.keys(flagMap).find(key => !validFlags.includes(key));
33
+ const params = args.filter(arg => !/^--/.test(arg));
34
+
35
+ // Data
36
+ const source = params[0];
37
+ const target = params[1];
38
+ const mode = { folder: 'folder' in flagMap, quiet: 'quiet' in flagMap };
35
39
 
36
40
  // Reporting
37
41
  const printReport = (result) => {
38
- const name = chalk.gray('copy-file-util');
42
+ const name = chalk.gray('copy-file');
39
43
  const origin = chalk.blue.bold(result.origin);
40
44
  const dest = chalk.magenta(result.dest);
41
45
  const arrow = chalk.gray.bold(' ⟹ '); //extra space for alignment
@@ -44,9 +48,6 @@ const printReport = (result) => {
44
48
  };
45
49
 
46
50
  // Copy File
47
- const exit = (message) => (console.error('[copy-file]', message), process.exit(1));
48
- const invalidFlag = Object.keys(flagMap).find(key => !validFlags.includes(key));
49
- const mode = { folder: 'folder' in flagMap, quiet: 'quiet' in flagMap };
50
51
  const error =
51
52
  invalidFlag ? 'Invalid flag: ' + invalidFlag :
52
53
  params.length > 2 ? 'Extraneous parameter: ' + params[2] :
@@ -55,8 +56,12 @@ const error =
55
56
  !target ? 'Missing target file.' :
56
57
  null;
57
58
  if (error)
58
- exit(error);
59
- const options = mode.folder ? { targetFolder: target } : { targetFile: target };
59
+ throw Error('[copy-file-util] ' + error);
60
+ const targetKey = mode.folder ? 'targetFolder' : 'targetFile';
61
+ const options = {
62
+ cd: flagMap.cd ?? null,
63
+ [targetKey]: target,
64
+ };
60
65
  const result = copyFile.cp(source, options);
61
66
  if (!mode.quiet)
62
67
  printReport(result);
@@ -1,6 +1,7 @@
1
- //! copy-file-util v0.0.2 ~~ https://github.com/center-key/copy-file-util ~~ MIT License
1
+ //! copy-file-util v0.1.1 ~~ https://github.com/center-key/copy-file-util ~~ MIT License
2
2
 
3
3
  export declare type Options = {
4
+ cd?: string;
4
5
  targetFile?: string;
5
6
  targetFolder?: string;
6
7
  fileExtension?: string;
package/dist/copy-file.js CHANGED
@@ -1,4 +1,4 @@
1
- //! copy-file-util v0.0.2 ~~ https://github.com/center-key/copy-file-util ~~ MIT License
1
+ //! copy-file-util v0.1.1 ~~ https://github.com/center-key/copy-file-util ~~ MIT License
2
2
 
3
3
  import fs from 'fs-extra';
4
4
  import path from 'path';
@@ -7,6 +7,7 @@ const copyFile = {
7
7
  cp(sourceFile, options) {
8
8
  var _a;
9
9
  const defaults = {
10
+ cd: null,
10
11
  targetFile: null,
11
12
  targetFolder: null,
12
13
  fileExtension: null,
@@ -16,11 +17,14 @@ const copyFile = {
16
17
  const missingTarget = !settings.targetFile && !settings.targetFolder;
17
18
  const ambiguousTarget = !!settings.targetFile && !!settings.targetFolder;
18
19
  const normalize = (folder) => !folder ? '' : slash(path.normalize(folder)).replace(/\/$/, '');
19
- const source = normalize(sourceFile);
20
+ const startFolder = settings.cd ? normalize(settings.cd) + '/' : '';
21
+ const source = normalize(startFolder + sourceFile);
20
22
  const sourceExists = fs.pathExistsSync(source);
21
23
  const sourceIsFile = sourceExists && fs.statSync(source).isFile();
22
- const targetFolder = normalize(settings.targetFile ? path.dirname(settings.targetFile) : settings.targetFolder);
23
- const target = normalize((_a = settings.targetFile) !== null && _a !== void 0 ? _a : settings.targetFolder + '/' + path.basename(source));
24
+ const targetPath = settings.targetFile ? path.dirname(settings.targetFile) : settings.targetFolder;
25
+ const targetFolder = normalize(startFolder + targetPath);
26
+ const targetFile = (_a = settings.targetFile) !== null && _a !== void 0 ? _a : settings.targetFolder + '/' + path.basename(source);
27
+ const target = normalize(startFolder + targetFile);
24
28
  if (targetFolder)
25
29
  fs.ensureDirSync(targetFolder);
26
30
  const badTargetFolder = !fs.pathExistsSync(targetFolder);
@@ -1,4 +1,4 @@
1
- //! copy-file-util v0.0.2 ~~ https://github.com/center-key/copy-file-util ~~ MIT License
1
+ //! copy-file-util v0.1.1 ~~ 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 };
@@ -22,6 +22,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
22
22
  cp(sourceFile, options) {
23
23
  var _a;
24
24
  const defaults = {
25
+ cd: null,
25
26
  targetFile: null,
26
27
  targetFolder: null,
27
28
  fileExtension: null,
@@ -31,11 +32,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
31
32
  const missingTarget = !settings.targetFile && !settings.targetFolder;
32
33
  const ambiguousTarget = !!settings.targetFile && !!settings.targetFolder;
33
34
  const normalize = (folder) => !folder ? '' : (0, slash_1.default)(path_1.default.normalize(folder)).replace(/\/$/, '');
34
- const source = normalize(sourceFile);
35
+ const startFolder = settings.cd ? normalize(settings.cd) + '/' : '';
36
+ const source = normalize(startFolder + sourceFile);
35
37
  const sourceExists = fs_extra_1.default.pathExistsSync(source);
36
38
  const sourceIsFile = sourceExists && fs_extra_1.default.statSync(source).isFile();
37
- const targetFolder = normalize(settings.targetFile ? path_1.default.dirname(settings.targetFile) : settings.targetFolder);
38
- const target = normalize((_a = settings.targetFile) !== null && _a !== void 0 ? _a : settings.targetFolder + '/' + path_1.default.basename(source));
39
+ const targetPath = settings.targetFile ? path_1.default.dirname(settings.targetFile) : settings.targetFolder;
40
+ const targetFolder = normalize(startFolder + targetPath);
41
+ const targetFile = (_a = settings.targetFile) !== null && _a !== void 0 ? _a : settings.targetFolder + '/' + path_1.default.basename(source);
42
+ const target = normalize(startFolder + targetFile);
39
43
  if (targetFolder)
40
44
  fs_extra_1.default.ensureDirSync(targetFolder);
41
45
  const badTargetFolder = !fs_extra_1.default.pathExistsSync(targetFolder);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "copy-file-util",
3
- "version": "0.0.2",
4
- "description": "A file copy and rename cli tool designed for use in npm scripts (written in functional TypeScript)",
3
+ "version": "0.1.1",
4
+ "description": "Copy or rename a file (CLI tool designed for use in npm scripts)",
5
5
  "license": "MIT",
6
6
  "type": "module",
7
7
  "module": "dist/copy-file.js",
@@ -18,7 +18,8 @@
18
18
  "./": "./dist/"
19
19
  },
20
20
  "bin": {
21
- "copy-file": "bin/cli.js"
21
+ "copy-file": "bin/cli.js",
22
+ "copy-file-util": "bin/cli.js"
22
23
  },
23
24
  "repository": "github:center-key/copy-file-util",
24
25
  "homepage": "https://github.com/center-key/copy-file-util",
@@ -69,8 +70,7 @@
69
70
  "step:04": "tsc",
70
71
  "step:05": "tsc --module UMD --outDir build/umd",
71
72
  "step:06": "cpy build/umd/copy-file.js build --rename=copy-file.umd.cjs --flat=true",
72
- "step:07": "cpy build/copy-file.* dist --flat=true",
73
- "step:08": "add-dist-header build dist",
73
+ "step:07": "add-dist-header build dist",
74
74
  "pretest": "npm-run-all step:*",
75
75
  "test": "mocha spec/*.spec.js"
76
76
  },
@@ -89,7 +89,7 @@
89
89
  "add-dist-header": "~0.2",
90
90
  "assert-deep-strict-equal": "~1.0",
91
91
  "cpy-cli": "~4.2",
92
- "eslint": "~8.23",
92
+ "eslint": "~8.24",
93
93
  "jshint": "~2.13",
94
94
  "mocha": "~10.0",
95
95
  "npm-run-all2": "~6.0",