Skip to content

Commit

Permalink
add --verbose, --no-verbose to bin
Browse files Browse the repository at this point in the history
Fix: #140
  • Loading branch information
isaacs committed Mar 4, 2023
1 parent ed3288e commit 3b57687
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

- Return boolean indicating whether the path was fully removed
- Add filter option
- bin: add --verbose, -v to print files as they are deleted
- bin: add --no-verbose, -V to not print files as they are deleted

# v4.2

Expand Down
19 changes: 18 additions & 1 deletion src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ Options:
--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
-v --verbose Be verbose when deleting files, showing them as
they are removed
-V --no-verbose Be silent when deleting files, showing nothing as
they are removed (default)
--impl=<type> Specify the implementation to use.
rimraf: choose the best option
Expand All @@ -34,9 +38,16 @@ Implementation-specific options:
--backoff=<n> Exponential backoff factor for retries (default: 1.2)
`

import { parse, resolve } from 'path'
import { parse, relative, resolve } from 'path'
const cwd = process.cwd()

const main = async (...args: string[]) => {
const yesFilter = () => true
const verboseFilter = (s: string) => {
console.log(relative(cwd, s))
return true
}

if (process.env.__RIMRAF_TESTING_BIN_FAIL__ === '1') {
throw new Error('simulated rimraf failure')
}
Expand All @@ -61,6 +72,12 @@ const main = async (...args: string[]) => {
} else if (arg === '-h' || arg === '--help') {
console.log(help)
return 0
} else if (arg === '--verbose' || arg === '-v') {
opt.filter = verboseFilter
continue
} else if (arg === '--no-verbose' || arg === '-V') {
opt.filter = yesFilter
continue
} else if (arg === '-g' || arg === '--glob') {
opt.glob = true
continue
Expand Down
53 changes: 53 additions & 0 deletions test/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,59 @@ t.test('basic arg parsing stuff', t => {
])
})

t.test('verbose', async t => {
t.equal(await bin('-v', 'foo'), 0)
t.equal(await bin('--verbose', 'foo'), 0)
t.equal(await bin('-v', '-V', '--verbose', 'foo'), 0)
t.same(LOGS, [])
t.same(ERRS, [])
for (const c of CALLS) {
t.equal(c[0], 'rimraf')
t.same(c[1], ['foo'])
t.type(c[2].filter, 'function')
t.equal(c[2].filter('x'), true)
}
})

t.test('verbose', async t => {
t.equal(await bin('-v', 'foo'), 0)
t.equal(await bin('--verbose', 'foo'), 0)
t.equal(await bin('-v', '-V', '--verbose', 'foo'), 0)
t.same(LOGS, [])
t.same(ERRS, [])
const { log } = console
t.teardown(() => { console.log = log })
const logs = []
console.log = (s) => logs.push(s)
for (const c of CALLS) {
t.equal(c[0], 'rimraf')
t.same(c[1], ['foo'])
t.type(c[2].filter, 'function')
t.equal(c[2].filter('x'), true)
t.same(logs, ['x'])
logs.length = 0
}
})

t.test('silent', async t => {
t.equal(await bin('-V', 'foo'), 0)
t.equal(await bin('--no-verbose', 'foo'), 0)
t.equal(await bin('-V', '-v', '--no-verbose', 'foo'), 0)
t.same(LOGS, [])
t.same(ERRS, [])
const { log } = console
t.teardown(() => { console.log = log })
const logs = []
console.log = (s) => logs.push(s)
for (const c of CALLS) {
t.equal(c[0], 'rimraf')
t.same(c[1], ['foo'])
t.type(c[2].filter, 'function')
t.equal(c[2].filter('x'), true)
t.same(logs, [])
}
})

t.test('glob true', async t => {
t.equal(await bin('-g', 'foo'), 0)
t.equal(await bin('--glob', 'foo'), 0)
Expand Down

0 comments on commit 3b57687

Please sign in to comment.