Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

move-sync.js 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. 'use strict'
  2. const fs = require('graceful-fs')
  3. const path = require('path')
  4. const copySync = require('../copy-sync').copySync
  5. const removeSync = require('../remove').removeSync
  6. const mkdirpSync = require('../mkdirs').mkdirpSync
  7. const stat = require('../util/stat')
  8. function moveSync (src, dest, opts) {
  9. opts = opts || {}
  10. const overwrite = opts.overwrite || opts.clobber || false
  11. const { srcStat } = stat.checkPathsSync(src, dest, 'move')
  12. stat.checkParentPathsSync(src, srcStat, dest, 'move')
  13. mkdirpSync(path.dirname(dest))
  14. return doRename(src, dest, overwrite)
  15. }
  16. function doRename (src, dest, overwrite) {
  17. if (overwrite) {
  18. removeSync(dest)
  19. return rename(src, dest, overwrite)
  20. }
  21. if (fs.existsSync(dest)) throw new Error('dest already exists.')
  22. return rename(src, dest, overwrite)
  23. }
  24. function rename (src, dest, overwrite) {
  25. try {
  26. fs.renameSync(src, dest)
  27. } catch (err) {
  28. if (err.code !== 'EXDEV') throw err
  29. return moveAcrossDevice(src, dest, overwrite)
  30. }
  31. }
  32. function moveAcrossDevice (src, dest, overwrite) {
  33. const opts = {
  34. overwrite,
  35. errorOnExist: true
  36. }
  37. copySync(src, dest, opts)
  38. return removeSync(src)
  39. }
  40. module.exports = moveSync