diff --git a/index.js b/index.js index e1956b8..af68afd 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,7 @@ 'use strict'; -const {promisify} = require('util'); +const { promisify } = require('util'); const path = require('path'); + const globby = require('globby'); const isGlob = require('is-glob'); const slash = require('slash'); @@ -13,109 +14,128 @@ const pMap = require('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 + 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.'); - } + 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 = Array.isArray(patterns) ? patterns : [patterns]; - patterns = patterns.map(pattern => { - if (process.platform === 'win32' && isGlob(pattern) === false) { - return slash(pattern); - } + patterns = patterns.map((pattern) => { + if (process.platform === 'win32' && isGlob(pattern) === false) { + return slash(pattern); + } - return pattern; - }); + return pattern; + }); - return patterns; + return patterns; } -module.exports = async (patterns, {force, dryRun, cwd = process.cwd(), ...options} = {}) => { - options = { - expandDirectories: false, - onlyFiles: false, - followSymbolicLinks: false, - cwd, - ...options - }; +/** + * @typedef {import('globby').GlobbyOptions} GlobbyOptions // Globby 13.1.1 version @ typedef {import('globby').Options} GlobbyOptions + * @typedef delOptionPropertys + * @property {boolean} [force] default false + * @property {boolean} [dryRun] default false + * @property {string} [cwd] default false + * @typedef {delOptionPropertys & GlobbyOptions} delOptions + * @typedef {string | readonly string[]} patterns + * @typedef {typeof delAsync & { sync: typeof delSync }} deprecatedCjsCompatModuleObject + */ +const normalizeDelOptions = (/** @type {delOptions} */ delOptions) => { + const { force = false, dryRun = false, cwd = process.cwd(), ...inputGlobbyOptions } = delOptions; + + /** @type {GlobbyOptions} */ + const globbyOptions = { + expandDirectories: false, + onlyFiles: false, + followSymbolicLinks: false, + cwd, + ...inputGlobbyOptions + }; + return { force, dryRun, cwd, globbyOptions }; +}; + +const delAsync = async (patterns, delOptions) => { + const { force, dryRun, cwd, globbyOptions: options } = normalizeDelOptions(delOptions); - patterns = normalizePatterns(patterns); + patterns = normalizePatterns(patterns); - const files = (await globby(patterns, options)) - .sort((a, b) => b.localeCompare(a)); + const files = (await globby(patterns, options)).sort((a, b) => b.localeCompare(a)); - const mapper = async file => { - file = path.resolve(cwd, file); + const mapper = async (file) => { + file = path.resolve(cwd, file); - if (!force) { - safeCheck(file, cwd); - } + if (!force) { + safeCheck(file, cwd); + } - if (!dryRun) { - await rimrafP(file, rimrafOptions); - } + if (!dryRun) { + await rimrafP(file, rimrafOptions); + } - return file; - }; + return file; + }; - const removedFiles = await pMap(files, mapper, options); + const removedFiles = await pMap(files, mapper, options); - removedFiles.sort((a, b) => a.localeCompare(b)); + removedFiles.sort((a, b) => a.localeCompare(b)); - return removedFiles; + return removedFiles; }; -module.exports.sync = (patterns, {force, dryRun, cwd = process.cwd(), ...options} = {}) => { - options = { - expandDirectories: false, - onlyFiles: false, - followSymbolicLinks: false, - cwd, - ...options - }; +const delSync = (patterns, delOptions) => { + const { force, dryRun, cwd, globbyOptions: options } = normalizeDelOptions(delOptions); - patterns = normalizePatterns(patterns); + patterns = normalizePatterns(patterns); - const files = globby.sync(patterns, options) - .sort((a, b) => b.localeCompare(a)); + const files = globby.sync(patterns, options).sort((a, b) => b.localeCompare(a)); - const removedFiles = files.map(file => { - file = path.resolve(cwd, file); + const removedFiles = files.map((file) => { + file = path.resolve(cwd, file); - if (!force) { - safeCheck(file, cwd); - } + if (!force) { + safeCheck(file, cwd); + } - if (!dryRun) { - rimraf.sync(file, rimrafOptions); - } + if (!dryRun) { + rimraf.sync(file, rimrafOptions); + } - return file; - }); + return file; + }); - removedFiles.sort((a, b) => a.localeCompare(b)); + removedFiles.sort((a, b) => a.localeCompare(b)); - return removedFiles; + return removedFiles; }; + +exports.delAsync = delAsync; +exports.delSync = delSync; + +module.exports = Object.assign(() => {}, delAsync, exports, { sync: delSync });