diff --git a/lib/bin.js b/lib/bin.js index 16433606..bfdf6ab6 100755 --- a/lib/bin.js +++ b/lib/bin.js @@ -37,7 +37,7 @@ const main = async (...args) => { if (process.env.__RIMRAF_TESTING_BIN_FAIL__ === '1') throw new Error('simulated rimraf failure') - const opts = {} + const opt = {} const paths = [] let dashdash = false let impl = rimraf @@ -54,22 +54,22 @@ const main = async (...args) => { console.log(help) return 0 } else if (arg === '--preserve-root') { - opts.preserveRoot = true + opt.preserveRoot = true continue } else if (arg === '--no-preserve-root') { - opts.preserveRoot = false + opt.preserveRoot = false continue } else if (/^--tmp=/.test(arg)) { const val = arg.substr('--tmp='.length) - opts.tmp = val + opt.tmp = val continue } else if (/^--max-retries=/.test(arg)) { const val = +arg.substr('--max-retries='.length) - opts.maxRetries = val + opt.maxRetries = val continue } else if (/^--retry-delay=/.test(arg)) { const val = +arg.substr('--retry-delay='.length) - opts.retryDelay = val + opt.retryDelay = val continue } else if (/^--impl=/.test(arg)) { const val = arg.substr('--impl='.length) @@ -96,7 +96,7 @@ const main = async (...args) => { paths.push(arg) } - if (opts.preserveRoot !== false) { + if (opt.preserveRoot !== false) { for (const path of paths.map(p => resolve(p))) { if (path === parse(path).root) { console.error(`rimraf: it is dangerous to operate recursively on '/'`) @@ -112,7 +112,7 @@ const main = async (...args) => { return 1 } - await impl(paths, opts) + await impl(paths, opt) return 0 } diff --git a/lib/index.js b/lib/index.js index 8087ecba..10cbfbeb 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,5 +1,5 @@ const pathArg = require('./path-arg.js') -const optsArg = require('./opts-arg.js') +const optArg = require('./opt-arg.js') const {rimrafNative, rimrafNativeSync} = require('./rimraf-native.js') const {rimrafManual, rimrafManualSync} = require('./rimraf-manual.js') @@ -7,29 +7,29 @@ const {rimrafWindows, rimrafWindowsSync} = require('./rimraf-windows.js') const {rimrafPosix, rimrafPosixSync} = require('./rimraf-posix.js') const {useNative, useNativeSync} = require('./use-native.js') -const wrap = fn => async (path, opts) => { - opts = optsArg(opts) +const wrap = fn => async (path, opt) => { + opt = optArg(opt) await (Array.isArray(path) - ? Promise.all(path.map(p => fn(pathArg(p, opts), opts))) - : fn(pathArg(path, opts), opts)) + ? Promise.all(path.map(p => fn(pathArg(p, opt), opt))) + : fn(pathArg(path, opt), opt)) } -const wrapSync = fn => (path, opts) => { - opts = optsArg(opts) +const wrapSync = fn => (path, opt) => { + opt = optArg(opt) return Array.isArray(path) - ? path.forEach(p => fn(pathArg(p, opts), opts)) - : fn(pathArg(path, opts), opts) + ? path.forEach(p => fn(pathArg(p, opt), opt)) + : fn(pathArg(path, opt), opt) } -const rimraf = wrap((path, opts) => - useNative(opts) - ? rimrafNative(path, opts) - : rimrafManual(path, opts)) +const rimraf = wrap((path, opt) => + useNative(opt) + ? rimrafNative(path, opt) + : rimrafManual(path, opt)) -const rimrafSync = wrapSync((path, opts) => - useNativeSync(opts) - ? rimrafNativeSync(path, opts) - : rimrafManualSync(path, opts)) +const rimrafSync = wrapSync((path, opt) => + useNativeSync(opt) + ? rimrafNativeSync(path, opt) + : rimrafManualSync(path, opt)) rimraf.rimraf = rimraf rimraf.sync = rimraf.rimrafSync = rimrafSync diff --git a/lib/opt-arg.js b/lib/opt-arg.js new file mode 100644 index 00000000..b049f8c5 --- /dev/null +++ b/lib/opt-arg.js @@ -0,0 +1,2 @@ +// TODO: validate options +module.exports = (opt = {}) => opt diff --git a/lib/opts-arg.js b/lib/opts-arg.js deleted file mode 100644 index cf6b4765..00000000 --- a/lib/opts-arg.js +++ /dev/null @@ -1,2 +0,0 @@ -// TODO: validate options -module.exports = (opts = {}) => opts diff --git a/lib/path-arg.js b/lib/path-arg.js index d9ef5bf5..b5f29255 100644 --- a/lib/path-arg.js +++ b/lib/path-arg.js @@ -1,7 +1,7 @@ const platform = require('./platform.js') const { resolve, parse } = require('path') const { inspect } = require('util') -const pathArg = (path, opts = {}) => { +const pathArg = (path, opt = {}) => { const type = typeof path if (type !== 'string') { const ctor = path && type === 'object' && path.constructor @@ -28,7 +28,7 @@ const pathArg = (path, opts = {}) => { path = resolve(path) const { root } = parse(path) - if (path === root && opts.preserveRoot !== false) { + if (path === root && opt.preserveRoot !== false) { const msg = 'refusing to remove root directory without preserveRoot:false' throw Object.assign(new Error(msg), { path, diff --git a/lib/rimraf-native.js b/lib/rimraf-native.js index 0dff528b..2be5cae2 100644 --- a/lib/rimraf-native.js +++ b/lib/rimraf-native.js @@ -5,14 +5,14 @@ const { }, } = require('./fs.js') -const rimrafNative = (path, opts) => rm(path, { - ...opts, +const rimrafNative = (path, opt) => rm(path, { + ...opt, force: true, recursive: true, }) -const rimrafNativeSync = (path, opts) => rmSync(path, { - ...opts, +const rimrafNativeSync = (path, opt) => rmSync(path, { + ...opt, force: true, recursive: true, }) diff --git a/lib/rimraf-posix.js b/lib/rimraf-posix.js index ba661533..70694d93 100644 --- a/lib/rimraf-posix.js +++ b/lib/rimraf-posix.js @@ -26,7 +26,7 @@ const { ignoreENOENTSync, } = require('./ignore-enoent.js') -const rimrafPosix = async (path, options) => { +const rimrafPosix = async (path, opt) => { const entries = await readdirOrError(path) if (!Array.isArray(entries)) { if (entries.code === 'ENOENT') @@ -36,7 +36,8 @@ const rimrafPosix = async (path, options) => { return ignoreENOENT(unlink(path)) } await Promise.all(entries.map(entry => - rimrafPosix(resolve(path, entry), options))) + rimrafPosix(resolve(path, entry), opt))) + return ignoreENOENT(rmdir(path)) } diff --git a/lib/rimraf-windows.js b/lib/rimraf-windows.js index 8d6b4482..3858c436 100644 --- a/lib/rimraf-windows.js +++ b/lib/rimraf-windows.js @@ -71,11 +71,11 @@ const unlinkFixEPERMSync = path => { } } -const rimrafWindows = async (path, opts) => { - if (!opts.tmp) - return rimrafWindows(path, { ...opts, tmp: await defaultTmp(path) }) +const rimrafWindows = async (path, opt) => { + if (!opt.tmp) + return rimrafWindows(path, { ...opt, tmp: await defaultTmp(path) }) - if (path === opts.tmp) + if (path === opt.tmp) throw new Error('cannot delete temp directory used for deletion') const entries = await readdirOrError(path) @@ -86,13 +86,13 @@ const rimrafWindows = async (path, opts) => { if (entries.code !== 'ENOTDIR') throw entries - return await ignoreENOENT(tmpUnlink(path, opts.tmp, unlinkFixEPERM)) + return await ignoreENOENT(tmpUnlink(path, opt.tmp, unlinkFixEPERM)) } await Promise.all(entries.map(entry => - rimrafWindows(resolve(path, entry), opts))) + rimrafWindows(resolve(path, entry), opt))) - return await ignoreENOENT(tmpUnlink(path, opts.tmp, rmdir)) + return await ignoreENOENT(tmpUnlink(path, opt.tmp, rmdir)) } const tmpUnlink = async (path, tmp, rm) => { @@ -101,11 +101,11 @@ const tmpUnlink = async (path, tmp, rm) => { return await rm(tmpFile) } -const rimrafWindowsSync = (path, opts) => { - if (!opts.tmp) - return rimrafWindowsSync(path, { ...opts, tmp: defaultTmpSync(path) }) +const rimrafWindowsSync = (path, opt) => { + if (!opt.tmp) + return rimrafWindowsSync(path, { ...opt, tmp: defaultTmpSync(path) }) - if (path === opts.tmp) + if (path === opt.tmp) throw new Error('cannot delete temp directory used for deletion') const entries = readdirOrErrorSync(path) @@ -117,13 +117,13 @@ const rimrafWindowsSync = (path, opts) => { throw entries return ignoreENOENTSync(() => - tmpUnlinkSync(path, opts.tmp, unlinkFixEPERMSync)) + tmpUnlinkSync(path, opt.tmp, unlinkFixEPERMSync)) } for (const entry of entries) - rimrafWindowsSync(resolve(path, entry), opts) + rimrafWindowsSync(resolve(path, entry), opt) - return ignoreENOENTSync(() => tmpUnlinkSync(path, opts.tmp, rmdirSync)) + return ignoreENOENTSync(() => tmpUnlinkSync(path, opt.tmp, rmdirSync)) } const tmpUnlinkSync = (path, tmp, rmSync) => { diff --git a/lib/use-native.js b/lib/use-native.js index 297c8279..678cf401 100644 --- a/lib/use-native.js +++ b/lib/use-native.js @@ -2,7 +2,7 @@ const version = process.env.__TESTING_RIMRAF_NODE_VERSION__ || process.version const versArr = version.replace(/^v/, '').split('.') const hasNative = +versArr[0] > 14 || +versArr[0] === 14 && +versArr[1] >= 14 -// TODO: check opts.rm === fs.rm, and use manual if they've overridden it +// TODO: check opt.rm === fs.rm, and use manual if they've overridden it const useNative = !hasNative ? () => false : () => true const useNativeSync = !hasNative ? () => false : () => true diff --git a/tap-snapshots/test/index.js.test.cjs b/tap-snapshots/test/index.js.test.cjs index 8695ca27..fa006ef9 100644 --- a/tap-snapshots/test/index.js.test.cjs +++ b/tap-snapshots/test/index.js.test.cjs @@ -8,7 +8,7 @@ exports[`test/index.js TAP mocky unit tests to select the correct function main function, useNative=false > must match snapshot 1`] = ` Array [ Array [ - "optsArg", + "optArg", Object { "a": 1, }, @@ -31,7 +31,7 @@ Array [ }, ], Array [ - "optsArg", + "optArg", Object { "a": 2, }, @@ -59,7 +59,7 @@ Array [ exports[`test/index.js TAP mocky unit tests to select the correct function main function, useNative=true > must match snapshot 1`] = ` Array [ Array [ - "optsArg", + "optArg", Object { "a": 1, }, @@ -82,7 +82,7 @@ Array [ }, ], Array [ - "optsArg", + "optArg", Object { "a": 2, }, @@ -110,7 +110,7 @@ Array [ exports[`test/index.js TAP mocky unit tests to select the correct function manual > must match snapshot 1`] = ` Array [ Array [ - "optsArg", + "optArg", Object { "a": 3, }, @@ -127,7 +127,7 @@ Array [ }, ], Array [ - "optsArg", + "optArg", Object { "a": 4, }, @@ -149,7 +149,7 @@ Array [ exports[`test/index.js TAP mocky unit tests to select the correct function native > must match snapshot 1`] = ` Array [ Array [ - "optsArg", + "optArg", Object { "a": 5, }, @@ -166,7 +166,7 @@ Array [ }, ], Array [ - "optsArg", + "optArg", Object { "a": 6, }, @@ -188,7 +188,7 @@ Array [ exports[`test/index.js TAP mocky unit tests to select the correct function posix > must match snapshot 1`] = ` Array [ Array [ - "optsArg", + "optArg", Object { "a": 7, }, @@ -205,7 +205,7 @@ Array [ }, ], Array [ - "optsArg", + "optArg", Object { "a": 8, }, @@ -227,7 +227,7 @@ Array [ exports[`test/index.js TAP mocky unit tests to select the correct function windows > must match snapshot 1`] = ` Array [ Array [ - "optsArg", + "optArg", Object { "a": 9, }, @@ -244,7 +244,7 @@ Array [ }, ], Array [ - "optsArg", + "optArg", Object { "a": 10, }, diff --git a/test/index.js b/test/index.js index 7a12db7c..8ba3dfa9 100644 --- a/test/index.js +++ b/test/index.js @@ -9,12 +9,12 @@ t.test('mocky unit tests to select the correct function', t => { let USE_NATIVE = true const mocks = { '../lib/use-native.js': { - useNative: opts => { - CALLS.push(['useNative', opts]) + useNative: opt => { + CALLS.push(['useNative', opt]) return USE_NATIVE }, - useNativeSync: opts => { - CALLS.push(['useNativeSync', opts]) + useNativeSync: opt => { + CALLS.push(['useNativeSync', opt]) return USE_NATIVE }, }, @@ -22,32 +22,32 @@ t.test('mocky unit tests to select the correct function', t => { CALLS.push(['pathArg', path]) return path }, - '../lib/opts-arg.js': opts => { - CALLS.push(['optsArg', opts]) - return opts + '../lib/opt-arg.js': opt => { + CALLS.push(['optArg', opt]) + return opt }, '../lib/rimraf-posix.js': { - rimrafPosix: async (path, opts) => { - CALLS.push(['rimrafPosix', path, opts]) + rimrafPosix: async (path, opt) => { + CALLS.push(['rimrafPosix', path, opt]) }, - rimrafPosixSync: async (path, opts) => { - CALLS.push(['rimrafPosixSync', path, opts]) + rimrafPosixSync: async (path, opt) => { + CALLS.push(['rimrafPosixSync', path, opt]) }, }, '../lib/rimraf-windows.js': { - rimrafWindows: async (path, opts) => { - CALLS.push(['rimrafWindows', path, opts]) + rimrafWindows: async (path, opt) => { + CALLS.push(['rimrafWindows', path, opt]) }, - rimrafWindowsSync: async (path, opts) => { - CALLS.push(['rimrafWindowsSync', path, opts]) + rimrafWindowsSync: async (path, opt) => { + CALLS.push(['rimrafWindowsSync', path, opt]) }, }, '../lib/rimraf-native.js': { - rimrafNative: async (path, opts) => { - CALLS.push(['rimrafNative', path, opts]) + rimrafNative: async (path, opt) => { + CALLS.push(['rimrafNative', path, opt]) }, - rimrafNativeSync: async (path, opts) => { - CALLS.push(['rimrafNativeSync', path, opts]) + rimrafNativeSync: async (path, opt) => { + CALLS.push(['rimrafNativeSync', path, opt]) }, }, } @@ -150,8 +150,8 @@ t.test('accept array of paths as first arg', async t => { useNativeSync: () => true, }, '../lib/rimraf-native.js': { - rimrafNative: async (path, opts) => ASYNC_CALLS.push([path, opts]), - rimrafNativeSync: (path, opts) => SYNC_CALLS.push([path, opts]), + rimrafNative: async (path, opt) => ASYNC_CALLS.push([path, opt]), + rimrafNativeSync: (path, opt) => SYNC_CALLS.push([path, opt]), }, }) t.equal(await rimraf(['a', 'b', 'c']), undefined) diff --git a/test/opt-arg.js b/test/opt-arg.js new file mode 100644 index 00000000..4d6c34f1 --- /dev/null +++ b/test/opt-arg.js @@ -0,0 +1,5 @@ +const t = require('tap') +const optArg = require('../lib/opt-arg.js') +const opt = { a: 1 } +t.equal(optArg(opt), opt, 'returns object if provided') +t.same(optArg(), {}, 'returns new object otherwise') diff --git a/test/opts-arg.js b/test/opts-arg.js deleted file mode 100644 index 1e3c97c9..00000000 --- a/test/opts-arg.js +++ /dev/null @@ -1,5 +0,0 @@ -const t = require('tap') -const optsArg = require('../lib/opts-arg.js') -const opts = { a: 1 } -t.equal(optsArg(opts), opts, 'returns object if provided') -t.same(optsArg(), {}, 'returns new object otherwise')