Skip to content

Commit

Permalink
consolidate all the spellings of 'opt' into one
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Jan 10, 2023
1 parent d4eec2e commit 2442655
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 88 deletions.
16 changes: 8 additions & 8 deletions lib/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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 '/'`)
Expand All @@ -112,7 +112,7 @@ const main = async (...args) => {
return 1
}

await impl(paths, opts)
await impl(paths, opt)
return 0
}

Expand Down
34 changes: 17 additions & 17 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
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')
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
Expand Down
2 changes: 2 additions & 0 deletions lib/opt-arg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// TODO: validate options
module.exports = (opt = {}) => opt
2 changes: 0 additions & 2 deletions lib/opts-arg.js

This file was deleted.

4 changes: 2 additions & 2 deletions lib/path-arg.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions lib/rimraf-native.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
Expand Down
5 changes: 3 additions & 2 deletions lib/rimraf-posix.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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))
}

Expand Down
28 changes: 14 additions & 14 deletions lib/rimraf-windows.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) => {
Expand All @@ -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)
Expand All @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion lib/use-native.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
24 changes: 12 additions & 12 deletions tap-snapshots/test/index.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand All @@ -31,7 +31,7 @@ Array [
},
],
Array [
"optsArg",
"optArg",
Object {
"a": 2,
},
Expand Down Expand Up @@ -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,
},
Expand All @@ -82,7 +82,7 @@ Array [
},
],
Array [
"optsArg",
"optArg",
Object {
"a": 2,
},
Expand Down Expand Up @@ -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,
},
Expand All @@ -127,7 +127,7 @@ Array [
},
],
Array [
"optsArg",
"optArg",
Object {
"a": 4,
},
Expand All @@ -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,
},
Expand All @@ -166,7 +166,7 @@ Array [
},
],
Array [
"optsArg",
"optArg",
Object {
"a": 6,
},
Expand All @@ -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,
},
Expand All @@ -205,7 +205,7 @@ Array [
},
],
Array [
"optsArg",
"optArg",
Object {
"a": 8,
},
Expand All @@ -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,
},
Expand All @@ -244,7 +244,7 @@ Array [
},
],
Array [
"optsArg",
"optArg",
Object {
"a": 10,
},
Expand Down
Loading

0 comments on commit 2442655

Please sign in to comment.