2022-01-21 08:28:41 +00:00
|
|
|
'use strict';
|
2023-08-17 09:47:40 +00:00
|
|
|
const {promisify} = require('util');
|
2022-01-21 08:28:41 +00:00
|
|
|
const path = require('path');
|
|
|
|
const globby = require('globby');
|
2023-08-17 09:47:40 +00:00
|
|
|
const isGlob = require('is-glob');
|
|
|
|
const slash = require('slash');
|
|
|
|
const gracefulFs = require('graceful-fs');
|
2022-01-21 08:28:41 +00:00
|
|
|
const isPathCwd = require('is-path-cwd');
|
2023-08-17 09:47:40 +00:00
|
|
|
const isPathInside = require('is-path-inside');
|
2022-01-21 08:28:41 +00:00
|
|
|
const rimraf = require('rimraf');
|
|
|
|
const pMap = require('p-map');
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
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
|
|
|
|
};
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
function safeCheck(file, cwd) {
|
2022-01-21 08:28:41 +00:00
|
|
|
if (isPathCwd(file)) {
|
2023-08-17 09:47:40 +00:00
|
|
|
throw new Error('Cannot delete the current working directory. Can be overridden with the `force` option.');
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
if (!isPathInside(file, cwd)) {
|
|
|
|
throw new Error('Cannot delete files/directories outside the current working directory. Can be overridden with the `force` option.');
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = async (patterns, {force, dryRun, cwd = process.cwd(), onProgress = () => {}, ...options} = {}) => {
|
|
|
|
options = {
|
|
|
|
expandDirectories: false,
|
|
|
|
onlyFiles: false,
|
|
|
|
followSymbolicLinks: false,
|
|
|
|
cwd,
|
|
|
|
...options
|
|
|
|
};
|
|
|
|
|
|
|
|
patterns = normalizePatterns(patterns);
|
|
|
|
|
|
|
|
const files = (await globby(patterns, options))
|
|
|
|
.sort((a, b) => b.localeCompare(a));
|
|
|
|
|
|
|
|
if (files.length === 0) {
|
|
|
|
onProgress({
|
|
|
|
totalCount: 0,
|
|
|
|
deletedCount: 0,
|
|
|
|
percent: 1
|
|
|
|
});
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
let deletedCount = 0;
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
const mapper = async file => {
|
|
|
|
file = path.resolve(cwd, file);
|
2022-01-21 08:28:41 +00:00
|
|
|
|
|
|
|
if (!force) {
|
2023-08-17 09:47:40 +00:00
|
|
|
safeCheck(file, cwd);
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
if (!dryRun) {
|
|
|
|
await rimrafP(file, rimrafOptions);
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
deletedCount += 1;
|
|
|
|
|
|
|
|
onProgress({
|
|
|
|
totalCount: files.length,
|
|
|
|
deletedCount,
|
|
|
|
percent: deletedCount / files.length
|
|
|
|
});
|
|
|
|
|
|
|
|
return file;
|
2022-01-21 08:28:41 +00:00
|
|
|
};
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
const removedFiles = await pMap(files, mapper, options);
|
|
|
|
|
|
|
|
removedFiles.sort((a, b) => a.localeCompare(b));
|
|
|
|
|
|
|
|
return removedFiles;
|
2022-01-21 08:28:41 +00:00
|
|
|
};
|
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
module.exports.sync = (patterns, {force, dryRun, cwd = process.cwd(), ...options} = {}) => {
|
|
|
|
options = {
|
|
|
|
expandDirectories: false,
|
|
|
|
onlyFiles: false,
|
|
|
|
followSymbolicLinks: false,
|
|
|
|
cwd,
|
|
|
|
...options
|
|
|
|
};
|
|
|
|
|
|
|
|
patterns = normalizePatterns(patterns);
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
const files = globby.sync(patterns, options)
|
|
|
|
.sort((a, b) => b.localeCompare(a));
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2023-08-17 09:47:40 +00:00
|
|
|
const removedFiles = files.map(file => {
|
|
|
|
file = path.resolve(cwd, file);
|
2022-01-21 08:28:41 +00:00
|
|
|
|
|
|
|
if (!force) {
|
2023-08-17 09:47:40 +00:00
|
|
|
safeCheck(file, cwd);
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!dryRun) {
|
2023-08-17 09:47:40 +00:00
|
|
|
rimraf.sync(file, rimrafOptions);
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return file;
|
|
|
|
});
|
2023-08-17 09:47:40 +00:00
|
|
|
|
|
|
|
removedFiles.sort((a, b) => a.localeCompare(b));
|
|
|
|
|
|
|
|
return removedFiles;
|
2022-01-21 08:28:41 +00:00
|
|
|
};
|