@take-out/scripts 0.1.29 → 0.1.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/helpers/handleProcessExit.ts +21 -29
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@take-out/scripts",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.30",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./src/run.ts",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"dependencies": {
|
|
31
31
|
"@clack/prompts": "^0.8.2",
|
|
32
32
|
"@lydell/node-pty": "^1.2.0-beta.3",
|
|
33
|
-
"@take-out/helpers": "0.1.
|
|
33
|
+
"@take-out/helpers": "0.1.30",
|
|
34
34
|
"picocolors": "^1.1.1"
|
|
35
35
|
},
|
|
36
36
|
"peerDependencies": {
|
|
@@ -5,18 +5,6 @@ import {
|
|
|
5
5
|
type ProcessType,
|
|
6
6
|
} from './run'
|
|
7
7
|
|
|
8
|
-
// reset terminal to sane state
|
|
9
|
-
function resetTerminal() {
|
|
10
|
-
try {
|
|
11
|
-
// restore cooked mode if we have a TTY
|
|
12
|
-
if (process.stdin.isTTY && process.stdin.setRawMode) {
|
|
13
|
-
process.stdin.setRawMode(false)
|
|
14
|
-
}
|
|
15
|
-
} catch {}
|
|
16
|
-
// reset ANSI attributes
|
|
17
|
-
process.stdout.write('\x1b[0m')
|
|
18
|
-
}
|
|
19
|
-
|
|
20
8
|
type ExitCallback = (info: { signal: NodeJS.Signals | string }) => void | Promise<void>
|
|
21
9
|
|
|
22
10
|
interface HandleProcessExitReturn {
|
|
@@ -85,13 +73,6 @@ export function handleProcessExit({
|
|
|
85
73
|
const doCleanup = async (signal: NodeJS.Signals | string) => {
|
|
86
74
|
setExitCleanupState(true)
|
|
87
75
|
|
|
88
|
-
if (signal === 'SIGINT') {
|
|
89
|
-
const noop = () => {}
|
|
90
|
-
console.log = noop
|
|
91
|
-
console.info = noop
|
|
92
|
-
console.warn = noop
|
|
93
|
-
}
|
|
94
|
-
|
|
95
76
|
if (onExit) {
|
|
96
77
|
try {
|
|
97
78
|
await onExit({ signal })
|
|
@@ -133,22 +114,32 @@ export function handleProcessExit({
|
|
|
133
114
|
|
|
134
115
|
addProcessHandler(addChildProcess)
|
|
135
116
|
|
|
117
|
+
let finalized = false
|
|
118
|
+
|
|
119
|
+
const finalizeWithSignal = (signal: NodeJS.Signals, fallbackCode: number) => {
|
|
120
|
+
if (finalized) return
|
|
121
|
+
finalized = true
|
|
122
|
+
process.off('SIGINT', sigintHandler)
|
|
123
|
+
process.off('SIGTERM', sigtermHandler)
|
|
124
|
+
process.off('beforeExit', beforeExitHandler)
|
|
125
|
+
process.exit = originalExit
|
|
126
|
+
|
|
127
|
+
try {
|
|
128
|
+
process.kill(process.pid, signal)
|
|
129
|
+
} catch {
|
|
130
|
+
originalExit(fallbackCode)
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
136
134
|
const sigtermHandler = () => {
|
|
137
135
|
cleanup('SIGTERM').then(() => {
|
|
138
|
-
|
|
136
|
+
finalizeWithSignal('SIGTERM', 143)
|
|
139
137
|
})
|
|
140
138
|
}
|
|
141
139
|
|
|
142
140
|
const sigintHandler = () => {
|
|
143
|
-
// restore terminal to sane state
|
|
144
|
-
resetTerminal()
|
|
145
|
-
// immediately print newline and reset cursor for clean terminal
|
|
146
|
-
process.stdout.write('\n')
|
|
147
|
-
// reset terminal attributes
|
|
148
|
-
process.stdout.write('\x1b[0m')
|
|
149
|
-
|
|
150
141
|
cleanup('SIGINT').then(() => {
|
|
151
|
-
|
|
142
|
+
finalizeWithSignal('SIGINT', 130)
|
|
152
143
|
})
|
|
153
144
|
}
|
|
154
145
|
|
|
@@ -162,7 +153,8 @@ export function handleProcessExit({
|
|
|
162
153
|
})
|
|
163
154
|
}) as typeof process.exit
|
|
164
155
|
|
|
165
|
-
|
|
156
|
+
const beforeExitHandler = () => cleanup('SIGTERM')
|
|
157
|
+
process.on('beforeExit', beforeExitHandler)
|
|
166
158
|
process.on('SIGINT', sigintHandler)
|
|
167
159
|
process.on('SIGTERM', sigtermHandler)
|
|
168
160
|
|