copy-file-util 0.1.2 → 0.1.4

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,14 +1,18 @@
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 (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 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** takes a source file and copies it to a new destination. The console output includes a timestamp and formatting helpful in build systems.
11
+ **copy-file-util** takes a source file and copies it to a new destination.&nbsp;
12
+ The command's console output includes a timestamp and formatting helpful in build systems.
13
+
14
+ <img src=https://raw.githubusercontent.com/center-key/copy-file-util/main/screenshot.png
15
+ width=800 alt=screenshot>
12
16
 
13
17
  ## A) Setup
14
18
  Install package for node:
@@ -20,8 +24,9 @@ $ npm install --save-dev copy-file-util
20
24
  ### 1. npm scripts
21
25
  Run `copy-file` from the `"scripts"` section of your **package.json** file.
22
26
 
23
- The **first** parameter is the *source* file.
24
- The **second** parameter is the *target* file or folder (use the `--folder` flag).
27
+ Parameters:
28
+ * The **first** parameter is the *source* file.
29
+ * The **second** parameter is the *target* file or folder (use the `--folder` flag).
25
30
 
26
31
  Example **package.json** scripts:
27
32
  ```json
@@ -30,10 +35,6 @@ Example **package.json** scripts:
30
35
  "backup-license": "copy-file src/LICENSE --folder backup",
31
36
  },
32
37
  ```
33
- Try out the first script with the command: `npm run pub-license`
34
-
35
- <img src=https://raw.githubusercontent.com/center-key/copy-file-util/main/screenshot.png
36
- width=800 alt=screenshot>
37
38
 
38
39
  ### 2. Global
39
40
  You can install **copy-file-util** globally and then run it anywhere directly from the terminal.
@@ -46,17 +47,24 @@ $ copy-file src/web/api.html docs/api-manual.html
46
47
 
47
48
  ### 3. CLI Flags
48
49
  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 |
50
+ | Flag | Description | Values |
51
+ | ---------- | ---------------------------------------------- | ---------- |
52
+ | `--cd` | Change working directory before starting copy. | **string** |
53
+ | `--folder` | Indicates the target is a folder. | N/A |
54
+ | `--quiet` | Suppress informational messages. | N/A |
53
55
 
54
56
  Examples:
55
57
  - `copy-file app.js app.mjs --quite` &nbsp; Displays no output.
56
58
  - `copy-file app.js --folder dist` &nbsp; Copies **app.js** into the **dist** folder.
57
59
 
60
+ ### 4. Template Variables
61
+ 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.
62
+
63
+ Example:
64
+ - `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.
65
+
58
66
  ## C) Application Code
59
- Even though **copy-file-util** is primarily intended for build scripts, the package can easily be used in ESM and TypeScript projects.
67
+ Even though **copy-file-util** is primarily intended for build scripts, the package can easily be used programmatically in ESM and TypeScript projects.
60
68
 
61
69
  Example:
62
70
  ``` typescript
@@ -70,10 +78,12 @@ See the **TypeScript Declarations** at the top of [copy-file.ts](copy-file.ts) f
70
78
  <br>
71
79
 
72
80
  ---
73
- **Build Tools**
74
- - 🎋 [add-dist-header](https://github.com/center-key/add-dist-header):&nbsp; _Prepend a one-line header comment (with license notice) to distribution files_
75
- - 📄 [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)_
76
- - 📂 [copy-folder-cli](https://github.com/center-key/copy-folder-cli):&nbsp; _Recursively copy a folder (CLI tool designed for use in npm scripts)_
81
+ **CLI Build Tools**
82
+ - 🎋 [add-dist-header](https://github.com/center-key/add-dist-header):&nbsp; _Prepend a one-line banner comment (with license notice) to distribution files_
83
+ - 📄 [copy-file-util](https://github.com/center-key/copy-file-util):&nbsp; _Copy or rename a file with optional package version number_
84
+ - 📂 [copy-folder-util](https://github.com/center-key/copy-folder-util):&nbsp; _Recursively copy files from one folder to another folder_
85
+ - 🔍 [replacer-util](https://github.com/center-key/replacer-util):&nbsp; _Find and replace strings or template outputs in text files_
86
+ - 🔢 [rev-web-assets](https://github.com/center-key/rev-web-assets):&nbsp; _Revision web asset filenames with cache busting content hash fingerprints_
77
87
  - 🚦 [w3c-html-validator](https://github.com/center-key/w3c-html-validator):&nbsp; _Check the markup validity of HTML files using the W3C validator_
78
88
 
79
89
  Feel free to submit questions at:<br>
package/bin/cli.js CHANGED
@@ -17,25 +17,33 @@
17
17
  // $ cd copy-file-util
18
18
  // $ npm install
19
19
  // $ npm test
20
- // $ node bin/cli.js --cd=spec/fixtures source/mock.txt --folder target/to-folder
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/{{pkg.type}}/{{pkg.name}}-v{{pkg.version}}.html
21
22
 
22
23
  // Imports
23
24
  import { copyFile } from '../dist/copy-file.js';
24
- import chalk from 'chalk';
25
- import log from 'fancy-log';
25
+ import { dna } from 'dna-engine';
26
+ import chalk from 'chalk';
27
+ import fs from 'fs';
28
+ import log from 'fancy-log';
26
29
 
27
30
  // Parameters
28
31
  const validFlags = ['cd', 'folder', 'quiet'];
29
32
  const args = process.argv.slice(2);
30
33
  const flags = args.filter(arg => /^--/.test(arg));
31
34
  const flagMap = Object.fromEntries(flags.map(flag => flag.replace(/^--/, '').split('=')));
35
+ const flagOn = Object.fromEntries(validFlags.map(flag => [flag, flag in flagMap]));
32
36
  const invalidFlag = Object.keys(flagMap).find(key => !validFlags.includes(key));
33
37
  const params = args.filter(arg => !/^--/.test(arg));
34
38
 
35
39
  // Data
36
40
  const source = params[0];
37
41
  const target = params[1];
38
- const mode = { folder: 'folder' in flagMap, quiet: 'quiet' in flagMap };
42
+
43
+ // Utilities
44
+ const readPackage = () => JSON.parse(fs.readFileSync('package.json', 'utf-8'));
45
+ const getPackageField = (match) =>
46
+ dna.util.value({ pkg: readPackage() }, match.replace(/[{}]/g, '')) ?? 'MISSING-FIELD-ERROR';
39
47
 
40
48
  // Reporting
41
49
  const printReport = (result) => {
@@ -49,19 +57,19 @@ const printReport = (result) => {
49
57
 
50
58
  // Copy File
51
59
  const error =
52
- invalidFlag ? 'Invalid flag: ' + invalidFlag :
53
- params.length > 2 ? 'Extraneous parameter: ' + params[2] :
54
- !source ? 'Missing source file.' :
55
- !target && mode.folder ? 'Missing target folder.' :
56
- !target ? 'Missing target file.' :
60
+ invalidFlag ? 'Invalid flag: ' + invalidFlag :
61
+ params.length > 2 ? 'Extraneous parameter: ' + params[2] :
62
+ !source ? 'Missing source file.' :
63
+ !target && flagOn.folder ? 'Missing target folder.' :
64
+ !target ? 'Missing target file.' :
57
65
  null;
58
66
  if (error)
59
67
  throw Error('[copy-file-util] ' + error);
60
- const targetKey = mode.folder ? 'targetFolder' : 'targetFile';
68
+ const targetKey = flagOn.folder ? 'targetFolder' : 'targetFile';
61
69
  const options = {
62
70
  cd: flagMap.cd ?? null,
63
- [targetKey]: target,
71
+ [targetKey]: target.replace(/{{[^{}]*}}/g, getPackageField),
64
72
  };
65
73
  const result = copyFile.cp(source, options);
66
- if (!mode.quiet)
74
+ if (!flagOn.quiet)
67
75
  printReport(result);
@@ -1,11 +1,12 @@
1
- //! copy-file-util v0.1.2 ~~ https://github.com/center-key/copy-file-util ~~ MIT License
1
+ //! copy-file-util v0.1.4 ~~ https://github.com/center-key/copy-file-util ~~ MIT License
2
2
 
3
- export declare type Options = {
4
- cd?: string;
5
- targetFile?: string;
6
- targetFolder?: string;
7
- fileExtension?: string;
3
+ export declare type Settings = {
4
+ cd: string;
5
+ targetFile: string;
6
+ targetFolder: string;
7
+ fileExtension: string;
8
8
  };
9
+ export declare type Options = Partial<Settings>;
9
10
  export declare type Result = {
10
11
  origin: string;
11
12
  dest: string;
package/dist/copy-file.js CHANGED
@@ -1,6 +1,6 @@
1
- //! copy-file-util v0.1.2 ~~ https://github.com/center-key/copy-file-util ~~ MIT License
1
+ //! copy-file-util v0.1.4 ~~ https://github.com/center-key/copy-file-util ~~ MIT License
2
2
 
3
- import fs from 'fs-extra';
3
+ import fs from 'fs';
4
4
  import path from 'path';
5
5
  import slash from 'slash';
6
6
  const copyFile = {
@@ -19,7 +19,7 @@ const copyFile = {
19
19
  const normalize = (folder) => !folder ? '' : slash(path.normalize(folder)).replace(/\/$/, '');
20
20
  const startFolder = settings.cd ? normalize(settings.cd) + '/' : '';
21
21
  const source = sourceFile ? normalize(startFolder + sourceFile) : '';
22
- const sourceExists = source && fs.pathExistsSync(source);
22
+ const sourceExists = source && fs.existsSync(source);
23
23
  const sourceIsFile = sourceExists && fs.statSync(source).isFile();
24
24
  const sourceFilename = sourceIsFile ? path.basename(source) : null;
25
25
  const targetPath = settings.targetFile ? path.dirname(settings.targetFile) : settings.targetFolder;
@@ -27,8 +27,8 @@ const copyFile = {
27
27
  const targetFile = (_a = settings.targetFile) !== null && _a !== void 0 ? _a : settings.targetFolder + '/' + sourceFilename;
28
28
  const target = normalize(startFolder + targetFile);
29
29
  if (targetFolder)
30
- fs.ensureDirSync(targetFolder);
31
- const badTargetFolder = !targetFolder || !fs.pathExistsSync(targetFolder);
30
+ fs.mkdirSync(targetFolder, { recursive: true });
31
+ const badTargetFolder = !targetFolder || !fs.existsSync(targetFolder);
32
32
  const errorMessage = settings.fileExtension ? 'Option "fileExtension" not yet implemented.' :
33
33
  !sourceFile ? 'Must specify the source file.' :
34
34
  !sourceExists ? 'Source file does not exist: ' + source :
@@ -39,7 +39,7 @@ const copyFile = {
39
39
  null;
40
40
  if (errorMessage)
41
41
  throw Error('[copy-file-util] ' + errorMessage);
42
- fs.copySync(source, target);
42
+ fs.copyFileSync(source, target);
43
43
  return {
44
44
  origin: source,
45
45
  dest: target,
@@ -1,4 +1,4 @@
1
- //! copy-file-util v0.1.2 ~~ https://github.com/center-key/copy-file-util ~~ MIT License
1
+ //! copy-file-util v0.1.4 ~~ 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 };
@@ -9,13 +9,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
9
9
  if (v !== undefined) module.exports = v;
10
10
  }
11
11
  else if (typeof define === "function" && define.amd) {
12
- define(["require", "exports", "fs-extra", "path", "slash"], factory);
12
+ define(["require", "exports", "fs", "path", "slash"], factory);
13
13
  }
14
14
  })(function (require, exports) {
15
15
  "use strict";
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  exports.copyFile = void 0;
18
- const fs_extra_1 = __importDefault(require("fs-extra"));
18
+ const fs_1 = __importDefault(require("fs"));
19
19
  const path_1 = __importDefault(require("path"));
20
20
  const slash_1 = __importDefault(require("slash"));
21
21
  const copyFile = {
@@ -34,16 +34,16 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
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
36
  const source = sourceFile ? normalize(startFolder + sourceFile) : '';
37
- const sourceExists = source && fs_extra_1.default.pathExistsSync(source);
38
- const sourceIsFile = sourceExists && fs_extra_1.default.statSync(source).isFile();
37
+ const sourceExists = source && fs_1.default.existsSync(source);
38
+ const sourceIsFile = sourceExists && fs_1.default.statSync(source).isFile();
39
39
  const sourceFilename = sourceIsFile ? path_1.default.basename(source) : null;
40
40
  const targetPath = settings.targetFile ? path_1.default.dirname(settings.targetFile) : settings.targetFolder;
41
41
  const targetFolder = targetPath ? normalize(startFolder + targetPath) : null;
42
42
  const targetFile = (_a = settings.targetFile) !== null && _a !== void 0 ? _a : settings.targetFolder + '/' + sourceFilename;
43
43
  const target = normalize(startFolder + targetFile);
44
44
  if (targetFolder)
45
- fs_extra_1.default.ensureDirSync(targetFolder);
46
- const badTargetFolder = !targetFolder || !fs_extra_1.default.pathExistsSync(targetFolder);
45
+ fs_1.default.mkdirSync(targetFolder, { recursive: true });
46
+ const badTargetFolder = !targetFolder || !fs_1.default.existsSync(targetFolder);
47
47
  const errorMessage = settings.fileExtension ? 'Option "fileExtension" not yet implemented.' :
48
48
  !sourceFile ? 'Must specify the source file.' :
49
49
  !sourceExists ? 'Source file does not exist: ' + source :
@@ -54,7 +54,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
54
54
  null;
55
55
  if (errorMessage)
56
56
  throw Error('[copy-file-util] ' + errorMessage);
57
- fs_extra_1.default.copySync(source, target);
57
+ fs_1.default.copyFileSync(source, target);
58
58
  return {
59
59
  origin: source,
60
60
  dest: target,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "copy-file-util",
3
- "version": "0.1.2",
4
- "description": "Copy or rename a file (CLI tool designed for use in npm scripts)",
3
+ "version": "0.1.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",
7
7
  "module": "dist/copy-file.js",
@@ -75,21 +75,20 @@
75
75
  "test": "mocha spec/*.spec.js"
76
76
  },
77
77
  "dependencies": {
78
- "chalk": "~5.0",
78
+ "chalk": "~5.1",
79
+ "dna-engine": "~2.2",
79
80
  "fancy-log": "~2.0",
80
- "fs-extra": "~10.1",
81
- "slash": "~4.0"
81
+ "slash": "~5.0"
82
82
  },
83
83
  "devDependencies": {
84
84
  "@types/fancy-log": "~2.0",
85
- "@types/fs-extra": "~9.0",
86
- "@types/node": "~18.7",
87
- "@typescript-eslint/eslint-plugin": "~5.38",
88
- "@typescript-eslint/parser": "~5.38",
89
- "add-dist-header": "~0.2",
85
+ "@types/node": "~18.8",
86
+ "@typescript-eslint/eslint-plugin": "~5.40",
87
+ "@typescript-eslint/parser": "~5.40",
88
+ "add-dist-header": "~0.3",
90
89
  "assert-deep-strict-equal": "~1.0",
91
90
  "cpy-cli": "~4.2",
92
- "eslint": "~8.24",
91
+ "eslint": "~8.25",
93
92
  "jshint": "~2.13",
94
93
  "mocha": "~10.0",
95
94
  "npm-run-all2": "~6.0",