Skip to content

Commit 6950284

Browse files
author
John Doe
committed
refactor: fix lint
1 parent 177355e commit 6950284

File tree

2 files changed

+30
-27
lines changed

2 files changed

+30
-27
lines changed

packages/utils/src/lib/exit-process.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
import os from 'node:os';
22
import process from 'node:process';
33

4-
const isWindows = os.platform() === 'win32';
5-
64
// POSIX shells convention: exit status = 128 + signal number
75
// https://www.gnu.org/software/bash/manual/html_node/Exit-Status.html#:~:text=When%20a%20command%20terminates%20on%20a%20fatal%20signal%20whose%20number%20is%20N%2C%20Bash%20uses%20the%20value%20128%2BN%20as%20the%20exit%20status.
86
const UNIX_SIGNAL_EXIT_CODE_OFFSET = 128;
97
const unixSignalExitCode = (signalNumber: number) =>
108
UNIX_SIGNAL_EXIT_CODE_OFFSET + signalNumber;
119

1210
const SIGINT_CODE = 2;
11+
const SIGTERM_CODE = 15;
12+
const SIGQUIT_CODE = 3;
1313

1414
export const SIGNAL_EXIT_CODES = (): Record<SignalName, number> => {
1515
const isWindowsRuntime = os.platform() === 'win32';
1616
return {
1717
SIGINT: isWindowsRuntime ? SIGINT_CODE : unixSignalExitCode(SIGINT_CODE),
18-
SIGTERM: unixSignalExitCode(15),
19-
SIGQUIT: unixSignalExitCode(3),
18+
SIGTERM: unixSignalExitCode(SIGTERM_CODE),
19+
SIGQUIT: unixSignalExitCode(SIGQUIT_CODE),
2020
};
2121
};
2222

@@ -39,6 +39,7 @@ export type ExitHandlerOptions = {
3939
};
4040

4141
export function installExitHandlers(options: ExitHandlerOptions = {}): void {
42+
// eslint-disable-next-line functional/no-let
4243
let closedReason: CloseReason | undefined;
4344
const {
4445
onClose,
@@ -49,40 +50,47 @@ export function installExitHandlers(options: ExitHandlerOptions = {}): void {
4950
} = options;
5051

5152
const close = (code: number, reason: CloseReason) => {
52-
if (closedReason) return;
53+
if (closedReason) {
54+
return;
55+
}
5356
closedReason = reason;
5457
onClose?.(code, reason);
5558
};
5659

5760
process.on('uncaughtException', err => {
5861
onError?.(err, 'uncaughtException');
59-
if (fatalExit)
62+
if (fatalExit) {
6063
close(fatalExitCode, {
6164
kind: 'fatal',
6265
fatal: 'uncaughtException',
6366
});
67+
}
6468
});
6569

6670
process.on('unhandledRejection', reason => {
6771
onError?.(reason, 'unhandledRejection');
68-
if (fatalExit)
72+
if (fatalExit) {
6973
close(fatalExitCode, {
7074
kind: 'fatal',
7175
fatal: 'unhandledRejection',
7276
});
77+
}
7378
});
7479

7580
(['SIGINT', 'SIGTERM', 'SIGQUIT'] as const).forEach(signal => {
7681
process.on(signal, () => {
7782
close(SIGNAL_EXIT_CODES()[signal], { kind: 'signal', signal });
7883
if (signalExit) {
84+
// eslint-disable-next-line n/no-process-exit
7985
process.exit(SIGNAL_EXIT_CODES()[signal]);
8086
}
8187
});
8288
});
8389

8490
process.on('exit', code => {
85-
if (closedReason) return;
91+
if (closedReason) {
92+
return;
93+
}
8694
close(code, { kind: 'exit' });
8795
});
8896
}

packages/utils/src/lib/exit-process.unit.test.ts

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os from 'node:os';
12
import process from 'node:process';
23
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
34
import { SIGNAL_EXIT_CODES, installExitHandlers } from './exit-process.js';
@@ -215,28 +216,22 @@ describe('exit-process tests', () => {
215216
expect(onClose).toHaveBeenCalledTimes(1);
216217
});
217218

218-
it('should have correct SIGINT exit code based on platform', () => {
219-
const os = require('node:os');
220-
const isWindows = os.platform() === 'win32';
221-
const SIGINT_CODE = 2;
222-
const UNIX_SIGNAL_EXIT_CODE_OFFSET = 128;
223-
224-
const expectedSigintCode = isWindows
225-
? SIGINT_CODE
226-
: UNIX_SIGNAL_EXIT_CODE_OFFSET + SIGINT_CODE;
227-
228-
if (isWindows) {
229-
expect(expectedSigintCode).toBe(2);
230-
} else {
231-
expect(expectedSigintCode).toBe(130);
232-
expect(SIGNAL_EXIT_CODES().SIGINT).toBe(130);
233-
}
219+
it('should have correct SIGINT exit code on Windows', () => {
220+
const osSpy = vi.spyOn(os, 'platform').mockReturnValue('win32');
221+
const exitCodes = SIGNAL_EXIT_CODES();
222+
expect(exitCodes.SIGINT).toBe(2);
223+
osSpy.mockRestore();
224+
});
225+
226+
it('should have correct SIGINT exit code on Unix-like systems', () => {
227+
const osSpy = vi.spyOn(os, 'platform').mockReturnValue('linux');
228+
const exitCodes = SIGNAL_EXIT_CODES();
229+
expect(exitCodes.SIGINT).toBe(130);
230+
osSpy.mockRestore();
234231
});
235232

236233
it('should calculate Windows exit codes correctly when platform is mocked to Windows', () => {
237-
const osSpy = vi
238-
.spyOn(require('node:os'), 'platform')
239-
.mockReturnValue('win32');
234+
const osSpy = vi.spyOn(os, 'platform').mockReturnValue('win32');
240235

241236
const exitCodes = SIGNAL_EXIT_CODES();
242237

0 commit comments

Comments
 (0)