Skip to content

Commit

Permalink
bin: remove realpath, use testing env instead
Browse files Browse the repository at this point in the history
If env.__TESTING_RIMRAF_BIN__=='1', then just export. Otherwise,
do the thing. This saves a realpath call at startup in the bin.
  • Loading branch information
isaacs committed Sep 25, 2023
1 parent 2e8e0d3 commit 0a84af6
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 6 deletions.
7 changes: 1 addition & 6 deletions src/bin.mts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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),
Expand Down
2 changes: 2 additions & 0 deletions test/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 0a84af6

Please sign in to comment.