copy-file-util 0.1.0 → 0.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
@@ -8,32 +8,32 @@ _Copy or rename a file (CLI tool designed for use in npm scripts)_
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
  ```
33
+ Try out the first script with the command: `npm run pub-license`
35
34
 
36
- Try out the script with the command: `npm run pub-license`
35
+ <img src=https://raw.githubusercontent.com/center-key/copy-file-util/main/screenshot.png
36
+ width=800 alt=screenshot>
37
37
 
38
38
  ### 2. Global
39
39
  You can install **copy-file-util** globally and then run it anywhere directly from the terminal.
@@ -44,27 +44,28 @@ $ npm install --global copy-file-util
44
44
  $ copy-file src/web/api.html docs/api-manual.html
45
45
  ```
46
46
 
47
- ### 3. ESM and TypeScript Code
47
+ ### 3. CLI Flags
48
+ Command-line flags:
49
+ | Flag | Description | Values | Default |
50
+ | ---------- | --------------------------------- | ------ | ------- |
51
+ | `--folder` | Indicates the target is a folder. | N/A | N/A |
52
+ | `--quiet` | Suppress informational messages. | N/A | N/A |
53
+
54
+ Examples:
55
+ - `copy-file app.js app.mjs --quite` &nbsp; Displays no output.
56
+ - `copy-file app.js --folder dist` &nbsp; Copies **app.js** into the **dist** folder.
57
+
58
+ ## C) Application Code
48
59
  Even though **copy-file-util** is primarily intended for build scripts, the package can easily be used in ESM and TypeScript projects.
49
60
 
61
+ Example:
50
62
  ``` typescript
51
63
  import { copyFile } from 'copy-file-util';
52
64
  const result = copyFile.cp('src/web/api.html' { targetFile: 'docs/api-manual.html' });
53
65
  console.log('Execution time:', result.duration, 'ms');
54
66
  ```
55
67
 
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.
68
+ See the **TypeScript Declarations** at the top of [copy-file.ts](copy-file.ts) for documentation.
68
69
 
69
70
  <br>
70
71
 
package/bin/cli.js CHANGED
@@ -25,17 +25,21 @@ import chalk from 'chalk';
25
25
  import log from 'fancy-log';
26
26
 
27
27
  // Parameters
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 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,8 +48,6 @@ const printReport = (result) => {
44
48
  };
45
49
 
46
50
  // Copy File
47
- const invalidFlag = Object.keys(flagMap).find(key => !validFlags.includes(key));
48
- const mode = { folder: 'folder' in flagMap, quiet: 'quiet' in flagMap };
49
51
  const error =
50
52
  invalidFlag ? 'Invalid flag: ' + invalidFlag :
51
53
  params.length > 2 ? 'Extraneous parameter: ' + params[2] :
@@ -54,7 +56,7 @@ const error =
54
56
  !target ? 'Missing target file.' :
55
57
  null;
56
58
  if (error)
57
- throw Error('[copy-file] ' + error);
59
+ throw Error('[copy-file-util] ' + error);
58
60
  const targetKey = mode.folder ? 'targetFolder' : 'targetFile';
59
61
  const options = {
60
62
  cd: flagMap.cd ?? null,
@@ -1,4 +1,4 @@
1
- //! copy-file-util v0.1.0 ~~ https://github.com/center-key/copy-file-util ~~ MIT License
1
+ //! copy-file-util v0.1.2 ~~ https://github.com/center-key/copy-file-util ~~ MIT License
2
2
 
3
3
  export declare type Options = {
4
4
  cd?: string;
package/dist/copy-file.js CHANGED
@@ -1,4 +1,4 @@
1
- //! copy-file-util v0.1.0 ~~ https://github.com/center-key/copy-file-util ~~ MIT License
1
+ //! copy-file-util v0.1.2 ~~ 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';
@@ -18,16 +18,17 @@ const copyFile = {
18
18
  const ambiguousTarget = !!settings.targetFile && !!settings.targetFolder;
19
19
  const normalize = (folder) => !folder ? '' : slash(path.normalize(folder)).replace(/\/$/, '');
20
20
  const startFolder = settings.cd ? normalize(settings.cd) + '/' : '';
21
- const source = normalize(startFolder + sourceFile);
22
- const sourceExists = fs.pathExistsSync(source);
21
+ const source = sourceFile ? normalize(startFolder + sourceFile) : '';
22
+ const sourceExists = source && fs.pathExistsSync(source);
23
23
  const sourceIsFile = sourceExists && fs.statSync(source).isFile();
24
+ const sourceFilename = sourceIsFile ? path.basename(source) : null;
24
25
  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);
26
+ const targetFolder = targetPath ? normalize(startFolder + targetPath) : null;
27
+ const targetFile = (_a = settings.targetFile) !== null && _a !== void 0 ? _a : settings.targetFolder + '/' + sourceFilename;
27
28
  const target = normalize(startFolder + targetFile);
28
29
  if (targetFolder)
29
30
  fs.ensureDirSync(targetFolder);
30
- const badTargetFolder = !fs.pathExistsSync(targetFolder);
31
+ const badTargetFolder = !targetFolder || !fs.pathExistsSync(targetFolder);
31
32
  const errorMessage = settings.fileExtension ? 'Option "fileExtension" not yet implemented.' :
32
33
  !sourceFile ? 'Must specify the source file.' :
33
34
  !sourceExists ? 'Source file does not exist: ' + source :
@@ -1,4 +1,4 @@
1
- //! copy-file-util v0.1.0 ~~ https://github.com/center-key/copy-file-util ~~ MIT License
1
+ //! copy-file-util v0.1.2 ~~ 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 };
@@ -33,16 +33,17 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
33
33
  const ambiguousTarget = !!settings.targetFile && !!settings.targetFolder;
34
34
  const normalize = (folder) => !folder ? '' : (0, slash_1.default)(path_1.default.normalize(folder)).replace(/\/$/, '');
35
35
  const startFolder = settings.cd ? normalize(settings.cd) + '/' : '';
36
- const source = normalize(startFolder + sourceFile);
37
- const sourceExists = fs_extra_1.default.pathExistsSync(source);
36
+ const source = sourceFile ? normalize(startFolder + sourceFile) : '';
37
+ const sourceExists = source && fs_extra_1.default.pathExistsSync(source);
38
38
  const sourceIsFile = sourceExists && fs_extra_1.default.statSync(source).isFile();
39
+ const sourceFilename = sourceIsFile ? path_1.default.basename(source) : null;
39
40
  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);
41
+ const targetFolder = targetPath ? normalize(startFolder + targetPath) : null;
42
+ const targetFile = (_a = settings.targetFile) !== null && _a !== void 0 ? _a : settings.targetFolder + '/' + sourceFilename;
42
43
  const target = normalize(startFolder + targetFile);
43
44
  if (targetFolder)
44
45
  fs_extra_1.default.ensureDirSync(targetFolder);
45
- const badTargetFolder = !fs_extra_1.default.pathExistsSync(targetFolder);
46
+ const badTargetFolder = !targetFolder || !fs_extra_1.default.pathExistsSync(targetFolder);
46
47
  const errorMessage = settings.fileExtension ? 'Option "fileExtension" not yet implemented.' :
47
48
  !sourceFile ? 'Must specify the source file.' :
48
49
  !sourceExists ? 'Source file does not exist: ' + source :
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "copy-file-util",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "Copy or rename a file (CLI tool designed for use in npm scripts)",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -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",
@@ -88,7 +89,7 @@
88
89
  "add-dist-header": "~0.2",
89
90
  "assert-deep-strict-equal": "~1.0",
90
91
  "cpy-cli": "~4.2",
91
- "eslint": "~8.23",
92
+ "eslint": "~8.24",
92
93
  "jshint": "~2.13",
93
94
  "mocha": "~10.0",
94
95
  "npm-run-all2": "~6.0",