Skip to content

Commit

Permalink
treat paths as glob patterns when glob option set
Browse files Browse the repository at this point in the history
Fix: #249
  • Loading branch information
isaacs committed Mar 3, 2023
1 parent 5760716 commit f768f26
Show file tree
Hide file tree
Showing 11 changed files with 319 additions and 25 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ Synchronous form of `rimraf.moveRemove()`
### Command Line Interface

```
rimraf version 4.0.4
rimraf version 4.2.0
Usage: rimraf <path> [<path> ...]
Deletes all files and folders at "path", recursively.
Expand All @@ -151,8 +151,10 @@ Options:
-h --help Display this usage info
--preserve-root Do not remove '/' recursively (default)
--no-preserve-root Do not treat '/' specially
-G --no-glob Treat arguments as literal paths, not globs (default)
-g --glob Treat arguments as glob patterns
--impl=<type> Specify the implementationt to use.
--impl=<type> Specify the implementation to use.
rimraf: choose the best option
native: the built-in implementation in Node.js
manual: the platform-specific JS implementation
Expand Down
2 changes: 2 additions & 0 deletions fixup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

cat >dist/cjs/package.json <<!EOF
{
"version": "$(node -p 'require("./package.json").version')",
"type": "commonjs"
}
!EOF

cat >dist/mjs/package.json <<!EOF
{
"version": "$(node -p 'require("./package.json").version')",
"type": "module"
}
!EOF
Expand Down
167 changes: 153 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,8 @@
},
"engines": {
"node": ">=14"
},
"dependencies": {
"glob": "^9.2.0"
}
}
10 changes: 9 additions & 1 deletion src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ Options:
-h --help Display this usage info
--preserve-root Do not remove '/' recursively (default)
--no-preserve-root Do not treat '/' specially
-G --no-glob Treat arguments as literal paths, not globs (default)
-g --glob Treat arguments as glob patterns
--impl=<type> Specify the implementationt to use.
--impl=<type> Specify the implementation to use.
rimraf: choose the best option
native: the built-in implementation in Node.js
manual: the platform-specific JS implementation
Expand Down Expand Up @@ -59,6 +61,12 @@ const main = async (...args: string[]) => {
} else if (arg === '-h' || arg === '--help') {
console.log(help)
return 0
} else if (arg === '-g' || arg === '--glob') {
opt.glob = true
continue
} else if (arg === '-G' || arg === '--no-glob') {
opt.glob = false
continue
} else if (arg === '--preserve-root') {
opt.preserveRoot = true
continue
Expand Down
14 changes: 12 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import optArg from './opt-arg.js'
import pathArg from './path-arg.js'

import { glob, GlobOptions, globSync } from 'glob'

export interface RimrafOptions {
preserveRoot?: boolean
tmp?: string
Expand All @@ -9,6 +11,7 @@ export interface RimrafOptions {
backoff?: number
maxBackoff?: number
signal?: AbortSignal
glob?: boolean | GlobOptions
}

const typeOrUndef = (val: any, t: string) =>
Expand All @@ -22,7 +25,8 @@ export const isRimrafOptions = (o: any): o is RimrafOptions =>
typeOrUndef(o.maxRetries, 'number') &&
typeOrUndef(o.retryDelay, 'number') &&
typeOrUndef(o.backoff, 'number') &&
typeOrUndef(o.maxBackoff, 'number')
typeOrUndef(o.maxBackoff, 'number') &&
(typeOrUndef(o.glob, 'boolean') || (o.glob && typeof o.glob === 'object'))

export const assertRimrafOptions: (o: any) => void = (
o: any
Expand All @@ -42,7 +46,10 @@ import { useNative, useNativeSync } from './use-native.js'
const wrap =
(fn: (p: string, o: RimrafOptions) => Promise<void>) =>
async (path: string | string[], opt?: RimrafOptions): Promise<void> => {
const options: RimrafOptions = optArg(opt)
const options = optArg(opt)
if (options.glob) {
path = await glob(path, options.glob)
}
await (Array.isArray(path)
? Promise.all(path.map(p => fn(pathArg(p, options), options)))
: fn(pathArg(path, options), options))
Expand All @@ -52,6 +59,9 @@ const wrapSync =
(fn: (p: string, o: RimrafOptions) => void) =>
(path: string | string[], opt?: RimrafOptions): void => {
const options = optArg(opt)
if (options.glob) {
path = globSync(path, options.glob)
}
return Array.isArray(path)
? path.forEach(p => fn(pathArg(p, options), options))
: fn(pathArg(path, options), options)
Expand Down
31 changes: 29 additions & 2 deletions src/opt-arg.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
import { GlobOptions } from 'glob'
import { assertRimrafOptions, RimrafOptions } from './index.js'
export default (opt: RimrafOptions = {}) => {
export default (
opt: RimrafOptions = {}
): RimrafOptions & {
glob?: GlobOptions & { withFileTypes: false }
} => {
assertRimrafOptions(opt)
return opt
const { glob, ...options } = opt
if (!glob) return options
const globOpt =
glob === true
? opt.signal
? { signal: opt.signal }
: {}
: opt.signal
? {
signal: opt.signal,
...glob,
}
: glob
return {
...options,
glob: {
...globOpt,
// always get absolute paths from glob, to ensure
// that we are referencing the correct thing.
absolute: true,
withFileTypes: false,
},
}
}
Loading

0 comments on commit f768f26

Please sign in to comment.