Skip to content

Commit 3b57687

Browse files
committed
add --verbose, --no-verbose to bin
Fix: #140
1 parent ed3288e commit 3b57687

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

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

68
# v4.2
79

src/bin.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ Options:
1818
--no-preserve-root Do not treat '/' specially
1919
-G --no-glob Treat arguments as literal paths, not globs (default)
2020
-g --glob Treat arguments as glob patterns
21+
-v --verbose Be verbose when deleting files, showing them as
22+
they are removed
23+
-V --no-verbose Be silent when deleting files, showing nothing as
24+
they are removed (default)
2125
2226
--impl=<type> Specify the implementation to use.
2327
rimraf: choose the best option
@@ -34,9 +38,16 @@ Implementation-specific options:
3438
--backoff=<n> Exponential backoff factor for retries (default: 1.2)
3539
`
3640

37-
import { parse, resolve } from 'path'
41+
import { parse, relative, resolve } from 'path'
42+
const cwd = process.cwd()
3843

3944
const main = async (...args: string[]) => {
45+
const yesFilter = () => true
46+
const verboseFilter = (s: string) => {
47+
console.log(relative(cwd, s))
48+
return true
49+
}
50+
4051
if (process.env.__RIMRAF_TESTING_BIN_FAIL__ === '1') {
4152
throw new Error('simulated rimraf failure')
4253
}
@@ -61,6 +72,12 @@ const main = async (...args: string[]) => {
6172
} else if (arg === '-h' || arg === '--help') {
6273
console.log(help)
6374
return 0
75+
} else if (arg === '--verbose' || arg === '-v') {
76+
opt.filter = verboseFilter
77+
continue
78+
} else if (arg === '--no-verbose' || arg === '-V') {
79+
opt.filter = yesFilter
80+
continue
6481
} else if (arg === '-g' || arg === '--glob') {
6582
opt.glob = true
6683
continue

test/bin.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,59 @@ t.test('basic arg parsing stuff', t => {
6666
])
6767
})
6868

69+
t.test('verbose', async t => {
70+
t.equal(await bin('-v', 'foo'), 0)
71+
t.equal(await bin('--verbose', 'foo'), 0)
72+
t.equal(await bin('-v', '-V', '--verbose', 'foo'), 0)
73+
t.same(LOGS, [])
74+
t.same(ERRS, [])
75+
for (const c of CALLS) {
76+
t.equal(c[0], 'rimraf')
77+
t.same(c[1], ['foo'])
78+
t.type(c[2].filter, 'function')
79+
t.equal(c[2].filter('x'), true)
80+
}
81+
})
82+
83+
t.test('verbose', async t => {
84+
t.equal(await bin('-v', 'foo'), 0)
85+
t.equal(await bin('--verbose', 'foo'), 0)
86+
t.equal(await bin('-v', '-V', '--verbose', 'foo'), 0)
87+
t.same(LOGS, [])
88+
t.same(ERRS, [])
89+
const { log } = console
90+
t.teardown(() => { console.log = log })
91+
const logs = []
92+
console.log = (s) => logs.push(s)
93+
for (const c of CALLS) {
94+
t.equal(c[0], 'rimraf')
95+
t.same(c[1], ['foo'])
96+
t.type(c[2].filter, 'function')
97+
t.equal(c[2].filter('x'), true)
98+
t.same(logs, ['x'])
99+
logs.length = 0
100+
}
101+
})
102+
103+
t.test('silent', async t => {
104+
t.equal(await bin('-V', 'foo'), 0)
105+
t.equal(await bin('--no-verbose', 'foo'), 0)
106+
t.equal(await bin('-V', '-v', '--no-verbose', 'foo'), 0)
107+
t.same(LOGS, [])
108+
t.same(ERRS, [])
109+
const { log } = console
110+
t.teardown(() => { console.log = log })
111+
const logs = []
112+
console.log = (s) => logs.push(s)
113+
for (const c of CALLS) {
114+
t.equal(c[0], 'rimraf')
115+
t.same(c[1], ['foo'])
116+
t.type(c[2].filter, 'function')
117+
t.equal(c[2].filter('x'), true)
118+
t.same(logs, [])
119+
}
120+
})
121+
69122
t.test('glob true', async t => {
70123
t.equal(await bin('-g', 'foo'), 0)
71124
t.equal(await bin('--glob', 'foo'), 0)

0 commit comments

Comments
 (0)