copy-file-util 0.0.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/LICENSE.txt +21 -0
- package/README.md +77 -0
- package/bin/cli.js +62 -0
- package/dist/copy-file.d.ts +16 -0
- package/dist/copy-file.js +45 -0
- package/dist/copy-file.umd.cjs +61 -0
- package/package.json +99 -0
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Individual contributors to copy-file-util
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# copy-file-util
|
|
2
|
+
<img src=https://centerkey.com/graphics/center-key-logo.svg align=right width=200 alt=logo>
|
|
3
|
+
|
|
4
|
+
_A file copy and rename cli tool designed for use in npm scripts_
|
|
5
|
+
|
|
6
|
+
[](https://github.com/center-key/copy-file-util/blob/main/LICENSE.txt)
|
|
7
|
+
[](https://www.npmjs.com/package/copy-file-util)
|
|
8
|
+
[](https://snyk.io/test/github/center-key/copy-file-util)
|
|
9
|
+
[](https://github.com/center-key/copy-file-util/actions/workflows/run-spec-on-push.yaml)
|
|
10
|
+
|
|
11
|
+
**copy-file-util** copies a folder and its subfolders.
|
|
12
|
+
|
|
13
|
+
## A) Setup
|
|
14
|
+
|
|
15
|
+
Install package for node:
|
|
16
|
+
```shell
|
|
17
|
+
$ npm install --save-dev copy-file-util
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## B) Usage
|
|
21
|
+
|
|
22
|
+
### 1. npm scripts
|
|
23
|
+
Call `copy-file` from the `"scripts"` section of your **package.json** file.
|
|
24
|
+
|
|
25
|
+
The **first** parameter is the *source* file.
|
|
26
|
+
The **second** parameter is the *target* file or folder (use the `--fodler` flag).
|
|
27
|
+
|
|
28
|
+
Example **package.json** script:
|
|
29
|
+
```json
|
|
30
|
+
"scripts": {
|
|
31
|
+
"pub-license": "copy-file src/LICENSE doc/license.txt",
|
|
32
|
+
"backup-license": "copy-file src/LICENSE --folder backup",
|
|
33
|
+
},
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Try out the script with the command: `npm run pub-license`
|
|
37
|
+
|
|
38
|
+
### 2. Global
|
|
39
|
+
You can install **copy-file-util** globally and then run it anywhere directly from the terminal.
|
|
40
|
+
|
|
41
|
+
Example terminal commands:
|
|
42
|
+
```shell
|
|
43
|
+
$ npm install --global copy-file-util
|
|
44
|
+
$ copy-file src/web/api.html docs/api-manual.html
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### 3. ESM and TypeScript Code
|
|
48
|
+
Even though **copy-file-util** is primarily intended for build scripts, the package can easily be used in ESM and TypeScript projects.
|
|
49
|
+
|
|
50
|
+
``` typescript
|
|
51
|
+
import { copyFile } from 'copy-file-util';
|
|
52
|
+
const result = copyFile.cp('src/web/api.html' { targetFile: 'docs/api-manual.html' });
|
|
53
|
+
console.log('Execution time:', result.duration, 'ms');
|
|
54
|
+
```
|
|
55
|
+
|
|
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` Displays no output.
|
|
67
|
+
- `copy-file app.js --folder dist` Copies **app.js** into the **dist** folder.
|
|
68
|
+
|
|
69
|
+
<br>
|
|
70
|
+
|
|
71
|
+
---
|
|
72
|
+
**Build Tools**
|
|
73
|
+
- 🎋 [add-dist-header](https://github.com/center-key/add-dist-header): _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): _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): _A recursive directory file copy utility designed for use in npm scripts_
|
|
76
|
+
|
|
77
|
+
[MIT License](LICENSE.txt)
|
package/bin/cli.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
////////////////////
|
|
3
|
+
// copy-file-util //
|
|
4
|
+
// MIT License //
|
|
5
|
+
////////////////////
|
|
6
|
+
|
|
7
|
+
// Usage in package.json:
|
|
8
|
+
// "scripts": {
|
|
9
|
+
// "pub-license": "copy-file src/LICENSE doc/license.txt"
|
|
10
|
+
// },
|
|
11
|
+
//
|
|
12
|
+
// Usage from command line:
|
|
13
|
+
// $ npm install --global copy-file-util
|
|
14
|
+
// $ copy-file src/LICENSE doc/license.txt
|
|
15
|
+
//
|
|
16
|
+
// Contributors to this project:
|
|
17
|
+
// $ cd copy-file-util
|
|
18
|
+
// $ npm install
|
|
19
|
+
// $ npm test
|
|
20
|
+
// $ node bin/cli.js spec/fixtures/source/mock.txt --folder spec/fixtures/target/to-folder
|
|
21
|
+
|
|
22
|
+
// Imports
|
|
23
|
+
import { copyFile } from '../dist/copy-file.js';
|
|
24
|
+
import chalk from 'chalk';
|
|
25
|
+
import log from 'fancy-log';
|
|
26
|
+
|
|
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];
|
|
35
|
+
|
|
36
|
+
// Reporting
|
|
37
|
+
const printReport = (result) => {
|
|
38
|
+
const name = chalk.gray('copy-file-util');
|
|
39
|
+
const origin = chalk.blue.bold(result.origin);
|
|
40
|
+
const dest = chalk.magenta(result.dest);
|
|
41
|
+
const arrow = chalk.gray.bold(' ⟹ '); //extra space for alignment
|
|
42
|
+
const info = chalk.white(`(${result.duration}ms)`);
|
|
43
|
+
log(name, origin, arrow, dest, info);
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
// 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
|
+
const error =
|
|
51
|
+
invalidFlag ? 'Invalid flag: ' + invalidFlag :
|
|
52
|
+
params.length > 2 ? 'Extraneous parameter: ' + params[2] :
|
|
53
|
+
!source ? 'Missing source file.' :
|
|
54
|
+
!target && mode.folder ? 'Missing target folder.' :
|
|
55
|
+
!target ? 'Missing target file.' :
|
|
56
|
+
null;
|
|
57
|
+
if (error)
|
|
58
|
+
exit(error);
|
|
59
|
+
const options = mode.folder ? { targetFolder: target } : { targetFile: target };
|
|
60
|
+
const result = copyFile.cp(source, options);
|
|
61
|
+
if (!mode.quiet)
|
|
62
|
+
printReport(result);
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
//! copy-file-util v0.0.2 ~~ https://github.com/center-key/copy-file-util ~~ MIT License
|
|
2
|
+
|
|
3
|
+
export declare type Options = {
|
|
4
|
+
targetFile?: string;
|
|
5
|
+
targetFolder?: string;
|
|
6
|
+
fileExtension?: string;
|
|
7
|
+
};
|
|
8
|
+
export declare type Result = {
|
|
9
|
+
origin: string;
|
|
10
|
+
dest: string;
|
|
11
|
+
duration: number;
|
|
12
|
+
};
|
|
13
|
+
declare const copyFile: {
|
|
14
|
+
cp(sourceFile: string, options: Options): Result;
|
|
15
|
+
};
|
|
16
|
+
export { copyFile };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
//! copy-file-util v0.0.2 ~~ https://github.com/center-key/copy-file-util ~~ MIT License
|
|
2
|
+
|
|
3
|
+
import fs from 'fs-extra';
|
|
4
|
+
import path from 'path';
|
|
5
|
+
import slash from 'slash';
|
|
6
|
+
const copyFile = {
|
|
7
|
+
cp(sourceFile, options) {
|
|
8
|
+
var _a;
|
|
9
|
+
const defaults = {
|
|
10
|
+
targetFile: null,
|
|
11
|
+
targetFolder: null,
|
|
12
|
+
fileExtension: null,
|
|
13
|
+
};
|
|
14
|
+
const settings = Object.assign(Object.assign({}, defaults), options);
|
|
15
|
+
const startTime = Date.now();
|
|
16
|
+
const missingTarget = !settings.targetFile && !settings.targetFolder;
|
|
17
|
+
const ambiguousTarget = !!settings.targetFile && !!settings.targetFolder;
|
|
18
|
+
const normalize = (folder) => !folder ? '' : slash(path.normalize(folder)).replace(/\/$/, '');
|
|
19
|
+
const source = normalize(sourceFile);
|
|
20
|
+
const sourceExists = fs.pathExistsSync(source);
|
|
21
|
+
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
|
+
if (targetFolder)
|
|
25
|
+
fs.ensureDirSync(targetFolder);
|
|
26
|
+
const badTargetFolder = !fs.pathExistsSync(targetFolder);
|
|
27
|
+
const errorMessage = settings.fileExtension ? 'Option "fileExtension" not yet implemented.' :
|
|
28
|
+
!sourceFile ? 'Must specify the source file.' :
|
|
29
|
+
!sourceExists ? 'Source file does not exist: ' + source :
|
|
30
|
+
!sourceIsFile ? 'Source is not a file: ' + source :
|
|
31
|
+
missingTarget ? 'Must specify a target file or folder.' :
|
|
32
|
+
ambiguousTarget ? 'Target cannot be both a file and a folder.' :
|
|
33
|
+
badTargetFolder ? 'Target folder cannot be written to: ' + targetFolder :
|
|
34
|
+
null;
|
|
35
|
+
if (errorMessage)
|
|
36
|
+
throw Error('[copy-file-util] ' + errorMessage);
|
|
37
|
+
fs.copySync(source, target);
|
|
38
|
+
return {
|
|
39
|
+
origin: source,
|
|
40
|
+
dest: target,
|
|
41
|
+
duration: Date.now() - startTime,
|
|
42
|
+
};
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
export { copyFile };
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
//! copy-file-util v0.0.2 ~~ https://github.com/center-key/copy-file-util ~~ MIT License
|
|
2
|
+
|
|
3
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
|
+
};
|
|
6
|
+
(function (factory) {
|
|
7
|
+
if (typeof module === "object" && typeof module.exports === "object") {
|
|
8
|
+
var v = factory(require, exports);
|
|
9
|
+
if (v !== undefined) module.exports = v;
|
|
10
|
+
}
|
|
11
|
+
else if (typeof define === "function" && define.amd) {
|
|
12
|
+
define(["require", "exports", "fs-extra", "path", "slash"], factory);
|
|
13
|
+
}
|
|
14
|
+
})(function (require, exports) {
|
|
15
|
+
"use strict";
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.copyFile = void 0;
|
|
18
|
+
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
19
|
+
const path_1 = __importDefault(require("path"));
|
|
20
|
+
const slash_1 = __importDefault(require("slash"));
|
|
21
|
+
const copyFile = {
|
|
22
|
+
cp(sourceFile, options) {
|
|
23
|
+
var _a;
|
|
24
|
+
const defaults = {
|
|
25
|
+
targetFile: null,
|
|
26
|
+
targetFolder: null,
|
|
27
|
+
fileExtension: null,
|
|
28
|
+
};
|
|
29
|
+
const settings = Object.assign(Object.assign({}, defaults), options);
|
|
30
|
+
const startTime = Date.now();
|
|
31
|
+
const missingTarget = !settings.targetFile && !settings.targetFolder;
|
|
32
|
+
const ambiguousTarget = !!settings.targetFile && !!settings.targetFolder;
|
|
33
|
+
const normalize = (folder) => !folder ? '' : (0, slash_1.default)(path_1.default.normalize(folder)).replace(/\/$/, '');
|
|
34
|
+
const source = normalize(sourceFile);
|
|
35
|
+
const sourceExists = fs_extra_1.default.pathExistsSync(source);
|
|
36
|
+
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
|
+
if (targetFolder)
|
|
40
|
+
fs_extra_1.default.ensureDirSync(targetFolder);
|
|
41
|
+
const badTargetFolder = !fs_extra_1.default.pathExistsSync(targetFolder);
|
|
42
|
+
const errorMessage = settings.fileExtension ? 'Option "fileExtension" not yet implemented.' :
|
|
43
|
+
!sourceFile ? 'Must specify the source file.' :
|
|
44
|
+
!sourceExists ? 'Source file does not exist: ' + source :
|
|
45
|
+
!sourceIsFile ? 'Source is not a file: ' + source :
|
|
46
|
+
missingTarget ? 'Must specify a target file or folder.' :
|
|
47
|
+
ambiguousTarget ? 'Target cannot be both a file and a folder.' :
|
|
48
|
+
badTargetFolder ? 'Target folder cannot be written to: ' + targetFolder :
|
|
49
|
+
null;
|
|
50
|
+
if (errorMessage)
|
|
51
|
+
throw Error('[copy-file-util] ' + errorMessage);
|
|
52
|
+
fs_extra_1.default.copySync(source, target);
|
|
53
|
+
return {
|
|
54
|
+
origin: source,
|
|
55
|
+
dest: target,
|
|
56
|
+
duration: Date.now() - startTime,
|
|
57
|
+
};
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
exports.copyFile = copyFile;
|
|
61
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
{
|
|
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)",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"module": "dist/copy-file.js",
|
|
8
|
+
"main": "dist/copy-file.umd.cjs",
|
|
9
|
+
"types": "dist/copy-file.d.ts",
|
|
10
|
+
"files": [
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"import": "./dist/copy-file.js",
|
|
16
|
+
"require": "./dist/copy-file.umd.cjs"
|
|
17
|
+
},
|
|
18
|
+
"./": "./dist/"
|
|
19
|
+
},
|
|
20
|
+
"bin": {
|
|
21
|
+
"copy-file": "bin/cli.js"
|
|
22
|
+
},
|
|
23
|
+
"repository": "github:center-key/copy-file-util",
|
|
24
|
+
"homepage": "https://github.com/center-key/copy-file-util",
|
|
25
|
+
"bugs": "https://github.com/center-key/copy-file-util/issues",
|
|
26
|
+
"docs": "https://github.com/center-key/copy-file-util#readme",
|
|
27
|
+
"author": "Center Key (https://centerkey.com)",
|
|
28
|
+
"keywords": [
|
|
29
|
+
"cli",
|
|
30
|
+
"copy",
|
|
31
|
+
"cp",
|
|
32
|
+
"file",
|
|
33
|
+
"rename",
|
|
34
|
+
"scripts"
|
|
35
|
+
],
|
|
36
|
+
"jshintConfig": {
|
|
37
|
+
"esversion": 11,
|
|
38
|
+
"strict": "implied",
|
|
39
|
+
"eqeqeq": true,
|
|
40
|
+
"undef": true,
|
|
41
|
+
"unused": true,
|
|
42
|
+
"varstmt": true,
|
|
43
|
+
"node": true,
|
|
44
|
+
"mocha": true
|
|
45
|
+
},
|
|
46
|
+
"eslintConfig": {
|
|
47
|
+
"ignorePatterns": [
|
|
48
|
+
"build",
|
|
49
|
+
"dist",
|
|
50
|
+
"node_modules"
|
|
51
|
+
],
|
|
52
|
+
"root": true,
|
|
53
|
+
"parser": "@typescript-eslint/parser",
|
|
54
|
+
"plugins": [
|
|
55
|
+
"@typescript-eslint"
|
|
56
|
+
],
|
|
57
|
+
"extends": [
|
|
58
|
+
"eslint:recommended",
|
|
59
|
+
"plugin:@typescript-eslint/recommended"
|
|
60
|
+
],
|
|
61
|
+
"rules": {
|
|
62
|
+
"@typescript-eslint/no-non-null-assertion": "off"
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
"scripts": {
|
|
66
|
+
"step:01": "rimraf build dist spec/fixtures/target **/.DS_Store",
|
|
67
|
+
"step:02": "jshint . --exclude-path .gitignore",
|
|
68
|
+
"step:03": "eslint --max-warnings 0 . --ext .ts",
|
|
69
|
+
"step:04": "tsc",
|
|
70
|
+
"step:05": "tsc --module UMD --outDir build/umd",
|
|
71
|
+
"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",
|
|
74
|
+
"pretest": "npm-run-all step:*",
|
|
75
|
+
"test": "mocha spec/*.spec.js"
|
|
76
|
+
},
|
|
77
|
+
"dependencies": {
|
|
78
|
+
"chalk": "~5.0",
|
|
79
|
+
"fancy-log": "~2.0",
|
|
80
|
+
"fs-extra": "~10.1",
|
|
81
|
+
"slash": "~4.0"
|
|
82
|
+
},
|
|
83
|
+
"devDependencies": {
|
|
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",
|
|
90
|
+
"assert-deep-strict-equal": "~1.0",
|
|
91
|
+
"cpy-cli": "~4.2",
|
|
92
|
+
"eslint": "~8.23",
|
|
93
|
+
"jshint": "~2.13",
|
|
94
|
+
"mocha": "~10.0",
|
|
95
|
+
"npm-run-all2": "~6.0",
|
|
96
|
+
"rimraf": "~3.0",
|
|
97
|
+
"typescript": "~4.8"
|
|
98
|
+
}
|
|
99
|
+
}
|