Skip to content

Commit

Permalink
accept strings, arrays of strings, and no other types
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Jan 10, 2023
1 parent ad4f2db commit 0c82d74
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 72 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -6,6 +6,7 @@ Install with `npm install rimraf`, or just drop rimraf.js somewhere.

* The function returns a `Promise` instead of taking a callback.
* Built-in glob support removed.
* Functions take arrays of paths, as well as a single path.
* Native implementation used by default when available.
* New implementation on Windows, making the exponential backoff for
`EBUSY` and `ENOTEMPTY` errors no longer necessary.
Expand Down
50 changes: 26 additions & 24 deletions lib/index.js
Expand Up @@ -7,51 +7,53 @@ const {rimrafWindows, rimrafWindowsSync} = require('./rimraf-windows.js')
const {rimrafPosix, rimrafPosixSync} = require('./rimraf-posix.js')
const {useNative, useNativeSync} = require('./use-native.js')

const rimraf = (path, opts) => {
path = pathArg(path, opts)
const wrap = fn => async (path, opts) => {
opts = optsArg(opts)
return useNative(opts) ? rimrafNative(path, opts)
: rimrafManual(path, opts)
await (Array.isArray(path)
? Promise.all(path.map(p => fn(pathArg(p, opts), opts)))
: fn(pathArg(path, opts), opts))
}

const rimrafSync = async (path, opts) => {
path = pathArg(path, opts)
const wrapSync = fn => (path, opts) => {
opts = optsArg(opts)
return useNativeSync(opts) ? rimrafNativeSync(path, opts)
: rimrafManualSync(path, opts)
return Array.isArray(path)
? path.forEach(p => fn(pathArg(p, opts), opts))
: fn(pathArg(path, opts), opts)
}

const rimraf = wrap((path, opts) =>
useNative(opts)
? rimrafNative(path, opts)
: rimrafManual(path, opts))

const rimrafSync = wrapSync((path, opts) =>
useNativeSync(opts)
? rimrafNativeSync(path, opts)
: rimrafManualSync(path, opts))

rimraf.rimraf = rimraf
rimraf.sync = rimraf.rimrafSync = rimrafSync

const native = async (path, opts) =>
rimrafNative(pathArg(path, opts), optsArg(opts))
const nativeSync = (path, opts) =>
rimrafNativeSync(pathArg(path, opts), optsArg(opts))
const native = wrap(rimrafNative)
const nativeSync = wrapSync(rimrafNativeSync)
native.sync = nativeSync
rimraf.native = native
rimraf.nativeSync = nativeSync

const manual = async (path, opts) =>
rimrafManual(pathArg(path, opts), optsArg(opts))
const manualSync = (path, opts) =>
rimrafManualSync(pathArg(path, opts), optsArg(opts))
const manual = wrap(rimrafManual)
const manualSync = wrapSync(rimrafManualSync)
manual.sync = manualSync
rimraf.manual = manual
rimraf.manualSync = manualSync

const windows = async (path, opts) =>
rimrafWindows(pathArg(path, opts), optsArg(opts))
const windowsSync = (path, opts) =>
rimrafWindowsSync(pathArg(path, opts), optsArg(opts))
const windows = wrap(rimrafWindows)
const windowsSync = wrapSync(rimrafWindowsSync)
windows.sync = windowsSync
rimraf.windows = windows
rimraf.windowsSync = windowsSync

const posix = async (path, opts) =>
rimrafPosix(pathArg(path, opts), optsArg(opts))
const posixSync = (path, opts) =>
rimrafPosixSync(pathArg(path, opts), optsArg(opts))
const posix = wrap(rimrafPosix)
const posixSync = wrapSync(rimrafPosixSync)
posix.sync = posixSync
rimraf.posix = posix
rimraf.posixSync = posixSync
Expand Down
15 changes: 15 additions & 0 deletions lib/path-arg.js
@@ -1,6 +1,21 @@
const platform = require('./platform.js')
const { resolve, parse } = require('path')
const { inspect } = require('util')
const pathArg = (path, opts = {}) => {
const type = typeof path
if (type !== 'string') {
const ctor = path && type === 'object' && path.constructor
const received = ctor && ctor.name ? `an instance of ${ctor.name}`
: type === 'object' ? inspect(path)
: `type ${type} ${path}`
const msg = 'The "path" argument must be of type string. ' +
`Received ${received}`
throw Object.assign(new TypeError(msg), {
path,
code: 'ERR_INVALID_ARG_TYPE',
})
}

if (/\0/.test(path)) {
// simulate same failure that node raises
const msg = 'path must be a string without null bytes'
Expand Down
96 changes: 48 additions & 48 deletions tap-snapshots/test/index.js.test.cjs
Expand Up @@ -7,16 +7,16 @@
'use strict'
exports[`test/index.js TAP mocky unit tests to select the correct function main function, useNative=false > must match snapshot 1`] = `
Array [
Array [
"pathArg",
"path",
],
Array [
"optsArg",
Object {
"a": 1,
},
],
Array [
"pathArg",
"path",
],
Array [
"useNative",
Object {
Expand All @@ -30,16 +30,16 @@ Array [
"a": 1,
},
],
Array [
"pathArg",
"path",
],
Array [
"optsArg",
Object {
"a": 2,
},
],
Array [
"pathArg",
"path",
],
Array [
"useNativeSync",
Object {
Expand All @@ -58,16 +58,16 @@ Array [

exports[`test/index.js TAP mocky unit tests to select the correct function main function, useNative=true > must match snapshot 1`] = `
Array [
Array [
"pathArg",
"path",
],
Array [
"optsArg",
Object {
"a": 1,
},
],
Array [
"pathArg",
"path",
],
Array [
"useNative",
Object {
Expand All @@ -81,16 +81,16 @@ Array [
"a": 1,
},
],
Array [
"pathArg",
"path",
],
Array [
"optsArg",
Object {
"a": 2,
},
],
Array [
"pathArg",
"path",
],
Array [
"useNativeSync",
Object {
Expand All @@ -109,33 +109,33 @@ Array [

exports[`test/index.js TAP mocky unit tests to select the correct function manual > must match snapshot 1`] = `
Array [
Array [
"pathArg",
"path",
],
Array [
"optsArg",
Object {
"a": 3,
},
],
Array [
"pathArg",
"path",
],
Array [
"rimrafPosix",
"path",
Object {
"a": 3,
},
],
Array [
"pathArg",
"path",
],
Array [
"optsArg",
Object {
"a": 4,
},
],
Array [
"pathArg",
"path",
],
Array [
"rimrafPosixSync",
"path",
Expand All @@ -148,33 +148,33 @@ Array [

exports[`test/index.js TAP mocky unit tests to select the correct function native > must match snapshot 1`] = `
Array [
Array [
"pathArg",
"path",
],
Array [
"optsArg",
Object {
"a": 5,
},
],
Array [
"pathArg",
"path",
],
Array [
"rimrafNative",
"path",
Object {
"a": 5,
},
],
Array [
"pathArg",
"path",
],
Array [
"optsArg",
Object {
"a": 6,
},
],
Array [
"pathArg",
"path",
],
Array [
"rimrafNativeSync",
"path",
Expand All @@ -187,33 +187,33 @@ Array [

exports[`test/index.js TAP mocky unit tests to select the correct function posix > must match snapshot 1`] = `
Array [
Array [
"pathArg",
"path",
],
Array [
"optsArg",
Object {
"a": 7,
},
],
Array [
"pathArg",
"path",
],
Array [
"rimrafPosix",
"path",
Object {
"a": 7,
},
],
Array [
"pathArg",
"path",
],
Array [
"optsArg",
Object {
"a": 8,
},
],
Array [
"pathArg",
"path",
],
Array [
"rimrafPosixSync",
"path",
Expand All @@ -226,33 +226,33 @@ Array [

exports[`test/index.js TAP mocky unit tests to select the correct function windows > must match snapshot 1`] = `
Array [
Array [
"pathArg",
"path",
],
Array [
"optsArg",
Object {
"a": 9,
},
],
Array [
"pathArg",
"path",
],
Array [
"rimrafWindows",
"path",
Object {
"a": 9,
},
],
Array [
"pathArg",
"path",
],
Array [
"optsArg",
Object {
"a": 10,
},
],
Array [
"pathArg",
"path",
],
Array [
"rimrafWindowsSync",
"path",
Expand Down

0 comments on commit 0c82d74

Please sign in to comment.