/
index.js
74 lines (58 loc) · 1.58 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
'use strict';
const {promisify} = require('util');
const path = require('path');
const globby = require('globby');
const isPathCwd = require('is-path-cwd');
const isPathInCwd = require('is-path-in-cwd');
const rimraf = require('rimraf');
const pMap = require('p-map');
const rimrafP = promisify(rimraf);
function safeCheck(file) {
if (isPathCwd(file)) {
throw new Error('Cannot delete the current working directory. Can be overridden with the `force` option.');
}
if (!isPathInCwd(file)) {
throw new Error('Cannot delete files/directories outside the current working directory. Can be overridden with the `force` option.');
}
}
module.exports = async (patterns, {force, dryRun, ...options} = {}) => {
options = {
expandDirectories: false,
onlyFiles: false,
followSymbolicLinks: false,
...options
};
const files = (await globby(patterns, options))
.sort((a, b) => b.localeCompare(a));
const mapper = async file => {
if (!force) {
safeCheck(file);
}
file = path.resolve(options.cwd || '', file);
if (!dryRun) {
await rimrafP(file, {glob: false});
}
return file;
};
return pMap(files, mapper, options);
};
module.exports.sync = (patterns, {force, dryRun, ...options} = {}) => {
options = {
expandDirectories: false,
onlyFiles: false,
followSymbolicLinks: false,
...options
};
const files = globby.sync(patterns, options)
.sort((a, b) => b.localeCompare(a));
return files.map(file => {
if (!force) {
safeCheck(file);
}
file = path.resolve(options.cwd || '', file);
if (!dryRun) {
rimraf.sync(file, {glob: false});
}
return file;
});
};