copy-files-from-to
Copy files from one path to another, based on the instructions provided in a configuration file.
Use cases
- This tool is useful when a few files need to be copied / updated frequently
- This tool works as a basic alternative for
npm
/ bower
when you wish to copy the scripts out of node_modules
/ bower_components
folder - You may like to use this tool if you prefer to keep some third-party dependencies (eg: from
node_modules
) updated and/or committed to your project's repository
Installation
$ npm install -g copy-files-from-to
How to use
In your package.json
file, add the "copyFiles"
and "copyFilesSettings"
(optional) instructions as described in this section.
Alternatively, you may create a file, say, copy-files-from-to.json
or copy-files-from-to.cjson
(JSON with comments) in your project and refer to the following usage examples.
Basic usage
Sample file: package.json
{
"name": "my-application",
"version": "1.0.0",
"dependencies": {
"jquery": "3.4.0"
},
"copyFiles": [
{
"from": "node_modules/jquery/dist/jquery.js",
"to": "scripts/jquery/jquery.js"
},
{
"from": "https://raw.githubusercontent.com/webextensions/console-panel/master/src/console-panel.js",
"to": "scripts/console-panel/console-panel.js"
},
{
"from": "https://raw.githubusercontent.com/webextensions/console-panel/master/src/console-panel.css",
"to": "scripts/console-panel/console-panel.css"
}
]
}
Sample file: copy-files-from-to.json
{
"copyFiles": [
{
"from": "node_modules/jquery/dist/jquery.js",
"to": "scripts/jquery/jquery.js"
},
{
"from": "https://raw.githubusercontent.com/webextensions/console-panel/master/src/console-panel.js",
"to": "scripts/console-panel/console-panel.js"
},
{
"from": "https://raw.githubusercontent.com/webextensions/console-panel/master/src/console-panel.css",
"to": "scripts/console-panel/console-panel.css"
}
]
}
Command and output
$ copy-files-from-to
Reading copy instructions from file copy-files-from-to.json
Starting copy operation in "default" mode:
✓ Copied [ utf8 ] node_modules/jquery/dist/jquery.js to scripts/jquery/jquery.js
✓ Copied [binary] assets/logo.png to build/logo.png
✓ Copied [remote] https://raw.githubusercontent.com/webextensions/console-panel/master/src/console-panel.css to scripts/console-panel/console-panel.css
✓ Copied [remote] https://raw.githubusercontent.com/webextensions/console-panel/master/src/console-panel.js to scripts/console-panel/console-panel.js
Advanced usage
Sample file: copy-files-from-to.cjson
{
"copyFiles": [
{
"from": {
"default": "https://raw.githubusercontent.com/jashkenas/underscore/master/underscore-min.js",
"development": "https://raw.githubusercontent.com/jashkenas/underscore/master/underscore.js"
},
"to": "scripts/underscore.js"
},
{
"from": "https://raw.githubusercontent.com/jashkenas/underscore/master/underscore-min.js.map",
"to": {
"default": {
"skip": true
},
"pre-production": "scripts/underscore.js.map"
}
},
{
"from": {
"default": {
"skip": true
},
"pre-production": "node_modules/native-promise-only/npo.js",
"production": "node_modules/native-promise-only/npo.js"
},
"to": "scripts/native-promise-only.js"
},
{
"from": "https://raw.githubusercontent.com/webextensions/console-panel/master/src/console-panel.js",
"to": {
"default": "scripts/console-panel/console-panel.js",
"production": {
"skip": true
}
}
},
{
"from": "src/app/images",
"to": "public/assets"
},
{
"from": "src/app/images/favicon.ico",
"to": "public/"
},
{
"from": "assets/**/*.jpg",
"to": "public/images/"
},
{
"from": "assets/**/*.jpg",
"to": "public/copy-all-jpg-files-to-this-directory/",
"toFlat": true
}
],
"copyFilesSettings": {
"whenFileExists": "notify-about-available-change",
"addReferenceToSourceOfOrigin": false,
"ignoreDotFilesAndFolders": true,
"removeSourceMappingURL": false,
"minifyJs": true,
"minifyJsTerserOptions": {
"compress": {
"sequences": false
},
"mangle": false,
"format": {
"semicolons": false
}
}
}
}
Command and output
$ copy-files-from-to
Reading copy instructions from file copy-files-from-to.cjson
Starting copy operation in "default" mode:
✓ Copied [remote] https://raw.githubusercontent.com/jashkenas/underscore/master/underscore-min.js to scripts/underscore.js
✓ Copied [remote] https://raw.githubusercontent.com/webextensions/console-panel/master/src/console-panel.js to scripts/console-panel/console-panel.js
$ copy-files-from-to --mode development
Reading copy instructions from file copy-files-from-to.cjson
Starting copy operation in "development" mode:
✓ Copied [remote] https://raw.githubusercontent.com/jashkenas/underscore/master/underscore.js to scripts/underscore.js
✓ Copied [remote] https://raw.githubusercontent.com/webextensions/console-panel/master/src/console-panel.js to scripts/console-panel/console-panel.js
$ copy-files-from-to --mode production
Reading copy instructions from file copy-files-from-to.cjson
Starting copy operation in "production" mode:
✓ Copied [ utf8 ] node_modules/native-promise-only/npo.js to scripts/native-promise-only.js
✓ Copied [remote] https://raw.githubusercontent.com/jashkenas/underscore/master/underscore-min.js to scripts/underscore.js
$ copy-files-from-to --mode pre-production --config copy-files-from-to.cjson
Reading copy instructions from file copy-files-from-to.cjson
Starting copy operation in "pre-production" mode:
✓ Copied [ utf8 ] node_modules/native-promise-only/npo.js to scripts/native-promise-only.js
✓ Copied [remote] https://raw.githubusercontent.com/jashkenas/underscore/master/underscore-min.js to scripts/underscore.js
✓ Copied [remote] https://raw.githubusercontent.com/jashkenas/underscore/master/underscore-min.js.map to scripts/underscore.js.map
✓ Copied [remote] https://raw.githubusercontent.com/webextensions/console-panel/master/src/console-panel.js to scripts/console-panel/console-panel.js
Configuration
Structure of "copyFiles" instruction file
- You can provide the "copyFiles" instructions in a JSON or CJSON file
- The file can be structured like:
{
"copyFiles": [
{
"from": "http://example.com/index.html",
"to": "example-index.html"
},
{
"from": [
"public/**/*",
"!public/tmp/**/*",
{ dot: true, followSymlinkedDirectories: false }
],
"to": "build/"
},
{
"from": {
"default": "node_modules/example/index.js",
"development": "node_modules/example/dev.js",
"production": "node_modules/example/production.js"
},
"to": {
"default": "example.js"
}
},
{
"from": {
"default": {
"src": "http://example.com/index-1.0.0.js.map",
"latest": "http://example.com/index.js.map"
}
},
"to": {
"default": {
"dest": "scripts/index.js.map",
"removeSourceMappingURL": false,
"minifyJs": false
},
"production": {
"skip": true
}
}
}
],
"copyFilesSettings": {
"whenFileExists": "notify-about-available-change",
"addReferenceToSourceOfOrigin": false
"ignoreDotFilesAndFolders": true
"removeSourceMappingURL": false,
"minifyJs": true,
"minifyJsTerserOptions": {
"compress": {
"sequences": false
},
"mangle": false,
"format": {
"semicolons": false
}
}
}
}
Command line options
$ copy-files-from-to --help
Usage:
copy-files-from-to [--config <config-file>] [--mode <mode-name>] [...]
Examples:
copy-files-from-to
copy-files-from-to --config copy-files-from-to.json
copy-files-from-to --mode production
copy-files-from-to -h
copy-files-from-to --version
Options:
--config <config-file-path> Path to configuration file
When unspecified, it looks for:
1) copy-files-from-to.cjson
2) copy-files-from-to.json
3) package.json
--mode <mode-name> Mode to use for copying the files
When unspecified, it uses "default" mode
--when-file-exists <operation> Override "whenFileExists" setting specified in configuration file
<operation> can be "notify-about-available-change" or "overwrite" or "do-nothing"
--outdated Notify about outdated parts of the configuration file
(takes cue from "latest" property, wherever specified)
--verbose Verbose logging
--silent Reduced logging (log only warnings and errors)
-v --version Output the version number
-h --help Show help
TODO
See TODO.md
About this project
Author
Connect with us
License