From 0a84af6eecb188312a75a6742353c4ea817c1594 Mon Sep 17 00:00:00 2001 From: isaacs Date: Mon, 25 Sep 2023 15:23:49 -0700 Subject: [PATCH] bin: remove realpath, use testing env instead If env.__TESTING_RIMRAF_BIN__=='1', then just export. Otherwise, do the thing. This saves a realpath call at startup in the bin. --- src/bin.mts | 7 +------ test/bin.ts | 2 ++ 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/bin.mts b/src/bin.mts index 324de6cd..5f563129 100755 --- a/src/bin.mts +++ b/src/bin.mts @@ -1,5 +1,4 @@ #!/usr/bin/env node -import { realpathSync } from 'fs' import { readFile } from 'fs/promises' import type { RimrafAsyncOptions } from './index.js' import { rimraf } from './index.js' @@ -262,11 +261,7 @@ main.help = help export default main -const isMainModule = - realpathSync(String(process.argv[1])) === - realpathSync(fileURLToPath(import.meta.url)) - -if (isMainModule) { +if (process.env.__TESTING_RIMRAF_BIN__ !== '1') { const args = process.argv.slice(2) main(...args).then( code => process.exit(code), diff --git a/test/bin.ts b/test/bin.ts index 93b21aab..2e4769b7 100644 --- a/test/bin.ts +++ b/test/bin.ts @@ -10,9 +10,11 @@ t.test('basic arg parsing stuff', async t => { const LOGS: any[] = [] const ERRS: any[] = [] const { log: consoleLog, error: consoleError } = console + process.env.__TESTING_RIMRAF_BIN__ = '1' t.teardown(() => { console.log = consoleLog console.error = consoleError + delete process.env.__TESTING_RIMRAF_BIN__ }) console.log = (...msg) => LOGS.push(msg) console.error = (...msg) => ERRS.push(msg)