1 | # copy-files-from-to
|
2 | Copy files from one path to another, based on the instructions provided in a configuration file.
|
3 |
|
4 | # Use cases
|
5 |
|
6 | * This tool is useful when a few files need to be copied / updated frequently
|
7 | * This tool works as a basic alternative for `npm` / `bower` when you wish to copy the scripts out of `node_modules` / `bower_components` folder
|
8 | * 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
|
9 |
|
10 |
|
11 | # Installation
|
12 | ```sh
|
13 | $ npm install -g copy-files-from-to
|
14 | ```
|
15 |
|
16 | # How to use
|
17 | Create a file, say, `copy-files-from-to.json` or `copy-files-from-to.cjson` [(JSON with comments)](https://github.com/kof/node-cjson) in your project and refer to the following usage examples.
|
18 |
|
19 | ## Basic usage
|
20 | Sample file: [copy-file-from-to.json](test/basic-usage/copy-files-from-to.json)
|
21 | ```json
|
22 | {
|
23 | "copyFiles": [
|
24 | {
|
25 | "from": "node_modules/jquery/dist/jquery.js",
|
26 | "to": "scripts/jquery/jquery.js"
|
27 | },
|
28 | {
|
29 | "from": "https://raw.githubusercontent.com/webextensions/console-panel/master/src/console-panel.js",
|
30 | "to": "scripts/console-panel/console-panel.js"
|
31 | },
|
32 | {
|
33 | "from": "https://raw.githubusercontent.com/webextensions/console-panel/master/src/console-panel.css",
|
34 | "to": "scripts/console-panel/console-panel.css"
|
35 | }
|
36 | ]
|
37 | }
|
38 | ```
|
39 |
|
40 | ### Command and output
|
41 | ```
|
42 | $ copy-files-from-to
|
43 | ```
|
44 |
|
45 | ```
|
46 | Reading copy instructions from file copy-files-from-to.json
|
47 |
|
48 | Starting copy operation in "default" mode:
|
49 | ✓ Copied [ utf8 ] node_modules/jquery/dist/jquery.js to scripts/jquery/jquery.js
|
50 | ✓ Copied [binary] assets/logo.png to build/logo.png
|
51 | ✓ Copied [remote] https://raw.githubusercontent.com/webextensions/console-panel/master/src/console-panel.css to scripts/console-panel/console-panel.css
|
52 | ✓ Copied [remote] https://raw.githubusercontent.com/webextensions/console-panel/master/src/console-panel.js to scripts/console-panel/console-panel.js
|
53 | ```
|
54 |
|
55 |
|
56 | ## Advanced usage
|
57 | Sample file: [copy-files-from-to.cjson](test/advanced-usage/copy-files-from-to.cjson)
|
58 | ```js
|
59 | // This is a CJSON file (JSON with comments)
|
60 | {
|
61 | "copyFiles": [
|
62 | // In "development" mode, copy from the full version of the library, in all other modes, use the minified version
|
63 | {
|
64 | "from": {
|
65 | "default": "https://raw.githubusercontent.com/jashkenas/underscore/master/underscore-min.js",
|
66 | "development": "https://raw.githubusercontent.com/jashkenas/underscore/master/underscore.js"
|
67 | },
|
68 | "to": "scripts/underscore.js"
|
69 | },
|
70 |
|
71 | // Copy this file only in "pre-production" mode
|
72 | {
|
73 | "from": "https://raw.githubusercontent.com/jashkenas/underscore/master/underscore-min.js.map",
|
74 | "to": {
|
75 | "default": {
|
76 | "skip": true
|
77 | },
|
78 | "pre-production": "scripts/underscore.js.map"
|
79 | }
|
80 | },
|
81 |
|
82 | // Copy this file in "pre-production" and "production" mode only
|
83 | {
|
84 | "from": {
|
85 | "default": {
|
86 | "skip": true
|
87 | },
|
88 | "pre-production": "node_modules/native-promise-only/npo.js",
|
89 | "production": "node_modules/native-promise-only/npo.js"
|
90 | },
|
91 | "to": "scripts/native-promise-only.js"
|
92 | },
|
93 |
|
94 | // Copy this file in all modes except "production" mode
|
95 | {
|
96 | "from": "https://raw.githubusercontent.com/webextensions/console-panel/master/src/console-panel.js",
|
97 | "to": {
|
98 | "default": "scripts/console-panel/console-panel.js",
|
99 | "production": {
|
100 | "skip": true
|
101 | }
|
102 | }
|
103 | },
|
104 |
|
105 | // Copy the files matching the "glob" pattern
|
106 | {
|
107 | "from": "assets/**/*.jpg",
|
108 | "to": "public/images/"
|
109 | }
|
110 | ],
|
111 | "settings": {
|
112 | "whenFileExists": "notify-about-available-change",
|
113 | "uglifyJs": false,
|
114 | "addReferenceToSourceOfOrigin": false
|
115 | }
|
116 | }
|
117 | ```
|
118 |
|
119 | ### Command and output
|
120 |
|
121 | <details>
|
122 | <summary>$ copy-files-from-to</summary>
|
123 | <p>
|
124 |
|
125 | ```
|
126 | Reading copy instructions from file copy-files-from-to.cjson
|
127 |
|
128 | Starting copy operation in "default" mode:
|
129 | ✓ Copied [remote] https://raw.githubusercontent.com/jashkenas/underscore/master/underscore-min.js to scripts/underscore.js
|
130 | ✓ Copied [remote] https://raw.githubusercontent.com/webextensions/console-panel/master/src/console-panel.js to scripts/console-panel/console-panel.js
|
131 | ```
|
132 | </p>
|
133 | </details>
|
134 |
|
135 | <details>
|
136 | <summary>$ copy-files-from-to --mode development</summary>
|
137 | <p>
|
138 |
|
139 | ```
|
140 | Reading copy instructions from file copy-files-from-to.cjson
|
141 |
|
142 | Starting copy operation in "development" mode:
|
143 | ✓ Copied [remote] https://raw.githubusercontent.com/jashkenas/underscore/master/underscore.js to scripts/underscore.js
|
144 | ✓ Copied [remote] https://raw.githubusercontent.com/webextensions/console-panel/master/src/console-panel.js to scripts/console-panel/console-panel.js
|
145 | ```
|
146 | </p>
|
147 | </details>
|
148 |
|
149 | <details>
|
150 | <summary>$ copy-files-from-to --mode production</summary>
|
151 | <p>
|
152 |
|
153 | ```
|
154 | Reading copy instructions from file copy-files-from-to.cjson
|
155 |
|
156 | Starting copy operation in "production" mode:
|
157 | ✓ Copied [ utf8 ] node_modules/native-promise-only/npo.js to scripts/native-promise-only.js
|
158 | ✓ Copied [remote] https://raw.githubusercontent.com/jashkenas/underscore/master/underscore-min.js to scripts/underscore.js
|
159 | ```
|
160 | </p>
|
161 | </details>
|
162 |
|
163 | <details>
|
164 | <summary>$ copy-files-from-to --mode pre-production --config copy-files-from-to.cjson</summary>
|
165 | <p>
|
166 |
|
167 | ```
|
168 | Reading copy instructions from file copy-files-from-to.cjson
|
169 |
|
170 | Starting copy operation in "pre-production" mode:
|
171 | ✓ Copied [ utf8 ] node_modules/native-promise-only/npo.js to scripts/native-promise-only.js
|
172 | ✓ Copied [remote] https://raw.githubusercontent.com/jashkenas/underscore/master/underscore-min.js to scripts/underscore.js
|
173 | ✓ Copied [remote] https://raw.githubusercontent.com/jashkenas/underscore/master/underscore-min.js.map to scripts/underscore.js.map
|
174 | ✓ Copied [remote] https://raw.githubusercontent.com/webextensions/console-panel/master/src/console-panel.js to scripts/console-panel/console-panel.js
|
175 | ```
|
176 | </p>
|
177 | </details>
|
178 |
|
179 | # Configuration
|
180 |
|
181 | ## Structure of "copyFiles" instruction file
|
182 | * You can provide the "copyFiles" instructions in a JSON or [CJSON](https://github.com/kof/node-cjson) file
|
183 | * The file can be structured like:
|
184 | ```js
|
185 | {
|
186 | // copyFiles (required parameter)
|
187 | // Summary: This is the instruction set for the files to be copied
|
188 | // Data type: array (of objects)
|
189 | "copyFiles": [
|
190 | // Using "from" and "to", both, as simple strings
|
191 | {
|
192 | // from (required parameter)
|
193 | // Summary: This contains the path of a file which is either on disk or accessible via "http"/"https" URL
|
194 | // Data type: string, array, or object
|
195 | // Note: When it is set as a string, it would be used for all modes. When it is set as an object, it can be
|
196 | // configured differently for different modes (refer to the next example)
|
197 | // When it is set as an array, it describes an array of glob patterns and settings
|
198 | "from": "http://example.com/index.html",
|
199 |
|
200 | // to (required parameter)
|
201 | // Data type: string or object
|
202 | // Summary: This instruction set would write a file to this path on the disk
|
203 | // Note: When it is set as a string, it would be used for all modes. When it is set as an object, it can be
|
204 | // configured differently for different modes
|
205 | "to": "example-index.html"
|
206 | },
|
207 |
|
208 | // Using "from" as an array
|
209 | {
|
210 | // from (required parameter)
|
211 | // Data type: array
|
212 | // Note: When it is set as an array, it describes an array of glob patterns and settings
|
213 | // Any strings in the array are used as glob patterns.
|
214 | // Any objects are used as fast-glob options: (See https://www.npmjs.com/package/fast-glob)
|
215 | "from": [
|
216 | // Copy all files from the public folder
|
217 | "public/**/*",
|
218 | // A "!" at the beginning of the pattern will ignore any files matching that pattern
|
219 | // This ignores all files in the public/tmp folder
|
220 | "!public/tmp/**/*",
|
221 | // Any objects in the array will be collected together to pass to fast-glob as options
|
222 | // This will copy any files starting with a .*
|
223 | // This will not copy symlinked folders
|
224 | { dot: true, followSymlinkedDirectories: false }
|
225 | ],
|
226 | // to (required parameter)
|
227 | // Data type: string
|
228 | // Summary: This instruction set would write all the files found with the glob patterns and settings
|
229 | // to this folder.
|
230 | // Note: When using glob patterns for the "from" value the target "to" path needs to be a folder
|
231 | "to": "build/"
|
232 | },
|
233 |
|
234 | // Using "from" and "to", both, as objects (and use string based mode entries)
|
235 | {
|
236 | "from": {
|
237 | // The "from" section should contain details about at least one mode
|
238 |
|
239 | // "default" mode (optional parameter, recommended to have)
|
240 | // Summary: "default" mode would be used when the command is executed without any mode or when the command
|
241 | // is executed in a mode which is not described for the given entry
|
242 | // Data type: string or object
|
243 | "default": "node_modules/example/index.js",
|
244 |
|
245 | // <custom> mode (optional parameter if any other mode exists)
|
246 | "development": "node_modules/example/dev.js",
|
247 |
|
248 | // <custom> mode (optional parameter if any other mode exists)
|
249 | "production": "node_modules/example/production.js"
|
250 |
|
251 | // More <custom> mode entries
|
252 | },
|
253 | "to": {
|
254 | "default": "example.js"
|
255 | }
|
256 | },
|
257 |
|
258 | // Using "from" and "to", both, as objects (and use object based mode entries)
|
259 | {
|
260 | "from": {
|
261 | "default": {
|
262 | // src (required parameter)
|
263 | // Summary: This contains the path of a file which is either on disk or accessible via "http"/"https" URL
|
264 | // Data type: string
|
265 | "src": "http://example.com/index-1.0.0.js.map",
|
266 |
|
267 | // latest (optional parameter)
|
268 | // Summary: This contains the path of a file which is either on disk or accessible via "http"/"https" URL
|
269 | // Data type: string
|
270 | // Note: When this tools is executed with "--outdated" parameter, then the file from "latest" would be
|
271 | // compared with the file from "src", and if there are any differences, it would be notified that
|
272 | // an update is available
|
273 | "latest": "http://example.com/index.js.map"
|
274 | }
|
275 | },
|
276 | "to": {
|
277 | "default": {
|
278 | // dest (required parameter, when not using "skip" parameter set as true)
|
279 | // Summary: This instruction set would write a file to this path on the disk
|
280 | // Data type: string
|
281 | "dest": "scripts/index.js.map",
|
282 |
|
283 | // uglifyJs (optional parameter)
|
284 | // Summary: When set to true, this JavaScript file would be uglified before the copy operation
|
285 | // (via https://www.npmjs.com/package/uglify-js)
|
286 | // Data type: boolean
|
287 | // Default value: undefined
|
288 | "uglifyJs": false
|
289 | },
|
290 | "production": {
|
291 | // skip (required parameter, when not using "dest" parameter)
|
292 | // Summary: If you wish to skip a file in some particular mode, add "skip" as true, otherwise a warning
|
293 | // would be raised when you run this tool for that particular mode
|
294 | // Data type: boolean
|
295 | // Default value: false
|
296 | "skip": true
|
297 | }
|
298 | }
|
299 | }
|
300 |
|
301 | // Add more object entries with "from" and "to" details, and you can use any of the supported data types to
|
302 | // represent the values of "from", "to" and their "default" or <custom> modes
|
303 | ],
|
304 |
|
305 | // settings (optional parameter)
|
306 | // Summary: Settings for the copy files operation
|
307 | // Data type: object
|
308 | "settings": {
|
309 | // whenFileExists (optional parameter)
|
310 | // Summary: When the file at "to" path already exists, what action should be taken
|
311 | // Data type: string
|
312 | // Supported values: "do-nothing" / "overwrite" / "notify-about-available-change"
|
313 | // Default value: "do-nothing"
|
314 | "whenFileExists": "notify-about-available-change",
|
315 |
|
316 | // uglifyJs (optional parameter)
|
317 | // Summary: When set to true, the JavaScript files would be uglified before the copy operation
|
318 | // (via https://www.npmjs.com/package/uglify-js)
|
319 | // Data type: boolean
|
320 | // Default value: false
|
321 | "uglifyJs": false,
|
322 |
|
323 | // addReferenceToSourceOfOrigin (optional parameter)
|
324 | // Summary: When set to true, the copy operation would create a file "<to-file-path>.source.txt"
|
325 | // which would contain a link to the "from" path
|
326 | // Data type: boolean
|
327 | // Default value: false
|
328 | "addReferenceToSourceOfOrigin": false
|
329 |
|
330 | // ignoreDotFilesAndFolders (optional parameter)
|
331 | // Summary: When set to true, globbing will ignore files and folders starting with a "." dot.
|
332 | // Data type: boolean
|
333 | // Default value: false
|
334 | "ignoreDotFilesAndFolders": true
|
335 | }
|
336 | }
|
337 | ```
|
338 |
|
339 | ## Command line options
|
340 |
|
341 | ```sh
|
342 | $ copy-files-from-to --help
|
343 | ```
|
344 |
|
345 | ```
|
346 | Usage:
|
347 | copy-files-from-to [--config <config-file>] [--mode <mode-name>] [...]
|
348 |
|
349 | Examples:
|
350 | copy-files-from-to
|
351 | copy-files-from-to --config copy-files-from-to.json
|
352 | copy-files-from-to --mode production
|
353 | copy-files-from-to -h
|
354 | copy-files-from-to --version
|
355 |
|
356 | Options:
|
357 | --config <config-file-path> Path to configuration file
|
358 | When unspecified, it looks for copy-files-from-to.cjson / copy-files-from-to.json
|
359 | --mode <mode-name> Mode to use for copying the files
|
360 | When unspecified, it uses "default" mode
|
361 | --when-file-exists <operation> Override "whenFileExists" setting specified in configuration file
|
362 | <operation> can be "notify-about-available-change" or "overwrite" or "do-nothing"
|
363 | --outdated Notify about outdated parts of the configuration file
|
364 | (takes cue from "latest" property, wherever specified)
|
365 | --verbose Verbose logging
|
366 | -v --version Output the version number
|
367 | -h --help Show help
|
368 | ```
|
369 |
|
370 | # TODO
|
371 |
|
372 | See [TODO.md](TODO.md)
|
373 |
|
374 |
|
375 | # About this project
|
376 |
|
377 | ## Author
|
378 |
|
379 | * Priyank Parashar - [GitHub](https://github.com/paras20xx) | [Twitter](https://twitter.com/paras20xx) | [LinkedIn](https://linkedin.com/in/ParasharPriyank/)
|
380 |
|
381 | ## Connect to us
|
382 |
|
383 | * https://webextensions.org/
|
384 | * [GitHub](https://github.com/webextensions/live-css-editor)
|
385 | * [Twitter](https://twitter.com/webextensions)
|
386 |
|
387 | ## License
|
388 |
|
389 | * [MIT](LICENSE)
|