Skip to content

Commit

Permalink
refactor: replace signal-exit with built-in solution
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Nov 21, 2024
1 parent d659260 commit ccba6a0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
1 change: 0 additions & 1 deletion packages/vitest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@
"expect-type": "^1.1.0",
"magic-string": "^0.30.12",
"pathe": "^1.1.2",
"signal-exit": "^3.0.7",
"std-env": "^3.8.0",
"tinybench": "^2.9.0",
"tinyexec": "^0.3.1",
Expand Down
36 changes: 27 additions & 9 deletions packages/vitest/src/node/reporters/renderers/windowedRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ import type { Writable } from 'node:stream'
import type { Vitest } from '../../core'
import { stripVTControlCharacters } from 'node:util'

// @ts-expect-error -- untyped, cannot use v4 due to other deps
import onExit from 'signal-exit'

const DEFAULT_RENDER_INTERVAL = 16

const ESC = '\x1B['
Expand Down Expand Up @@ -48,15 +45,10 @@ export class WindowRenderer {
error: options.logger.errorStream.write.bind(options.logger.errorStream),
}

// Write buffered content on unexpected exits, e.g. direct `process.exit()` calls
onExit(() => {
this.flushBuffer()
this.write(SHOW_CURSOR)
})

this.cleanups.push(
this.interceptStream(process.stdout, 'output'),
this.interceptStream(process.stderr, 'error'),
this.addProcessExitListeners(),
)

this.write(HIDE_CURSOR, 'output')
Expand Down Expand Up @@ -182,6 +174,32 @@ export class WindowRenderer {
private write(message: string, type: 'output' | 'error' = 'output') {
(this.streams[type] as Writable['write'])(message)
}

private addProcessExitListeners() {
const onExit = (signal?: string | number, exitCode?: number) => {
// Write buffered content on unexpected exits, e.g. direct `process.exit()` calls
this.flushBuffer()
this.stop()

// Interrupted signals don't set exit code automatically.
// Use same exit code as node: https://nodejs.org/api/process.html#signal-events
if (process.exitCode === undefined) {
process.exitCode = exitCode !== undefined ? (128 + exitCode) : Number(signal)
}

process.exit()
}

process.once('SIGINT', onExit)
process.once('SIGTERM', onExit)
process.once('exit', onExit)

return function cleanup() {
process.off('SIGINT', onExit)
process.off('SIGTERM', onExit)
process.off('exit', onExit)
}
}
}

/** Calculate the actual row count needed to render `rows` into `stream` */
Expand Down
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ccba6a0

Please sign in to comment.