source: trip-planner-front/node_modules/fs-extra/lib/move/move.js@ eed0bf8

Last change on this file since eed0bf8 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 1.6 KB
Line 
1'use strict'
2
3const fs = require('graceful-fs')
4const path = require('path')
5const copy = require('../copy').copy
6const remove = require('../remove').remove
7const mkdirp = require('../mkdirs').mkdirp
8const pathExists = require('../path-exists').pathExists
9const stat = require('../util/stat')
10
11function move (src, dest, opts, cb) {
12 if (typeof opts === 'function') {
13 cb = opts
14 opts = {}
15 }
16
17 const overwrite = opts.overwrite || opts.clobber || false
18
19 stat.checkPaths(src, dest, 'move', (err, stats) => {
20 if (err) return cb(err)
21 const { srcStat } = stats
22 stat.checkParentPaths(src, srcStat, dest, 'move', err => {
23 if (err) return cb(err)
24 mkdirp(path.dirname(dest), err => {
25 if (err) return cb(err)
26 return doRename(src, dest, overwrite, cb)
27 })
28 })
29 })
30}
31
32function doRename (src, dest, overwrite, cb) {
33 if (overwrite) {
34 return remove(dest, err => {
35 if (err) return cb(err)
36 return rename(src, dest, overwrite, cb)
37 })
38 }
39 pathExists(dest, (err, destExists) => {
40 if (err) return cb(err)
41 if (destExists) return cb(new Error('dest already exists.'))
42 return rename(src, dest, overwrite, cb)
43 })
44}
45
46function rename (src, dest, overwrite, cb) {
47 fs.rename(src, dest, err => {
48 if (!err) return cb()
49 if (err.code !== 'EXDEV') return cb(err)
50 return moveAcrossDevice(src, dest, overwrite, cb)
51 })
52}
53
54function moveAcrossDevice (src, dest, overwrite, cb) {
55 const opts = {
56 overwrite,
57 errorOnExist: true
58 }
59 copy(src, dest, opts, err => {
60 if (err) return cb(err)
61 return remove(src, cb)
62 })
63}
64
65module.exports = move
Note: See TracBrowser for help on using the repository browser.