index.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. const { dirname, join, resolve, relative, isAbsolute } = require('path')
  2. const rimraf_ = require('rimraf')
  3. const { promisify } = require('util')
  4. const {
  5. access: access_,
  6. accessSync,
  7. copyFile: copyFile_,
  8. copyFileSync,
  9. unlink: unlink_,
  10. unlinkSync,
  11. readdir: readdir_,
  12. readdirSync,
  13. rename: rename_,
  14. renameSync,
  15. stat: stat_,
  16. statSync,
  17. lstat: lstat_,
  18. lstatSync,
  19. symlink: symlink_,
  20. symlinkSync,
  21. readlink: readlink_,
  22. readlinkSync
  23. } = require('fs')
  24. const access = promisify(access_)
  25. const copyFile = promisify(copyFile_)
  26. const unlink = promisify(unlink_)
  27. const readdir = promisify(readdir_)
  28. const rename = promisify(rename_)
  29. const stat = promisify(stat_)
  30. const lstat = promisify(lstat_)
  31. const symlink = promisify(symlink_)
  32. const readlink = promisify(readlink_)
  33. const rimraf = promisify(rimraf_)
  34. const rimrafSync = rimraf_.sync
  35. const mkdirp = require('mkdirp')
  36. const pathExists = async path => {
  37. try {
  38. await access(path)
  39. return true
  40. } catch (er) {
  41. return er.code !== 'ENOENT'
  42. }
  43. }
  44. const pathExistsSync = path => {
  45. try {
  46. accessSync(path)
  47. return true
  48. } catch (er) {
  49. return er.code !== 'ENOENT'
  50. }
  51. }
  52. const moveFile = async (source, destination, options = {}, root = true, symlinks = []) => {
  53. if (!source || !destination) {
  54. throw new TypeError('`source` and `destination` file required')
  55. }
  56. options = {
  57. overwrite: true,
  58. ...options
  59. }
  60. if (!options.overwrite && await pathExists(destination)) {
  61. throw new Error(`The destination file exists: ${destination}`)
  62. }
  63. await mkdirp(dirname(destination))
  64. try {
  65. await rename(source, destination)
  66. } catch (error) {
  67. if (error.code === 'EXDEV' || error.code === 'EPERM') {
  68. const sourceStat = await lstat(source)
  69. if (sourceStat.isDirectory()) {
  70. const files = await readdir(source)
  71. await Promise.all(files.map((file) => moveFile(join(source, file), join(destination, file), options, false, symlinks)))
  72. } else if (sourceStat.isSymbolicLink()) {
  73. symlinks.push({ source, destination })
  74. } else {
  75. await copyFile(source, destination)
  76. }
  77. } else {
  78. throw error
  79. }
  80. }
  81. if (root) {
  82. await Promise.all(symlinks.map(async ({ source, destination }) => {
  83. let target = await readlink(source)
  84. // junction symlinks in windows will be absolute paths, so we need to make sure they point to the destination
  85. if (isAbsolute(target))
  86. target = resolve(destination, relative(source, target))
  87. // try to determine what the actual file is so we can create the correct type of symlink in windows
  88. let targetStat
  89. try {
  90. targetStat = await stat(resolve(dirname(source), target))
  91. } catch (err) {}
  92. await symlink(target, destination, targetStat && targetStat.isDirectory() ? 'junction' : 'file')
  93. }))
  94. await rimraf(source)
  95. }
  96. }
  97. const moveFileSync = (source, destination, options = {}, root = true, symlinks = []) => {
  98. if (!source || !destination) {
  99. throw new TypeError('`source` and `destination` file required')
  100. }
  101. options = {
  102. overwrite: true,
  103. ...options
  104. }
  105. if (!options.overwrite && pathExistsSync(destination)) {
  106. throw new Error(`The destination file exists: ${destination}`)
  107. }
  108. mkdirp.sync(dirname(destination))
  109. try {
  110. renameSync(source, destination)
  111. } catch (error) {
  112. if (error.code === 'EXDEV' || error.code === 'EPERM') {
  113. const sourceStat = lstatSync(source)
  114. if (sourceStat.isDirectory()) {
  115. const files = readdirSync(source)
  116. for (const file of files) {
  117. moveFileSync(join(source, file), join(destination, file), options, false, symlinks)
  118. }
  119. } else if (sourceStat.isSymbolicLink()) {
  120. symlinks.push({ source, destination })
  121. } else {
  122. copyFileSync(source, destination)
  123. }
  124. } else {
  125. throw error
  126. }
  127. }
  128. if (root) {
  129. for (const { source, destination } of symlinks) {
  130. let target = readlinkSync(source)
  131. // junction symlinks in windows will be absolute paths, so we need to make sure they point to the destination
  132. if (isAbsolute(target))
  133. target = resolve(destination, relative(source, target))
  134. // try to determine what the actual file is so we can create the correct type of symlink in windows
  135. let targetStat
  136. try {
  137. targetStat = statSync(resolve(dirname(source), target))
  138. } catch (err) {}
  139. symlinkSync(target, destination, targetStat && targetStat.isDirectory() ? 'junction' : 'file')
  140. }
  141. rimrafSync(source)
  142. }
  143. }
  144. module.exports = moveFile
  145. module.exports.sync = moveFileSync