Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

README.md 8.5 KiB

3 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. Node.js: fs-extra
  2. =================
  3. `fs-extra` adds file system methods that aren't included in the native `fs` module and adds promise support to the `fs` methods. It should be a drop in replacement for `fs`.
  4. [![npm Package](https://img.shields.io/npm/v/fs-extra.svg?style=flat-square)](https://www.npmjs.org/package/fs-extra)
  5. [![build status](https://api.travis-ci.org/jprichardson/node-fs-extra.svg)](http://travis-ci.org/jprichardson/node-fs-extra)
  6. [![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)
  7. [![downloads per month](http://img.shields.io/npm/dm/fs-extra.svg)](https://www.npmjs.org/package/fs-extra)
  8. [![Coverage Status](https://img.shields.io/coveralls/jprichardson/node-fs-extra.svg)](https://coveralls.io/r/jprichardson/node-fs-extra)
  9. <a href="https://github.com/feross/standard"><img src="https://cdn.rawgit.com/feross/standard/master/sticker.svg" alt="Standard JavaScript" width="100"></a>
  10. Why?
  11. ----
  12. I got tired of including `mkdirp`, `rimraf`, and `ncp` in most of my projects.
  13. Installation
  14. ------------
  15. npm install --save fs-extra
  16. Usage
  17. -----
  18. `fs-extra` is a drop in replacement for native `fs`. All methods in `fs` are attached to `fs-extra`. All `fs` methods return promises if the callback isn't passed.
  19. You don't ever need to include the original `fs` module again:
  20. ```js
  21. const fs = require('fs') // this is no longer necessary
  22. ```
  23. you can now do this:
  24. ```js
  25. const fs = require('fs-extra')
  26. ```
  27. or if you prefer to make it clear that you're using `fs-extra` and not `fs`, you may want
  28. to name your `fs` variable `fse` like so:
  29. ```js
  30. const fse = require('fs-extra')
  31. ```
  32. you can also keep both, but it's redundant:
  33. ```js
  34. const fs = require('fs')
  35. const fse = require('fs-extra')
  36. ```
  37. Sync vs Async
  38. -------------
  39. Most methods are async by default. All async methods will return a promise if the callback isn't passed.
  40. Sync methods on the other hand will throw if an error occurs.
  41. Example:
  42. ```js
  43. const fs = require('fs-extra')
  44. // Async with promises:
  45. fs.copy('/tmp/myfile', '/tmp/mynewfile')
  46. .then(() => console.log('success!'))
  47. .catch(err => console.error(err))
  48. // Async with callbacks:
  49. fs.copy('/tmp/myfile', '/tmp/mynewfile', err => {
  50. if (err) return console.error(err)
  51. console.log('success!')
  52. })
  53. // Sync:
  54. try {
  55. fs.copySync('/tmp/myfile', '/tmp/mynewfile')
  56. console.log('success!')
  57. } catch (err) {
  58. console.error(err)
  59. }
  60. ```
  61. Methods
  62. -------
  63. ### Async
  64. - [copy](docs/copy.md)
  65. - [emptyDir](docs/emptyDir.md)
  66. - [ensureFile](docs/ensureFile.md)
  67. - [ensureDir](docs/ensureDir.md)
  68. - [ensureLink](docs/ensureLink.md)
  69. - [ensureSymlink](docs/ensureSymlink.md)
  70. - [mkdirs](docs/ensureDir.md)
  71. - [move](docs/move.md)
  72. - [outputFile](docs/outputFile.md)
  73. - [outputJson](docs/outputJson.md)
  74. - [pathExists](docs/pathExists.md)
  75. - [readJson](docs/readJson.md)
  76. - [remove](docs/remove.md)
  77. - [writeJson](docs/writeJson.md)
  78. ### Sync
  79. - [copySync](docs/copy-sync.md)
  80. - [emptyDirSync](docs/emptyDir-sync.md)
  81. - [ensureFileSync](docs/ensureFile-sync.md)
  82. - [ensureDirSync](docs/ensureDir-sync.md)
  83. - [ensureLinkSync](docs/ensureLink-sync.md)
  84. - [ensureSymlinkSync](docs/ensureSymlink-sync.md)
  85. - [mkdirsSync](docs/ensureDir-sync.md)
  86. - [moveSync](docs/move-sync.md)
  87. - [outputFileSync](docs/outputFile-sync.md)
  88. - [outputJsonSync](docs/outputJson-sync.md)
  89. - [pathExistsSync](docs/pathExists-sync.md)
  90. - [readJsonSync](docs/readJson-sync.md)
  91. - [removeSync](docs/remove-sync.md)
  92. - [writeJsonSync](docs/writeJson-sync.md)
  93. **NOTE:** You can still use the native Node.js methods. They are promisified and copied over to `fs-extra`.
  94. ### What happened to `walk()` and `walkSync()`?
  95. They were removed from `fs-extra` in v2.0.0. If you need the functionality, `walk` and `walkSync` are available as separate packages, [`klaw`](https://github.com/jprichardson/node-klaw) and [`klaw-sync`](https://github.com/manidlou/node-klaw-sync).
  96. Third Party
  97. -----------
  98. ### TypeScript
  99. If you like TypeScript, you can use `fs-extra` with it: https://github.com/borisyankov/DefinitelyTyped/tree/master/fs-extra
  100. ### File / Directory Watching
  101. If you want to watch for changes to files or directories, then you should use [chokidar](https://github.com/paulmillr/chokidar).
  102. ### Misc.
  103. - [mfs](https://github.com/cadorn/mfs) - Monitor your fs-extra calls.
  104. Hacking on fs-extra
  105. -------------------
  106. 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
  107. uses [JavaScript Standard Style](https://github.com/feross/standard) - if the name or style choices bother you,
  108. you're gonna have to get over it :) If `standard` is good enough for `npm`, it's good enough for `fs-extra`.
  109. [![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)
  110. What's needed?
  111. - First, take a look at existing issues. Those are probably going to be where the priority lies.
  112. - More tests for edge cases. Specifically on different platforms. There can never be enough tests.
  113. - Improve test coverage. See coveralls output for more info.
  114. Note: If you make any big changes, **you should definitely file an issue for discussion first.**
  115. ### Running the Test Suite
  116. fs-extra contains hundreds of tests.
  117. - `npm run lint`: runs the linter ([standard](http://standardjs.com/))
  118. - `npm run unit`: runs the unit tests
  119. - `npm test`: runs both the linter and the tests
  120. ### Windows
  121. If you run the tests on the Windows and receive a lot of symbolic link `EPERM` permission errors, it's
  122. because on Windows you need elevated privilege to create symbolic links. You can add this to your Windows's
  123. account by following the instructions here: http://superuser.com/questions/104845/permission-to-make-symbolic-links-in-windows-7
  124. However, I didn't have much luck doing this.
  125. Since I develop on Mac OS X, I use VMWare Fusion for Windows testing. I create a shared folder that I map to a drive on Windows.
  126. I open the `Node.js command prompt` and run as `Administrator`. I then map the network drive running the following command:
  127. net use z: "\\vmware-host\Shared Folders"
  128. I can then navigate to my `fs-extra` directory and run the tests.
  129. Naming
  130. ------
  131. 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:
  132. * https://github.com/jprichardson/node-fs-extra/issues/2
  133. * https://github.com/flatiron/utile/issues/11
  134. * https://github.com/ryanmcgrath/wrench-js/issues/29
  135. * https://github.com/substack/node-mkdirp/issues/17
  136. 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.
  137. 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.
  138. 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`?
  139. 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.
  140. 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)`.
  141. Credit
  142. ------
  143. `fs-extra` wouldn't be possible without using the modules from the following authors:
  144. - [Isaac Shlueter](https://github.com/isaacs)
  145. - [Charlie McConnel](https://github.com/avianflu)
  146. - [James Halliday](https://github.com/substack)
  147. - [Andrew Kelley](https://github.com/andrewrk)
  148. License
  149. -------
  150. Licensed under MIT
  151. Copyright (c) 2011-2017 [JP Richardson](https://github.com/jprichardson)
  152. [1]: http://nodejs.org/docs/latest/api/fs.html
  153. [jsonfile]: https://github.com/jprichardson/node-jsonfile