This repository has been archived by the owner on Aug 15, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
moveFileSystemNode.js
95 lines (85 loc) · 3.43 KB
/
moveFileSystemNode.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
/* eslint-disable import/max-dependencies */
import { rename } from "fs"
import { urlTargetsSameFileSystemPath } from "./internal/urlTargetsSameFileSystemPath.js"
import { statsToType } from "./internal/statsToType.js"
import { assertAndNormalizeFileUrl } from "./assertAndNormalizeFileUrl.js"
import { urlToFileSystemPath } from "./urlToFileSystemPath.js"
import { resolveUrl } from "./resolveUrl.js"
import { ensureParentDirectories } from "./ensureParentDirectories.js"
import { removeFileSystemNode } from "./removeFileSystemNode.js"
import { copyFileSystemNode } from "./copyFileSystemNode.js"
import { readFileSystemNodeStat } from "./readFileSystemNodeStat.js"
import { readSymbolicLink } from "./readSymbolicLink.js"
export const moveFileSystemNode = async (
source,
destination,
{ overwrite = false, allowUseless = false, followLink = true } = {},
) => {
const sourceUrl = assertAndNormalizeFileUrl(source)
let destinationUrl = assertAndNormalizeFileUrl(destination)
const sourcePath = urlToFileSystemPath(sourceUrl)
const sourceStats = await readFileSystemNodeStat(sourceUrl, {
nullIfNotFound: true,
followLink: false,
})
if (!sourceStats) {
throw new Error(`nothing to move from ${sourcePath}`)
}
let destinationStats = await readFileSystemNodeStat(destinationUrl, {
nullIfNotFound: true,
// we force false here but in fact we will follow the destination link
// to know where we will actually move and detect useless move overrite etc..
followLink: false,
})
if (followLink && destinationStats && destinationStats.isSymbolicLink()) {
const target = await readSymbolicLink(destinationUrl)
destinationUrl = resolveUrl(target, destinationUrl)
destinationStats = await readFileSystemNodeStat(destinationUrl, { nullIfNotFound: true })
}
const destinationPath = urlToFileSystemPath(destinationUrl)
if (urlTargetsSameFileSystemPath(sourceUrl, destinationUrl)) {
if (allowUseless) {
return
}
throw new Error(`no move needed for ${sourcePath} because destination and source are the same`)
}
if (destinationStats) {
const sourceType = statsToType(sourceStats)
const destinationType = statsToType(destinationStats)
if (sourceType !== destinationType) {
throw new Error(
`cannot move ${sourceType} from ${sourcePath} to ${destinationPath} because destination exists and is not a ${sourceType} (it's a ${destinationType})`,
)
}
if (!overwrite) {
throw new Error(
`cannot move ${sourceType} from ${sourcePath} to ${destinationPath} because destination exists and overwrite option is disabled`,
)
}
// remove file, link, directory...
await removeFileSystemNode(destinationUrl, { recursive: true })
} else {
await ensureParentDirectories(destinationUrl)
}
await moveNaive(sourcePath, destinationPath, {
handleCrossDeviceError: async () => {
await copyFileSystemNode(sourceUrl, destinationUrl, { preserveStat: true })
await removeFileSystemNode(sourceUrl, { recursive: true })
},
})
}
const moveNaive = (sourcePath, destinationPath, { handleCrossDeviceError = null } = {}) => {
return new Promise((resolve, reject) => {
rename(sourcePath, destinationPath, (error) => {
if (error) {
if (handleCrossDeviceError && error.code === "EXDEV") {
resolve(handleCrossDeviceError(error))
} else {
reject(error)
}
} else {
resolve()
}
})
})
}