Newer
Older
CVSS_3.0_GUI / dist / CVSS_3.0_Calc-1.0.0-linux-x64 / node_modules / nwjs-builder-phoenix / node_modules / fs-extra / lib / copy / copy.js
root on 7 May 2019 1 KB Initial commit
  1. 'use strict'
  2.  
  3. const fs = require('graceful-fs')
  4. const path = require('path')
  5. const ncp = require('./ncp')
  6. const mkdir = require('../mkdirs')
  7. const pathExists = require('../path-exists').pathExists
  8.  
  9. function copy (src, dest, options, callback) {
  10. if (typeof options === 'function' && !callback) {
  11. callback = options
  12. options = {}
  13. } else if (typeof options === 'function' || options instanceof RegExp) {
  14. options = {filter: options}
  15. }
  16. callback = callback || function () {}
  17. options = options || {}
  18.  
  19. // Warn about using preserveTimestamps on 32-bit node:
  20. if (options.preserveTimestamps && process.arch === 'ia32') {
  21. console.warn(`fs-extra: Using the preserveTimestamps option in 32-bit node is not recommended;\n
  22. see https://github.com/jprichardson/node-fs-extra/issues/269`)
  23. }
  24.  
  25. // don't allow src and dest to be the same
  26. const basePath = process.cwd()
  27. const currentPath = path.resolve(basePath, src)
  28. const targetPath = path.resolve(basePath, dest)
  29. if (currentPath === targetPath) return callback(new Error('Source and destination must not be the same.'))
  30.  
  31. fs.lstat(src, (err, stats) => {
  32. if (err) return callback(err)
  33.  
  34. let dir = null
  35. if (stats.isDirectory()) {
  36. const parts = dest.split(path.sep)
  37. parts.pop()
  38. dir = parts.join(path.sep)
  39. } else {
  40. dir = path.dirname(dest)
  41. }
  42.  
  43. pathExists(dir, (err, dirExists) => {
  44. if (err) return callback(err)
  45. if (dirExists) return ncp(src, dest, options, callback)
  46. mkdir.mkdirs(dir, err => {
  47. if (err) return callback(err)
  48. ncp(src, dest, options, callback)
  49. })
  50. })
  51. })
  52. }
  53.  
  54. module.exports = copy
Buy Me A Coffee