forked from sindresorhus/del
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
140 lines (109 loc) · 2.99 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
import {promisify} from 'node:util';
import path from 'node:path';
import process from 'node:process';
import {globby, globbySync} from 'globby';
import isGlob from 'is-glob';
import slash from 'slash';
import gracefulFs from 'graceful-fs';
import isPathCwd from 'is-path-cwd';
import isPathInside from 'is-path-inside';
import rimraf from 'rimraf';
import pMap from 'p-map';
const rimrafP = promisify(rimraf);
const rimrafOptions = {
glob: false,
unlink: gracefulFs.unlink,
unlinkSync: gracefulFs.unlinkSync,
chmod: gracefulFs.chmod,
chmodSync: gracefulFs.chmodSync,
stat: gracefulFs.stat,
statSync: gracefulFs.statSync,
lstat: gracefulFs.lstat,
lstatSync: gracefulFs.lstatSync,
rmdir: gracefulFs.rmdir,
rmdirSync: gracefulFs.rmdirSync,
readdir: gracefulFs.readdir,
readdirSync: gracefulFs.readdirSync,
};
function safeCheck(file, cwd) {
if (isPathCwd(file)) {
throw new Error('Cannot delete the current working directory. Can be overridden with the `force` option.');
}
if (!isPathInside(file, cwd)) {
throw new Error('Cannot delete files/directories outside the current working directory. Can be overridden with the `force` option.');
}
}
function normalizePatterns(patterns) {
patterns = Array.isArray(patterns) ? patterns : [patterns];
patterns = patterns.map(pattern => {
if (process.platform === 'win32' && isGlob(pattern) === false) {
return slash(pattern);
}
return pattern;
});
return patterns;
}
export async function deleteAsync(patterns, {force, dryRun, cwd = process.cwd(), onProgress = () => {}, ...options} = {}) {
options = {
expandDirectories: false,
onlyFiles: false,
followSymbolicLinks: false,
cwd,
...options,
};
patterns = normalizePatterns(patterns);
const paths = await globby(patterns, options);
const files = paths.sort((a, b) => b.localeCompare(a));
if (files.length === 0) {
onProgress({
totalCount: 0,
deletedCount: 0,
percent: 1,
});
}
let deletedCount = 0;
const mapper = async file => {
file = path.resolve(cwd, file);
if (!force) {
safeCheck(file, cwd);
}
if (!dryRun) {
await rimrafP(file, rimrafOptions);
}
deletedCount += 1;
onProgress({
totalCount: files.length,
deletedCount,
percent: deletedCount / files.length,
path: file,
});
return file;
};
const removedFiles = await pMap(files, mapper, options);
removedFiles.sort((a, b) => a.localeCompare(b));
return removedFiles;
}
export function deleteSync(patterns, {force, dryRun, cwd = process.cwd(), ...options} = {}) {
options = {
expandDirectories: false,
onlyFiles: false,
followSymbolicLinks: false,
cwd,
...options,
};
patterns = normalizePatterns(patterns);
const files = globbySync(patterns, options)
.sort((a, b) => b.localeCompare(a));
const removedFiles = files.map(file => {
file = path.resolve(cwd, file);
if (!force) {
safeCheck(file, cwd);
}
if (!dryRun) {
rimraf.sync(file, rimrafOptions);
}
return file;
});
removedFiles.sort((a, b) => a.localeCompare(b));
return removedFiles;
}