From 3b57687ac28c7feee2caae455fb52d1b55e39f92 Mon Sep 17 00:00:00 2001 From: isaacs Date: Fri, 3 Mar 2023 12:56:33 -0800 Subject: [PATCH] add --verbose, --no-verbose to bin Fix: #140 --- CHANGELOG.md | 2 ++ src/bin.ts | 19 ++++++++++++++++++- test/bin.js | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 97971fa..766630f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/bin.ts b/src/bin.ts index f71b613..ad49e31 100755 --- a/src/bin.ts +++ b/src/bin.ts @@ -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= Specify the implementation to use. rimraf: choose the best option @@ -34,9 +38,16 @@ Implementation-specific options: --backoff= 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') } @@ -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 diff --git a/test/bin.js b/test/bin.js index 2940763..f8e7557 100644 --- a/test/bin.js +++ b/test/bin.js @@ -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)