-
Notifications
You must be signed in to change notification settings - Fork 774
/
index.js
177 lines (151 loc) · 4.36 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
'use strict'
// most of this code was written by Andrew Kelley
// licensed under the BSD license: see
// https://github.com/andrewrk/node-mv/blob/master/package.json
// this needs a cleanup
const fs = require('graceful-fs')
const ncp = require('../copy/ncp')
const path = require('path')
const remove = require('../remove').remove
const mkdirp = require('../mkdirs').mkdirs
const utimes = require('../util/utimes')
function move (source, dest, options, callback) {
if (typeof options === 'function') {
callback = options
options = {}
}
const shouldMkdirp = ('mkdirp' in options) ? options.mkdirp : true
const overwrite = options.overwrite || options.clobber || false
let optionsCopy = {
preserveTimestamps: ('preserveTimestamps' in options) ? options.preserveTimestamps : true
}
if (shouldMkdirp) {
mkdirs()
} else {
doRename()
}
function mkdirs () {
mkdirp(path.dirname(dest), err => {
if (err) return callback(err)
doRename()
})
}
function doRename () {
if (overwrite) {
fs.rename(source, dest, err => {
if (!err) return callback()
if (err.code === 'ENOTEMPTY' || err.code === 'EEXIST') {
remove(dest, err => {
if (err) return callback(err)
options.overwrite = false // just overwriteed it, no need to do it again
move(source, dest, options, callback)
})
return
}
// weird Windows shit
if (err.code === 'EPERM') {
setTimeout(() => {
remove(dest, err => {
if (err) return callback(err)
options.overwrite = false
move(source, dest, options, callback)
})
}, 200)
return
}
if (err.code !== 'EXDEV') return callback(err)
moveAcrossDevice(source, dest, overwrite, optionsCopy, callback)
})
} else {
fs.link(source, dest, err => {
if (err) {
if (err.code === 'EXDEV' || err.code === 'EISDIR' || err.code === 'EPERM' || err.code === 'ENOTSUP') {
moveAcrossDevice(source, dest, overwrite, optionsCopy, callback)
return
}
callback(err)
return
}
fs.unlink(source, callback)
})
}
}
}
function moveAcrossDevice (source, dest, overwrite, optionsCopy, callback) {
fs.stat(source, (err, stat) => {
if (err) {
callback(err)
return
}
let optionsTemp = Object.assign({}, optionsCopy, {
stat: stat
})
if (stat.isDirectory()) {
moveDirAcrossDevice(source, dest, overwrite, optionsTemp, callback)
} else {
moveFileAcrossDevice(source, dest, overwrite, optionsTemp, callback)
}
})
}
function moveFileAcrossDevice (source, dest, overwrite, optionsTemp, callback) {
const flags = overwrite ? 'w' : 'wx'
const ins = fs.createReadStream(source)
const outs = fs.createWriteStream(dest, { flags })
ins.on('error', err => {
ins.destroy()
outs.destroy()
outs.removeListener('close', onClose)
// may want to create a directory but `out` line above
// creates an empty file for us: See #108
// don't care about error here
fs.unlink(dest, () => {
// note: `err` here is from the input stream errror
if (err.code === 'EISDIR' || err.code === 'EPERM') {
moveDirAcrossDevice(source, dest, overwrite, callback)
} else {
// callback(err)
if (optionsTemp.preserveTimestamps) {
utimes.utimesMillis(dest, optionsTemp.stat.atime, optionsTemp.stat.mtime, function (err) {
return callback(err)
})
} else {
callback(err)
}
}
})
})
outs.on('error', err => {
ins.destroy()
outs.destroy()
outs.removeListener('close', onClose)
callback(err)
})
outs.once('close', onClose)
ins.pipe(outs)
function onClose () {
fs.unlink(source, callback)
}
}
function moveDirAcrossDevice (source, dest, overwrite, optionsTemp, callback) {
const options = {
preserveTimestamps: optionsTemp.preserveTimestamps,
overwrite: false
}
if (overwrite) {
remove(dest, err => {
if (err) return callback(err)
startNcp()
})
} else {
startNcp()
}
function startNcp () {
ncp(source, dest, options, err => {
if (err) return callback(err)
remove(source, callback)
})
}
}
module.exports = {
move
}