MCPcopy Create free account
hub / github.com/code-pushup/cli / subscribeProcessExit

Function subscribeProcessExit

packages/utils/src/lib/exit-process.ts:52–126  ·  view source on GitHub ↗
(
  options: ExitHandlerOptions = {},
)

Source from the content-addressed store, hash-verified

50 */
51// eslint-disable-next-line max-lines-per-function
52export function subscribeProcessExit(
53 options: ExitHandlerOptions = {},
54): () => void {
55 // eslint-disable-next-line functional/no-let
56 let closedReason: CloseReason | undefined;
57 const {
58 onExit,
59 onError,
60 exitOnFatal = false,
61 exitOnSignal = false,
62 fatalExitCode = DEFAULT_FATAL_EXIT_CODE,
63 } = options;
64
65 const close = (code: number, reason: CloseReason) => {
66 if (closedReason) {
67 return;
68 }
69 closedReason = reason;
70 onExit?.(code, reason);
71 };
72
73 const uncaughtExceptionHandler = (err: unknown) => {
74 onError?.(err, 'uncaughtException');
75 if (exitOnFatal) {
76 close(fatalExitCode, {
77 kind: 'fatal',
78 fatal: 'uncaughtException',
79 });
80 }
81 };
82
83 const unhandledRejectionHandler = (reason: unknown) => {
84 onError?.(reason, 'unhandledRejection');
85 if (exitOnFatal) {
86 close(fatalExitCode, {
87 kind: 'fatal',
88 fatal: 'unhandledRejection',
89 });
90 }
91 };
92
93 const signalHandlers = (['SIGINT', 'SIGTERM', 'SIGQUIT'] as const).map(
94 signal => {
95 const handler = () => {
96 close(SIGNAL_EXIT_CODES()[signal], { kind: 'signal', signal });
97 if (exitOnSignal) {
98 // eslint-disable-next-line unicorn/no-process-exit,n/no-process-exit
99 process.exit(SIGNAL_EXIT_CODES()[signal]);
100 }
101 };
102 process.on(signal, handler);
103 return { signal, handler };
104 },
105 );
106
107 const exitHandler = (code: number) => {
108 if (closedReason) {
109 return;

Callers 3

constructorMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected