You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # copy(src, dest, [options, callback])
  2. Copy a file or directory. The directory can have contents. Like `cp -r`.
  3. - `src` `<String>`
  4. - `dest` `<String>`
  5. - `options` `<Object>`
  6. - `overwrite` `<boolean>`: overwrite existing file or directory, default is `true`. _Note that the copy operation will silently fail if you set this to `false` and the destination exists._ Use the `errorOnExist` option to change this behavior.
  7. - `errorOnExist` `<boolean>`: when `overwrite` is `false` and the destination exists, throw an error. Default is `false`.
  8. - `dereference` `<boolean>`: dereference symlinks, default is `false`.
  9. - `preserveTimestamps` `<boolean>`: will set last modification and access times to the ones of the original source files, default is `false`.
  10. - `filter` `<Function>`: Function to filter copied files. Return `true` to include, `false` to exclude. This can also be a RegExp, however this is deprecated (See [issue #239](https://github.com/jprichardson/node-fs-extra/issues/239) for background).
  11. - `callback` `<Function>`
  12. ## Example:
  13. ```js
  14. const fs = require('fs-extra')
  15. fs.copy('/tmp/myfile', '/tmp/mynewfile', err => {
  16. if (err) return console.error(err)
  17. console.log('success!')
  18. }) // copies file
  19. fs.copy('/tmp/mydir', '/tmp/mynewdir', err => {
  20. if (err) return console.error(err)
  21. console.log('success!')
  22. }) // copies directory, even if it has subdirectories or files
  23. // Promise usage:
  24. fs.copy('/tmp/myfile', '/tmp/mynewfile')
  25. .then(() => {
  26. console.log('success!')
  27. })
  28. .catch(err => {
  29. console.error(err)
  30. })
  31. ```
  32. **Using filter function**
  33. ```js
  34. const fs = require('fs-extra')
  35. const filterFunc = (src, dest) => {
  36. // your logic here
  37. // it will be copied if return true
  38. }
  39. fs.copy('/tmp/mydir', '/tmp/mynewdir', { filter: filterFunc }, err => {
  40. if (err) return console.error(err)
  41. console.log('success!')
  42. })
  43. ```