/
index.js
61 lines (52 loc) · 1.29 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
'use strict';
const fs = require('fs');
const path = require('path');
const globby = require('globby');
const pTry = require('p-try');
const isPathInside = require('is-path-inside');
const macos = require('./lib/macos');
const linux = require('./lib/linux');
const windows = require('./lib/windows');
const trash = (paths, options) => pTry(() => {
paths = (typeof paths === 'string' ? [paths] : paths).map(String);
options = {
glob: true,
...options
};
// TOOD: Upgrading to latest `globby` version is blocked by https://github.com/mrmlnc/fast-glob/issues/110
if (options.glob) {
paths = globby.sync(paths, {
expandDirectories: false,
nodir: false,
nonull: true
});
}
paths = paths.map(filePath => path.resolve(filePath));
paths = paths.filter(filePath => {
if (paths.some(otherPath => isPathInside(filePath, otherPath))) {
return false;
}
try {
return fs.lstatSync(filePath);
} catch (error) {
if (error.code === 'ENOENT') {
return false;
}
throw error;
}
});
if (paths.length === 0) {
return;
}
switch (process.platform) {
case 'darwin':
return macos(paths);
case 'win32':
return windows(paths);
default:
return linux(paths);
}
});
module.exports = trash;
// TODO: Remove this for the next major release
module.exports.default = trash;