Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / fs-extra / README.md
1 Node.js: fs-extra
2 =================
3
4 [![build status](https://api.travis-ci.org/jprichardson/node-fs-extra.svg)](http://travis-ci.org/jprichardson/node-fs-extra)
5 [![windows Build status](https://img.shields.io/appveyor/ci/jprichardson/node-fs-extra/master.svg?label=windows%20build)](https://ci.appveyor.com/project/jprichardson/node-fs-extra/branch/master)
6 [![downloads per month](http://img.shields.io/npm/dm/fs-extra.svg)](https://www.npmjs.org/package/fs-extra)
7 [![Coverage Status](https://img.shields.io/coveralls/jprichardson/node-fs-extra.svg)](https://coveralls.io/r/jprichardson/node-fs-extra)
8
9
10 `fs-extra` adds file system methods that aren't included in the native `fs` module. It is a drop in replacement for `fs`.
11
12 **NOTE (2016-01-13):** Node v0.10 will be unsupported AFTER Ubuntu LTS releases their next version AND [Amazon Lambda
13 upgrades](http://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html) its Node.js runtime from v0.10.
14 I anticipate this will happen around late spring / summer 2016. Please prepare accordingly. After this, we'll make a strong push
15 for a 1.0.0 release.
16
17
18 Why?
19 ----
20
21 I got tired of including `mkdirp`, `rimraf`, and `cp -r` in most of my projects.
22
23
24
25
26 Installation
27 ------------
28
29     npm install --save fs-extra
30
31
32
33 Usage
34 -----
35
36 `fs-extra` is a drop in replacement for native `fs`. All methods in `fs` are unmodified and attached to `fs-extra`.
37
38 You don't ever need to include the original `fs` module again:
39
40 ```js
41 var fs = require('fs') // this is no longer necessary
42 ```
43
44 you can now do this:
45
46 ```js
47 var fs = require('fs-extra')
48 ```
49
50 or if you prefer to make it clear that you're using `fs-extra` and not `fs`, you may want
51 to name your `fs` variable `fse` like so:
52
53 ```js
54 var fse = require('fs-extra')
55 ```
56
57 you can also keep both, but it's redundant:
58
59 ```js
60 var fs = require('fs')
61 var fse = require('fs-extra')
62 ```
63
64 Sync vs Async
65 -------------
66 Most methods are async by default (they take a callback with an `Error` as first argument).
67
68 Sync methods on the other hand will throw if an error occurs.
69
70 Example:
71
72 ```js
73 var fs = require('fs-extra')
74
75 fs.copy('/tmp/myfile', '/tmp/mynewfile', function (err) {
76   if (err) return console.error(err)
77   console.log("success!")
78 });
79
80 try {
81   fs.copySync('/tmp/myfile', '/tmp/mynewfile')
82   console.log("success!")
83 } catch (err) {
84   console.error(err)
85 }
86 ```
87
88
89 Methods
90 -------
91 - [copy](#copy)
92 - [copySync](#copy)
93 - [createOutputStream](#createoutputstreamfile-options)
94 - [emptyDir](#emptydirdir-callback)
95 - [emptyDirSync](#emptydirdir-callback)
96 - [ensureFile](#ensurefilefile-callback)
97 - [ensureFileSync](#ensurefilefile-callback)
98 - [ensureDir](#ensuredirdir-callback)
99 - [ensureDirSync](#ensuredirdir-callback)
100 - [ensureLink](#ensurelinksrcpath-dstpath-callback)
101 - [ensureLinkSync](#ensurelinksrcpath-dstpath-callback)
102 - [ensureSymlink](#ensuresymlinksrcpath-dstpath-type-callback)
103 - [ensureSymlinkSync](#ensuresymlinksrcpath-dstpath-type-callback)
104 - [mkdirs](#mkdirsdir-callback)
105 - [mkdirsSync](#mkdirsdir-callback)
106 - [move](#movesrc-dest-options-callback)
107 - [outputFile](#outputfilefile-data-options-callback)
108 - [outputFileSync](#outputfilefile-data-options-callback)
109 - [outputJson](#outputjsonfile-data-options-callback)
110 - [outputJsonSync](#outputjsonfile-data-options-callback)
111 - [readJson](#readjsonfile-options-callback)
112 - [readJsonSync](#readjsonfile-options-callback)
113 - [remove](#removedir-callback)
114 - [removeSync](#removedir-callback)
115 - [walk](#walk)
116 - [writeJson](#writejsonfile-object-options-callback)
117 - [writeJsonSync](#writejsonfile-object-options-callback)
118
119
120 **NOTE:** You can still use the native Node.js methods. They are copied over to `fs-extra`.
121
122
123 ### copy()
124
125 **copy(src, dest, [options], callback)**
126
127
128 Copy a file or directory. The directory can have contents. Like `cp -r`.
129
130 Options:
131 - clobber (boolean): overwrite existing file or directory
132 - preserveTimestamps (boolean): will set last modification and access times to the ones of the original source files, default is `false`.
133 - filter: Function or RegExp to filter copied files. If function, return true to include, false to exclude. If RegExp, same as function, where `filter` is `filter.test`.
134
135 Sync: `copySync()`
136
137 Example:
138
139 ```js
140 var fs = require('fs-extra')
141
142 fs.copy('/tmp/myfile', '/tmp/mynewfile', function (err) {
143   if (err) return console.error(err)
144   console.log("success!")
145 }) // copies file
146
147 fs.copy('/tmp/mydir', '/tmp/mynewdir', function (err) {
148   if (err) return console.error(err)
149   console.log('success!')
150 }) // copies directory, even if it has subdirectories or files
151 ```
152
153 ### createOutputStream(file, [options])
154
155 Exactly like `createWriteStream`, but if the directory does not exist, it's created.
156
157 Examples:
158
159 ```js
160 var fs = require('fs-extra')
161
162 // if /tmp/some does not exist, it is created
163 var ws = fs.createOutputStream('/tmp/some/file.txt')
164 ws.write('hello\n')
165 ```
166
167 Note on naming: you'll notice that fs-extra has some methods like `fs.outputJson`, `fs.outputFile`, etc that use the
168 word `output` to denote that if the containing directory does not exist, it should be created. If you can think of a
169 better succinct nomenclature for these methods, please open an issue for discussion. Thanks.
170
171
172 ### emptyDir(dir, [callback])
173
174 Ensures that a directory is empty. Deletes directory contents if the directory is not empty. If the directory does not exist, it is created. The directory itself is not deleted.
175
176 Alias: `emptydir()`
177
178 Sync: `emptyDirSync()`, `emptydirSync()`
179
180 Example:
181
182 ```js
183 var fs = require('fs-extra')
184
185 // assume this directory has a lot of files and folders
186 fs.emptyDir('/tmp/some/dir', function (err) {
187   if (!err) console.log('success!')
188 })
189 ```
190
191
192 ### ensureFile(file, callback)
193
194 Ensures that the file exists. If the file that is requested to be created is in directories that do not exist, these directories are created. If the file already exists, it is **NOT MODIFIED**.
195
196 Alias: `createFile()`
197
198 Sync: `createFileSync()`,`ensureFileSync()`
199
200
201 Example:
202
203 ```js
204 var fs = require('fs-extra')
205
206 var file = '/tmp/this/path/does/not/exist/file.txt'
207 fs.ensureFile(file, function (err) {
208   console.log(err) // => null
209   // file has now been created, including the directory it is to be placed in
210 })
211 ```
212
213
214 ### ensureDir(dir, callback)
215
216 Ensures that the directory exists. If the directory structure does not exist, it is created.
217
218 Sync: `ensureDirSync()`
219
220
221 Example:
222
223 ```js
224 var fs = require('fs-extra')
225
226 var dir = '/tmp/this/path/does/not/exist'
227 fs.ensureDir(dir, function (err) {
228   console.log(err) // => null
229   // dir has now been created, including the directory it is to be placed in
230 })
231 ```
232
233
234 ### ensureLink(srcpath, dstpath, callback)
235
236 Ensures that the link exists. If the directory structure does not exist, it is created.
237
238 Sync: `ensureLinkSync()`
239
240
241 Example:
242
243 ```js
244 var fs = require('fs-extra')
245
246 var srcpath = '/tmp/file.txt'
247 var dstpath = '/tmp/this/path/does/not/exist/file.txt'
248 fs.ensureLink(srcpath, dstpath, function (err) {
249   console.log(err) // => null
250   // link has now been created, including the directory it is to be placed in
251 })
252 ```
253
254
255 ### ensureSymlink(srcpath, dstpath, [type], callback)
256
257 Ensures that the symlink exists. If the directory structure does not exist, it is created.
258
259 Sync: `ensureSymlinkSync()`
260
261
262 Example:
263
264 ```js
265 var fs = require('fs-extra')
266
267 var srcpath = '/tmp/file.txt'
268 var dstpath = '/tmp/this/path/does/not/exist/file.txt'
269 fs.ensureSymlink(srcpath, dstpath, function (err) {
270   console.log(err) // => null
271   // symlink has now been created, including the directory it is to be placed in
272 })
273 ```
274
275
276 ### mkdirs(dir, callback)
277
278 Creates a directory. If the parent hierarchy doesn't exist, it's created. Like `mkdir -p`.
279
280 Alias: `mkdirp()`
281
282 Sync: `mkdirsSync()` / `mkdirpSync()`
283
284
285 Examples:
286
287 ```js
288 var fs = require('fs-extra')
289
290 fs.mkdirs('/tmp/some/long/path/that/prob/doesnt/exist', function (err) {
291   if (err) return console.error(err)
292   console.log("success!")
293 })
294
295 fs.mkdirsSync('/tmp/another/path')
296 ```
297
298
299 ### move(src, dest, [options], callback)
300
301 Moves a file or directory, even across devices.
302
303 Options:
304 - clobber (boolean): overwrite existing file or directory
305 - limit (number): number of concurrent moves, see ncp for more information
306
307 Example:
308
309 ```js
310 var fs = require('fs-extra')
311
312 fs.move('/tmp/somefile', '/tmp/does/not/exist/yet/somefile', function (err) {
313   if (err) return console.error(err)
314   console.log("success!")
315 })
316 ```
317
318
319 ### outputFile(file, data, [options], callback)
320
321 Almost the same as `writeFile` (i.e. it [overwrites](http://pages.citebite.com/v2o5n8l2f5reb)), except that if the parent directory does not exist, it's created. `options` are what you'd pass to [`fs.writeFile()`](https://nodejs.org/api/fs.html#fs_fs_writefile_file_data_options_callback).
322
323 Sync: `outputFileSync()`
324
325
326 Example:
327
328 ```js
329 var fs = require('fs-extra')
330 var file = '/tmp/this/path/does/not/exist/file.txt'
331
332 fs.outputFile(file, 'hello!', function (err) {
333   console.log(err) // => null
334
335   fs.readFile(file, 'utf8', function (err, data) {
336     console.log(data) // => hello!
337   })
338 })
339 ```
340
341
342
343 ### outputJson(file, data, [options], callback)
344
345 Almost the same as `writeJson`, except that if the directory does not exist, it's created.
346 `options` are what you'd pass to [`jsonFile.writeFile()`](https://github.com/jprichardson/node-jsonfile#writefilefilename-options-callback).
347
348 Alias: `outputJSON()`
349
350 Sync: `outputJsonSync()`, `outputJSONSync()`
351
352
353 Example:
354
355 ```js
356 var fs = require('fs-extra')
357 var file = '/tmp/this/path/does/not/exist/file.txt'
358
359 fs.outputJson(file, {name: 'JP'}, function (err) {
360   console.log(err) // => null
361
362   fs.readJson(file, function(err, data) {
363     console.log(data.name) // => JP
364   })
365 })
366 ```
367
368
369
370 ### readJson(file, [options], callback)
371
372 Reads a JSON file and then parses it into an object. `options` are the same
373 that you'd pass to [`jsonFile.readFile`](https://github.com/jprichardson/node-jsonfile#readfilefilename-options-callback).
374
375 Alias: `readJSON()`
376
377 Sync: `readJsonSync()`, `readJSONSync()`
378
379
380 Example:
381
382 ```js
383 var fs = require('fs-extra')
384
385 fs.readJson('./package.json', function (err, packageObj) {
386   console.log(packageObj.version) // => 0.1.3
387 })
388 ```
389
390 `readJsonSync()` can take a `throws` option set to `false` and it won't throw if the JSON is invalid. Example:
391
392 ```js
393 var fs = require('fs-extra')
394 var file = path.join('/tmp/some-invalid.json')
395 var data = '{not valid JSON'
396 fs.writeFileSync(file, data)
397
398 var obj = fs.readJsonSync(file, {throws: false})
399 console.log(obj) // => null
400 ```
401
402
403 ### remove(dir, callback)
404
405 Removes a file or directory. The directory can have contents. Like `rm -rf`.
406
407 Sync: `removeSync()`
408
409
410 Examples:
411
412 ```js
413 var fs = require('fs-extra')
414
415 fs.remove('/tmp/myfile', function (err) {
416   if (err) return console.error(err)
417
418   console.log('success!')
419 })
420
421 fs.removeSync('/home/jprichardson') //I just deleted my entire HOME directory.
422 ```
423
424 ### walk()
425
426 **walk(dir, [streamOptions])**
427
428 The function `walk()` from the module [`klaw`](https://github.com/jprichardson/node-klaw).
429
430 Returns a [Readable stream](https://nodejs.org/api/stream.html#stream_class_stream_readable) that iterates
431 through every file and directory starting with `dir` as the root. Every `read()` or `data` event
432 returns an object with two properties: `path` and `stats`. `path` is the full path of the file and
433 `stats` is an instance of [fs.Stats](https://nodejs.org/api/fs.html#fs_class_fs_stats).
434
435 Streams 1 (push) example:
436
437 ```js
438 var items = [] // files, directories, symlinks, etc
439 fse.walk(TEST_DIR)
440   .on('data', function (item) {
441     items.push(item.path)
442   })
443   .on('end', function () {
444     console.dir(items) // => [ ... array of files]
445   })
446 ```
447
448 Streams 2 & 3 (pull) example:
449
450 ```js
451 var items = [] // files, directories, symlinks, etc
452 fse.walk(TEST_DIR)
453   .on('readable', function () {
454     var item
455     while ((item = this.read())) {
456       items.push(item.path)
457     }
458   })
459   .on('end', function () {
460     console.dir(items) // => [ ... array of files]
461   })
462 ```
463
464 If you're not sure of the differences on Node.js streams 1, 2, 3 then I'd
465 recommend this resource as a good starting point: https://strongloop.com/strongblog/whats-new-io-js-beta-streams3/.
466
467 **See [`klaw` documentation](https://github.com/jprichardson/node-klaw) for more detailed usage.**
468
469
470 ### writeJson(file, object, [options], callback)
471
472 Writes an object to a JSON file. `options` are the same that
473 you'd pass to [`jsonFile.writeFile()`](https://github.com/jprichardson/node-jsonfile#writefilefilename-options-callback).
474
475 Alias: `writeJSON()`
476
477 Sync: `writeJsonSync()`, `writeJSONSync()`
478
479 Example:
480
481 ```js
482 var fs = require('fs-extra')
483 fs.writeJson('./package.json', {name: 'fs-extra'}, function (err) {
484   console.log(err)
485 })
486 ```
487
488
489 Third Party
490 -----------
491
492 ### Promises
493
494 Use [Bluebird](https://github.com/petkaantonov/bluebird). See https://github.com/petkaantonov/bluebird/blob/master/API.md#promisification. `fs-extra` is
495 explicitly listed as supported.
496
497 ```js
498 var Promise = require('bluebird')
499 var fs = Promise.promisifyAll(require('fs-extra'))
500 ```
501
502 Or you can use the package [`fs-extra-promise`](https://github.com/overlookmotel/fs-extra-promise) that marries the two together.
503
504
505 ### TypeScript
506
507 If you like TypeScript, you can use `fs-extra` with it: https://github.com/borisyankov/DefinitelyTyped/tree/master/fs-extra
508
509
510 ### File / Directory Watching
511
512 If you want to watch for changes to files or directories, then you should use [chokidar](https://github.com/paulmillr/chokidar).
513
514
515 ### Misc.
516
517 - [mfs](https://github.com/cadorn/mfs) - Monitor your fs-extra calls.
518
519
520
521 Hacking on fs-extra
522 -------------------
523
524 Wanna hack on `fs-extra`? Great! Your help is needed! [fs-extra is one of the most depended upon Node.js packages](http://nodei.co/npm/fs-extra.png?downloads=true&downloadRank=true&stars=true). This project
525 uses [JavaScript Standard Style](https://github.com/feross/standard) - if the name or style choices bother you,
526 you're gonna have to get over it :) If `standard` is good enough for `npm`, it's good enough for `fs-extra`.
527
528 [![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)
529
530 What's needed?
531 - First, take a look at existing issues. Those are probably going to be where the priority lies.
532 - More tests for edge cases. Specifically on different platforms. There can never be enough tests.
533 - Really really help with the Windows tests. See appveyor outputs for more info.
534 - Improve test coverage. See coveralls output for more info.
535 - A directory walker. Probably this one: https://github.com/thlorenz/readdirp imported into `fs-extra`.
536 - After the directory walker is integrated, any function that needs to traverse directories like
537 `copy`, `remove`, or `mkdirs` should be built on top of it.
538
539 Note: If you make any big changes, **you should definitely post an issue for discussion first.**
540
541
542 Naming
543 ------
544
545 I put a lot of thought into the naming of these functions. Inspired by @coolaj86's request. So he deserves much of the credit for raising the issue. See discussion(s) here:
546
547 * https://github.com/jprichardson/node-fs-extra/issues/2
548 * https://github.com/flatiron/utile/issues/11
549 * https://github.com/ryanmcgrath/wrench-js/issues/29
550 * https://github.com/substack/node-mkdirp/issues/17
551
552 First, I believe that in as many cases as possible, the [Node.js naming schemes](http://nodejs.org/api/fs.html) should be chosen. However, there are problems with the Node.js own naming schemes.
553
554 For example, `fs.readFile()` and `fs.readdir()`: the **F** is capitalized in *File* and the **d** is not capitalized in *dir*. Perhaps a bit pedantic, but they should still be consistent. Also, Node.js has chosen a lot of POSIX naming schemes, which I believe is great. See: `fs.mkdir()`, `fs.rmdir()`, `fs.chown()`, etc.
555
556 We have a dilemma though. How do you consistently name methods that perform the following POSIX commands: `cp`, `cp -r`, `mkdir -p`, and `rm -rf`?
557
558 My perspective: when in doubt, err on the side of simplicity. A directory is just a hierarchical grouping of directories and files. Consider that for a moment. So when you want to copy it or remove it, in most cases you'll want to copy or remove all of its contents. When you want to create a directory, if the directory that it's suppose to be contained in does not exist, then in most cases you'll want to create that too.
559
560 So, if you want to remove a file or a directory regardless of whether it has contents, just call `fs.remove(path)`. If you want to copy a file or a directory whether it has contents, just call `fs.copy(source, destination)`. If you want to create a directory regardless of whether its parent directories exist, just call `fs.mkdirs(path)` or `fs.mkdirp(path)`.
561
562
563 Credit
564 ------
565
566 `fs-extra` wouldn't be possible without using the modules from the following authors:
567
568 - [Isaac Shlueter](https://github.com/isaacs)
569 - [Charlie McConnel](https://github.com/avianflu)
570 - [James Halliday](https://github.com/substack)
571 - [Andrew Kelley](https://github.com/andrewrk)
572
573
574
575
576 License
577 -------
578
579 Licensed under MIT
580
581 Copyright (c) 2011-2016 [JP Richardson](https://github.com/jprichardson)
582
583 [1]: http://nodejs.org/docs/latest/api/fs.html
584
585
586 [jsonfile]: https://github.com/jprichardson/node-jsonfile