UNPKG

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