-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
106 lines (88 loc) · 3.02 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
import process from 'node:process';
import path from 'node:path';
import fs, {promises as fsP} from 'node:fs';
import {pathExists} from 'path-exists';
const resolvePath = (cwd, sourcePath, destinationPath) => {
sourcePath = path.resolve(cwd, sourcePath);
destinationPath = path.resolve(cwd, destinationPath);
return {
sourcePath,
destinationPath,
};
};
const validatePathsExist = (sourcePath, destinationPath, suffix = 'Path') => {
if (!sourcePath || !destinationPath) {
throw new TypeError(`\`source${suffix}\` and \`destination${suffix}\` required`);
}
};
const validateSameDirectory = (source, destination) => {
if (path.dirname(source) !== path.dirname(destination)) {
throw new Error('`source` and `destination` must be in the same directory');
}
};
const _moveFile = async (sourcePath, destinationPath, {overwrite = true, cwd = process.cwd(), directoryMode, validateDirectory = false} = {}) => {
if (cwd) {
({sourcePath, destinationPath} = resolvePath(cwd, sourcePath, destinationPath));
}
if (validateDirectory) {
validateSameDirectory(sourcePath, destinationPath);
}
if (!overwrite && await pathExists(destinationPath)) {
throw new Error(`The destination file exists: ${destinationPath}`);
}
await fsP.mkdir(path.dirname(destinationPath), {
recursive: true,
mode: directoryMode,
});
try {
await fsP.rename(sourcePath, destinationPath);
} catch (error) {
if (error.code === 'EXDEV') {
await fsP.copyFile(sourcePath, destinationPath);
await fsP.unlink(sourcePath);
} else {
throw error;
}
}
};
const _moveFileSync = (sourcePath, destinationPath, {overwrite = true, cwd = process.cwd(), directoryMode, validateDirectory = false} = {}) => {
if (cwd) {
({sourcePath, destinationPath} = resolvePath(cwd, sourcePath, destinationPath));
}
if (validateDirectory) {
validateSameDirectory(sourcePath, destinationPath);
}
if (!overwrite && fs.existsSync(destinationPath)) {
throw new Error(`The destination file exists: ${destinationPath}`);
}
fs.mkdirSync(path.dirname(destinationPath), {
recursive: true,
mode: directoryMode,
});
try {
fs.renameSync(sourcePath, destinationPath);
} catch (error) {
if (error.code === 'EXDEV') {
fs.copyFileSync(sourcePath, destinationPath);
fs.unlinkSync(sourcePath);
} else {
throw error;
}
}
};
export async function moveFile(sourcePath, destinationPath, options) {
validatePathsExist(sourcePath, destinationPath);
return _moveFile(sourcePath, destinationPath, options);
}
export function moveFileSync(sourcePath, destinationPath, options) {
validatePathsExist(sourcePath, destinationPath);
return _moveFileSync(sourcePath, destinationPath, options);
}
export async function renameFile(source, destination, options = {}) {
validatePathsExist(source, destination, '');
return _moveFile(source, destination, {...options, validateDirectory: true});
}
export function renameFileSync(source, destination, options = {}) {
validatePathsExist(source, destination, '');
return _moveFileSync(source, destination, {...options, validateDirectory: true});
}