@notionhq/custom-blocks-dev-shell 0.1.69 → 0.1.71

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.
@@ -1,164 +0,0 @@
1
- /**
2
- * Own child processes and non-child resources for one dev shell run.
3
- */
4
- import { spawn } from "node:child_process";
5
- import { createInterface } from "node:readline";
6
- import { makeChangeLogger } from "./vite-block-server.js";
7
- const SHUTDOWN_GRACE_MS = 1500;
8
- const dim = "\x1b[2m";
9
- const reset = "\x1b[0m";
10
- export class ProcessSupervisor {
11
- callbacks;
12
- processes = new Map();
13
- resources = [];
14
- shuttingDown = false;
15
- constructor(callbacks) {
16
- this.callbacks = callbacks;
17
- }
18
- addResource(resource) {
19
- if (this.shuttingDown) {
20
- void closeResource(resource);
21
- return;
22
- }
23
- this.resources.push(resource);
24
- }
25
- start(specs) {
26
- for (const spec of specs) {
27
- if (this.shuttingDown) {
28
- return;
29
- }
30
- const detached = spec.detached ?? process.platform !== "win32";
31
- const proc = spawn(spec.command, [...spec.args], {
32
- cwd: spec.cwd,
33
- stdio: ["ignore", "pipe", "inherit"],
34
- detached,
35
- env: spec.env === undefined
36
- ? process.env
37
- : { ...process.env, ...spec.env },
38
- });
39
- this.processes.set(proc, detached);
40
- if (proc.stdout !== null) {
41
- createInterface({ input: proc.stdout }).on("line", makeChangeLogger(message => this.callbacks.onChange(spec.name, message)));
42
- }
43
- proc.on("error", error => {
44
- if (this.shuttingDown) {
45
- return;
46
- }
47
- this.callbacks.onProcessError(spec.name, error);
48
- process.exitCode = 1;
49
- this.shutdown("SIGTERM");
50
- });
51
- proc.on("exit", code => {
52
- // Retain an unexpectedly failed process until cleanup. Its PID may
53
- // still identify a detached process group with live descendants.
54
- if (!this.shuttingDown && (code === 0 || code === null)) {
55
- this.processes.delete(proc);
56
- }
57
- if (this.shuttingDown || code === 0 || code === null) {
58
- return;
59
- }
60
- this.callbacks.onUnexpectedExit(spec.name, code);
61
- process.exitCode = code;
62
- this.shutdown("SIGTERM");
63
- });
64
- }
65
- }
66
- shutdown(signal) {
67
- if (this.shuttingDown) {
68
- if (signal !== "exit") {
69
- this.forceShutdown();
70
- }
71
- return;
72
- }
73
- this.shuttingDown = true;
74
- if (signal !== "exit" &&
75
- (this.processes.size > 0 || this.resources.length > 0)) {
76
- console.log(`\n${dim}Shutting down...${reset}`);
77
- }
78
- for (const [proc, detached] of this.processes) {
79
- killProcess(proc, detached, "SIGTERM");
80
- }
81
- if (signal === "exit") {
82
- for (const resource of this.resources) {
83
- void closeResource(resource);
84
- }
85
- return;
86
- }
87
- void this.finishShutdown();
88
- }
89
- async finishShutdown() {
90
- const resourceCleanup = Promise.all(this.resources.map(resource => closeResource(resource)));
91
- const processCleanup = Promise.all([...this.processes.keys()].map(proc => waitForExit(proc)));
92
- let finished = false;
93
- await Promise.race([
94
- Promise.all([resourceCleanup, processCleanup]).then(() => {
95
- finished = true;
96
- }),
97
- delay(SHUTDOWN_GRACE_MS),
98
- ]);
99
- if (!finished) {
100
- this.forceShutdown();
101
- return;
102
- }
103
- // A process can exit before its descendants do. Re-signal every
104
- // remaining process group even after the direct child has exited.
105
- this.forceKillProcesses();
106
- process.exit(process.exitCode ?? 0);
107
- }
108
- forceShutdown() {
109
- this.forceKillProcesses();
110
- process.exit(process.exitCode ?? 130);
111
- }
112
- forceKillProcesses() {
113
- for (const [proc, detached] of this.processes) {
114
- killProcess(proc, detached, "SIGKILL", true);
115
- }
116
- }
117
- }
118
- async function closeResource(resource) {
119
- try {
120
- await resource();
121
- }
122
- catch { }
123
- }
124
- function killProcess(proc, detached, signal, force = false) {
125
- const pid = proc.pid;
126
- if (pid === undefined) {
127
- return;
128
- }
129
- try {
130
- if (detached && process.platform !== "win32") {
131
- process.kill(-pid, signal);
132
- return;
133
- }
134
- if (force || isProcessRunning(proc)) {
135
- proc.kill(signal);
136
- }
137
- }
138
- catch { }
139
- }
140
- function isProcessRunning(proc) {
141
- return proc.exitCode === null && proc.signalCode === null;
142
- }
143
- function waitForExit(proc) {
144
- if (proc.exitCode !== null || proc.signalCode !== null) {
145
- return Promise.resolve();
146
- }
147
- return new Promise(resolve => {
148
- proc.once("exit", () => resolve());
149
- });
150
- }
151
- function delay(milliseconds) {
152
- return new Promise(resolve => setTimeout(resolve, milliseconds));
153
- }
154
- export function installProcessSignalHandlers(run) {
155
- for (const signal of ["SIGINT", "SIGTERM", "SIGHUP"]) {
156
- process.on(signal, () => run.shutdown(signal));
157
- }
158
- process.on("exit", () => run.shutdown("exit"));
159
- process.on("uncaughtException", error => {
160
- console.error(error);
161
- process.exitCode = 1;
162
- run.shutdown("SIGTERM");
163
- });
164
- }