Skip to content

Commit

Permalink
prettier formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Jan 10, 2023
1 parent 40f64ec commit e7501cd
Show file tree
Hide file tree
Showing 31 changed files with 1,422 additions and 2,990 deletions.
9 changes: 9 additions & 0 deletions .prettierignore
@@ -0,0 +1,9 @@
/node_modules
/example
/.github
/dist
.env
/tap-snapshots
/.nyc_output
/coverage
/benchmark
50 changes: 25 additions & 25 deletions README.md
@@ -1,51 +1,51 @@
The [UNIX command](http://en.wikipedia.org/wiki/Rm_(Unix)) `rm -rf` for node.
The [UNIX command](<http://en.wikipedia.org/wiki/Rm_(Unix)>) `rm -rf` for node.

Install with `npm install rimraf`, or just drop rimraf.js somewhere.

## Major Changes from v3 to v4

* 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, falling back to "move then
- 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, falling back to "move then
remove" strategy when exponential backoff for `EBUSY` fails to
resolve the situation.
* Simplified implementation on Posix, since the Windows affordances are not
- Simplified implementation on Posix, since the Windows affordances are not
necessary there.

## API

### `rimraf(f, [opts]) -> Promise`

This first parameter is a path. The second argument is an options object.
This first parameter is a path. The second argument is an options object.

Options:

* `preserveRoot`: If set to boolean `false`, then allow the
recursive removal of the root directory. Otherwise, this is
- `preserveRoot`: If set to boolean `false`, then allow the
recursive removal of the root directory. Otherwise, this is
not allowed.
* `tmp`: Windows only. Temp folder to use to place files and
folders for the "move then remove" fallback. Must be on the
same physical device as the path being deleted. Defaults to
- `tmp`: Windows only. Temp folder to use to place files and
folders for the "move then remove" fallback. Must be on the
same physical device as the path being deleted. Defaults to
`os.tmpdir()` when that is on the same drive letter as the path
being deleted, or `${drive}:\temp` if present, or `${drive}:\`
if not.
* `retries`: Windows only. Maximum number of synchronous retry
- `retries`: Windows only. Maximum number of synchronous retry
attempts in case of `EBUSY`, `EMFILE`, and `ENFILE` errors.
Default `10`
* `backoff`: Windows only. Rate of exponential backoff for async
- `backoff`: Windows only. Rate of exponential backoff for async
removal in case of `EBUSY`, `EMFILE`, and `ENFILE` errors.
Should be a number greater than 1. Default `1.2`
* `maxBackoff`: Windows only. Maximum backoff time in ms to
Should be a number greater than 1. Default `1.2`
- `maxBackoff`: Windows only. Maximum backoff time in ms to
attempt asynchronous retries in case of `EBUSY`, `EMFILE`, and
`ENFILE` errors. Default `100`
`ENFILE` errors. Default `100`

Any other options are provided to the native Node.js `fs.rm` implementation
when that is used.

This will attempt to choose the best implementation, based on Node.js
version and `process.platform`. To force a specific implementation, use
version and `process.platform`. To force a specific implementation, use
one of the other functions provided.

### `rimraf.sync(f, [opts])` `rimraf.rimrafSync(f, [opts])`
Expand All @@ -58,7 +58,7 @@ deletion is extremely parallelizable.

### `rimraf.native(f, [opts])`

Uses the built-in `fs.rm` implementation that Node.js provides. This is
Uses the built-in `fs.rm` implementation that Node.js provides. This is
used by default on Node.js versions greater than or equal to `14.14.0`.

### `rimraf.nativeSync(f, [opts])` `rimraf.native.sync(f, [opts])`
Expand All @@ -76,13 +76,13 @@ Synchronous form of `rimraf.manual()`
### `rimraf.windows(f, [opts])`

JavaScript implementation of file removal appropriate for Windows
platforms. Works around `unlink` and `rmdir` not being atomic
platforms. Works around `unlink` and `rmdir` not being atomic
operations, and `EPERM` when deleting files with certain
permission modes.

First deletes all non-directory files within the tree, and then
removes all directories, which should ideally be empty by that
time. When an `ENOTEMPTY` is raised in the second pass, falls
time. When an `ENOTEMPTY` is raised in the second pass, falls
back to the `rimraf.moveRemove` strategy as needed.

### `rimraf.windows.sync(path, [opts])` `rimraf.windowsSync(path, [opts])`
Expand All @@ -96,17 +96,17 @@ with a temporary filename prior to attempting to remove them.

Note that, in cases where the operation fails, this _may_ leave
files lying around in the parent directory with names like
`.file-basename.txt.0.123412341`. Until the Windows kernel
`.file-basename.txt.0.123412341`. Until the Windows kernel
provides a way to perform atomic `unlink` and `rmdir` operations,
this is unfortunately unavoidable.

To move files to a different temporary directory other than the
parent, provide `opts.tmp`. Note that this _must_ be on the same
parent, provide `opts.tmp`. Note that this _must_ be on the same
physical device as the folder being deleted, or else the
operation will fail.

This is the slowest strategy, but most reliable on Windows
platforms. Used as a last-ditch fallback by `rimraf.windows()`.
platforms. Used as a last-ditch fallback by `rimraf.windows()`.

### `rimraf.moveRemove.sync(path, [opts])` `rimraf.moveRemoveSync(path, [opts])`

Expand Down
11 changes: 7 additions & 4 deletions lib/bin.js
Expand Up @@ -121,8 +121,11 @@ const main = async (...args) => {
module.exports = Object.assign(main, { help })
if (module === require.main) {
const args = process.argv.slice(2)
main(...args).then(code => process.exit(code), er => {
console.error(er)
process.exit(1)
})
main(...args).then(
code => process.exit(code),
er => {
console.error(er)
process.exit(1)
}
)
}
28 changes: 19 additions & 9 deletions lib/default-tmp.js
Expand Up @@ -9,7 +9,10 @@
// On Posix (not that you'd be likely to use the windows algorithm there),
// it uses os.tmpdir() always.
const platform = require('./platform.js')
const { statSync, promises: { stat } } = require('./fs.js')
const {
statSync,
promises: { stat },
} = require('./fs.js')
const { tmpdir } = require('os')
const { parse, resolve } = require('path')

Expand All @@ -21,7 +24,11 @@ const isDirSync = path => {
}
}

const isDir = path => stat(path).then(st => st.isDirectory(), () => false)
const isDir = path =>
stat(path).then(
st => st.isDirectory(),
() => false
)

const win32DefaultTmp = async path => {
const { root } = parse(path)
Expand Down Expand Up @@ -58,10 +65,13 @@ const win32DefaultTmpSync = path => {
const posixDefaultTmp = async () => tmpdir()
const posixDefaultTmpSync = () => tmpdir()

module.exports = platform === 'win32' ? {
defaultTmp: win32DefaultTmp,
defaultTmpSync: win32DefaultTmpSync,
} : {
defaultTmp: posixDefaultTmp,
defaultTmpSync: posixDefaultTmpSync,
}
module.exports =
platform === 'win32'
? {
defaultTmp: win32DefaultTmp,
defaultTmpSync: win32DefaultTmpSync,
}
: {
defaultTmp: posixDefaultTmp,
defaultTmpSync: posixDefaultTmpSync,
}
54 changes: 36 additions & 18 deletions lib/fs.js
Expand Up @@ -20,32 +20,50 @@ const {
// const makeCb = (res, rej) => (er, ...d) => er ? rej(er) : res(...d)
// which would be a bit cleaner.

const chmod = (...args) => new Promise((res, rej) =>
fs.chmod(...(args.concat((er, ...data) => er ? rej(er) : res(...data)))))
const chmod = (...args) =>
new Promise((res, rej) =>
fs.chmod(...args.concat((er, ...data) => (er ? rej(er) : res(...data))))
)

const mkdir = (...args) => new Promise((res, rej) =>
fs.mkdir(...(args.concat((er, ...data) => er ? rej(er) : res(...data)))))
const mkdir = (...args) =>
new Promise((res, rej) =>
fs.mkdir(...args.concat((er, ...data) => (er ? rej(er) : res(...data))))
)

const readdir = (...args) => new Promise((res, rej) =>
fs.readdir(...(args.concat((er, ...data) => er ? rej(er) : res(...data)))))
const readdir = (...args) =>
new Promise((res, rej) =>
fs.readdir(...args.concat((er, ...data) => (er ? rej(er) : res(...data))))
)

const rename = (...args) => new Promise((res, rej) =>
fs.rename(...(args.concat((er, ...data) => er ? rej(er) : res(...data)))))
const rename = (...args) =>
new Promise((res, rej) =>
fs.rename(...args.concat((er, ...data) => (er ? rej(er) : res(...data))))
)

const rm = (...args) => new Promise((res, rej) =>
fs.rm(...(args.concat((er, ...data) => er ? rej(er) : res(...data)))))
const rm = (...args) =>
new Promise((res, rej) =>
fs.rm(...args.concat((er, ...data) => (er ? rej(er) : res(...data))))
)

const rmdir = (...args) => new Promise((res, rej) =>
fs.rmdir(...(args.concat((er, ...data) => er ? rej(er) : res(...data)))))
const rmdir = (...args) =>
new Promise((res, rej) =>
fs.rmdir(...args.concat((er, ...data) => (er ? rej(er) : res(...data))))
)

const stat = (...args) => new Promise((res, rej) =>
fs.stat(...(args.concat((er, ...data) => er ? rej(er) : res(...data)))))
const stat = (...args) =>
new Promise((res, rej) =>
fs.stat(...args.concat((er, ...data) => (er ? rej(er) : res(...data))))
)

const unlink = (...args) => new Promise((res, rej) =>
fs.unlink(...(args.concat((er, ...data) => er ? rej(er) : res(...data)))))
const unlink = (...args) =>
new Promise((res, rej) =>
fs.unlink(...args.concat((er, ...data) => (er ? rej(er) : res(...data))))
)

const writeFile = (...args) => new Promise((res, rej) =>
fs.writeFile(...(args.concat((er, ...data) => er ? rej(er) : res(...data)))))
const writeFile = (...args) =>
new Promise((res, rej) =>
fs.writeFile(...args.concat((er, ...data) => (er ? rej(er) : res(...data))))
)

module.exports = {
chmodSync,
Expand Down
11 changes: 6 additions & 5 deletions lib/ignore-enoent.js
@@ -1,8 +1,9 @@
const ignoreENOENT = async p => p.catch(er => {
if (er.code !== 'ENOENT') {
throw er
}
})
const ignoreENOENT = async p =>
p.catch(er => {
if (er.code !== 'ENOENT') {
throw er
}
})

const ignoreENOENTSync = fn => {
try {
Expand Down
25 changes: 13 additions & 12 deletions lib/index.js
@@ -1,12 +1,15 @@
const pathArg = require('./path-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 {rimrafMoveRemove, rimrafMoveRemoveSync} = require('./rimraf-move-remove.js')
const {useNative, useNativeSync} = require('./use-native.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 {
rimrafMoveRemove,
rimrafMoveRemoveSync,
} = require('./rimraf-move-remove.js')
const { useNative, useNativeSync } = require('./use-native.js')

const wrap = fn => async (path, opt) => {
opt = optArg(opt)
Expand All @@ -23,14 +26,12 @@ const wrapSync = fn => (path, opt) => {
}

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

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

rimraf.rimraf = rimraf
rimraf.sync = rimraf.rimrafSync = rimrafSync
Expand Down
15 changes: 9 additions & 6 deletions lib/path-arg.js
Expand Up @@ -5,11 +5,14 @@ const pathArg = (path, opt = {}) => {
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}`
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',
Expand Down Expand Up @@ -38,7 +41,7 @@ const pathArg = (path, opt = {}) => {

if (platform === 'win32') {
const badWinChars = /[*|"<>?:]/
const {root} = parse(path)
const { root } = parse(path)
if (badWinChars.test(path.substr(root.length))) {
throw Object.assign(new Error('Illegal characters in path.'), {
path,
Expand Down
6 changes: 2 additions & 4 deletions lib/readdir-or-error.js
Expand Up @@ -2,12 +2,10 @@
// or the error that readdir() raised if not.
const {
readdirSync,
promises: {
readdir,
},
promises: { readdir },
} = require('./fs.js')
const readdirOrError = path => readdir(path).catch(er => er)
const readdirOrErrorSync = (path) => {
const readdirOrErrorSync = path => {
try {
return readdirSync(path)
} catch (er) {
Expand Down
11 changes: 4 additions & 7 deletions lib/rimraf-manual.js
Expand Up @@ -3,13 +3,10 @@ const platform = require('./platform.js')
const { rimrafWindows, rimrafWindowsSync } = require('./rimraf-windows.js')
const { rimrafPosix, rimrafPosixSync } = require('./rimraf-posix.js')

const [rimrafManual, rimrafManualSync] = platform === 'win32' ? [
rimrafWindows,
rimrafWindowsSync,
] : [
rimrafPosix,
rimrafPosixSync,
]
const [rimrafManual, rimrafManualSync] =
platform === 'win32'
? [rimrafWindows, rimrafWindowsSync]
: [rimrafPosix, rimrafPosixSync]

module.exports = {
rimrafManual,
Expand Down

0 comments on commit e7501cd

Please sign in to comment.