-
-
Notifications
You must be signed in to change notification settings - Fork 124
/
preflight.cts
48 lines (43 loc) · 1.26 KB
/
preflight.cts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { constants as osConstants } from 'os';
import './suppress-warnings.cts';
/**
* Hook require() to transform to CJS
*
* This needs to be loaded via --require flag so subsequent --require
* flags can support TypeScript.
*
* This is also added in loader.ts for the loader API.
* Although it is required twice, it's not executed twice because
* it's cached.
*/
require('@esbuild-kit/cjs-loader');
// If a parent process is detected
if (process.send) {
function relaySignal(signal: NodeJS.Signals) {
process.send!({
type: 'kill',
signal,
});
/**
* Since we're setting a custom signal handler, we need to emulate the
* default behavior when there are no other handlers set
*/
if (process.listenerCount(signal) === 0) {
// eslint-disable-next-line unicorn/no-process-exit
process.exit(128 + osConstants.signals[signal]);
}
}
const relaySignals = ['SIGINT', 'SIGTERM'] as const;
for (const signal of relaySignals) {
process.on(signal, relaySignal);
}
// Reduce the listenerCount to hide the one set above
const { listenerCount } = process;
process.listenerCount = function (eventName) {
let count = Reflect.apply(listenerCount, this, arguments);
if (relaySignals.includes(eventName as any)) {
count -= 1;
}
return count;
};
}