node-plume 1.0.1
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/dist/cli/index.mjs +639 -0
- package/dist/index.d.ts +918 -0
- package/dist/index.mjs +3749 -0
- package/dist/start/app.mjs +3087 -0
- package/dist/start/chunk-EOB3JR5Y.mjs +239 -0
- package/dist/start/chunk-FSUAIZUX.mjs +244 -0
- package/dist/start/chunk-T7M5RXO7.mjs +239 -0
- package/dist/start/index.mjs +8 -0
- package/package.json +54 -0
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
2
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
3
|
+
}) : x)(function(x) {
|
|
4
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
5
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
// src/start/index.ts
|
|
9
|
+
import { fork } from "child_process";
|
|
10
|
+
import { existsSync } from "fs";
|
|
11
|
+
import { join, dirname } from "path";
|
|
12
|
+
import { fileURLToPath } from "url";
|
|
13
|
+
import { logger } from "@plume/logger";
|
|
14
|
+
var ProcessLauncher = class _ProcessLauncher {
|
|
15
|
+
process = null;
|
|
16
|
+
state = "idle";
|
|
17
|
+
stats = {
|
|
18
|
+
startTime: 0,
|
|
19
|
+
restartCount: 0,
|
|
20
|
+
lastRestartTime: 0
|
|
21
|
+
};
|
|
22
|
+
eventHandlers = /* @__PURE__ */ new Map();
|
|
23
|
+
options;
|
|
24
|
+
shutdownPromise = null;
|
|
25
|
+
resolveShutdown = null;
|
|
26
|
+
static DEFAULT_OPTIONS = {
|
|
27
|
+
entry: "",
|
|
28
|
+
cwd: process.cwd(),
|
|
29
|
+
watch: false,
|
|
30
|
+
env: {}
|
|
31
|
+
};
|
|
32
|
+
constructor(options = {}) {
|
|
33
|
+
this.options = { ..._ProcessLauncher.DEFAULT_OPTIONS, ...options };
|
|
34
|
+
this.setupSignalHandlers();
|
|
35
|
+
}
|
|
36
|
+
setupSignalHandlers() {
|
|
37
|
+
const signals = ["SIGINT", "SIGTERM", "SIGUSR2"];
|
|
38
|
+
signals.forEach((signal) => {
|
|
39
|
+
process.on(signal, () => {
|
|
40
|
+
logger.info(`\u6536\u5230\u4FE1\u53F7 ${signal}\uFF0C\u6B63\u5728\u5173\u95ED...`);
|
|
41
|
+
this.stop();
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
process.on("exit", () => {
|
|
45
|
+
this.kill();
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
async start() {
|
|
49
|
+
if (this.state === "running" && this.process) {
|
|
50
|
+
logger.warn("\u8FDB\u7A0B\u5DF2\u5728\u8FD0\u884C\u4E2D");
|
|
51
|
+
return this.process;
|
|
52
|
+
}
|
|
53
|
+
this.state = "starting";
|
|
54
|
+
const entry = this.resolveEntry();
|
|
55
|
+
if (!entry) {
|
|
56
|
+
const error = new Error("\u672A\u627E\u5230\u6709\u6548\u7684\u5165\u53E3\u6587\u4EF6");
|
|
57
|
+
this.emit("error", error);
|
|
58
|
+
throw error;
|
|
59
|
+
}
|
|
60
|
+
logger.info(`\u6B63\u5728\u542F\u52A8\u5E94\u7528 | \u5165\u53E3: ${entry}`);
|
|
61
|
+
this.process = fork(entry, [], {
|
|
62
|
+
cwd: this.options.cwd,
|
|
63
|
+
env: { ...process.env, ...this.options.env },
|
|
64
|
+
stdio: "inherit"
|
|
65
|
+
});
|
|
66
|
+
this.stats.startTime = Date.now();
|
|
67
|
+
this.stats.lastRestartTime = this.stats.startTime;
|
|
68
|
+
this.state = "running";
|
|
69
|
+
this.setupProcessEventHandlers();
|
|
70
|
+
this.emit("start", { pid: this.process.pid, entry });
|
|
71
|
+
logger.info(`\u5E94\u7528\u5DF2\u542F\u52A8 | PID: ${this.process.pid}`);
|
|
72
|
+
return this.process;
|
|
73
|
+
}
|
|
74
|
+
async restart() {
|
|
75
|
+
if (this.state !== "running") {
|
|
76
|
+
logger.warn("\u8FDB\u7A0B\u672A\u8FD0\u884C\uFF0C\u76F4\u63A5\u542F\u52A8");
|
|
77
|
+
return this.start();
|
|
78
|
+
}
|
|
79
|
+
logger.info("\u6B63\u5728\u91CD\u542F\u5E94\u7528...");
|
|
80
|
+
this.stats.restartCount++;
|
|
81
|
+
this.stats.lastRestartTime = Date.now();
|
|
82
|
+
await this.stop();
|
|
83
|
+
this.state = "idle";
|
|
84
|
+
const newProcess = await this.start();
|
|
85
|
+
this.emit("restart", {
|
|
86
|
+
pid: newProcess.pid,
|
|
87
|
+
restartCount: this.stats.restartCount
|
|
88
|
+
});
|
|
89
|
+
return newProcess;
|
|
90
|
+
}
|
|
91
|
+
async stop() {
|
|
92
|
+
if (this.state !== "running" || !this.process) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
this.state = "stopping";
|
|
96
|
+
logger.info("\u6B63\u5728\u505C\u6B62\u5E94\u7528...");
|
|
97
|
+
this.shutdownPromise = new Promise((resolve) => {
|
|
98
|
+
this.resolveShutdown = resolve;
|
|
99
|
+
});
|
|
100
|
+
this.process.kill("SIGTERM");
|
|
101
|
+
const timeout = setTimeout(() => {
|
|
102
|
+
if (this.process && this.process.pid) {
|
|
103
|
+
logger.warn("\u8FDB\u7A0B\u672A\u5728\u8D85\u65F6\u65F6\u95F4\u5185\u9000\u51FA\uFF0C\u5F3A\u5236\u7EC8\u6B62");
|
|
104
|
+
this.process.kill("SIGKILL");
|
|
105
|
+
}
|
|
106
|
+
}, 1e4);
|
|
107
|
+
await this.shutdownPromise;
|
|
108
|
+
clearTimeout(timeout);
|
|
109
|
+
this.state = "stopped";
|
|
110
|
+
this.emit("stop");
|
|
111
|
+
logger.info("\u5E94\u7528\u5DF2\u505C\u6B62");
|
|
112
|
+
}
|
|
113
|
+
kill() {
|
|
114
|
+
if (this.process && this.process.pid) {
|
|
115
|
+
try {
|
|
116
|
+
process.kill(this.process.pid, "SIGKILL");
|
|
117
|
+
} catch {
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
this.process = null;
|
|
121
|
+
this.state = "stopped";
|
|
122
|
+
}
|
|
123
|
+
getState() {
|
|
124
|
+
return this.state;
|
|
125
|
+
}
|
|
126
|
+
getStats() {
|
|
127
|
+
return { ...this.stats };
|
|
128
|
+
}
|
|
129
|
+
getUptime() {
|
|
130
|
+
if (this.state !== "running") return 0;
|
|
131
|
+
return Date.now() - this.stats.startTime;
|
|
132
|
+
}
|
|
133
|
+
on(event, handler) {
|
|
134
|
+
if (!this.eventHandlers.has(event)) {
|
|
135
|
+
this.eventHandlers.set(event, /* @__PURE__ */ new Set());
|
|
136
|
+
}
|
|
137
|
+
this.eventHandlers.get(event).add(handler);
|
|
138
|
+
return this;
|
|
139
|
+
}
|
|
140
|
+
off(event, handler) {
|
|
141
|
+
this.eventHandlers.get(event)?.delete(handler);
|
|
142
|
+
return this;
|
|
143
|
+
}
|
|
144
|
+
emit(event, data) {
|
|
145
|
+
this.eventHandlers.get(event)?.forEach((handler) => {
|
|
146
|
+
try {
|
|
147
|
+
handler(event, data);
|
|
148
|
+
} catch (error) {
|
|
149
|
+
logger.error(`\u4E8B\u4EF6\u5904\u7406\u5668\u9519\u8BEF [${event}]:`, error);
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
setupProcessEventHandlers() {
|
|
154
|
+
if (!this.process) return;
|
|
155
|
+
this.process.on("exit", (code, signal) => {
|
|
156
|
+
this.handleProcessExit(code, signal);
|
|
157
|
+
});
|
|
158
|
+
this.process.on("error", (error) => {
|
|
159
|
+
this.handleProcessError(error);
|
|
160
|
+
});
|
|
161
|
+
this.process.on("message", (message) => {
|
|
162
|
+
this.handleProcessMessage(message);
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
handleProcessExit(code, signal) {
|
|
166
|
+
const uptime = this.getUptime();
|
|
167
|
+
if (signal) {
|
|
168
|
+
logger.info(`\u8FDB\u7A0B\u9000\u51FA | \u4FE1\u53F7: ${signal} | \u8FD0\u884C\u65F6\u95F4: ${uptime}ms`);
|
|
169
|
+
} else {
|
|
170
|
+
logger.info(`\u8FDB\u7A0B\u9000\u51FA | \u9000\u51FA\u7801: ${code ?? "\u672A\u77E5"} | \u8FD0\u884C\u65F6\u95F4: ${uptime}ms`);
|
|
171
|
+
}
|
|
172
|
+
this.emit("exit", { code, signal, uptime });
|
|
173
|
+
if (this.resolveShutdown) {
|
|
174
|
+
this.resolveShutdown();
|
|
175
|
+
this.shutdownPromise = null;
|
|
176
|
+
this.resolveShutdown = null;
|
|
177
|
+
}
|
|
178
|
+
this.process = null;
|
|
179
|
+
this.state = "stopped";
|
|
180
|
+
}
|
|
181
|
+
handleProcessError(error) {
|
|
182
|
+
logger.error("\u8FDB\u7A0B\u9519\u8BEF:", error);
|
|
183
|
+
this.emit("error", error);
|
|
184
|
+
}
|
|
185
|
+
handleProcessMessage(message) {
|
|
186
|
+
if (typeof message === "object" && message !== null) {
|
|
187
|
+
const msg = message;
|
|
188
|
+
if (msg.type === "plume:restart") {
|
|
189
|
+
this.restart().catch((err) => logger.error("\u91CD\u542F\u5931\u8D25:", err));
|
|
190
|
+
} else if (msg.type === "plume:stop") {
|
|
191
|
+
this.stop().catch((err) => logger.error("\u505C\u6B62\u5931\u8D25:", err));
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
resolveEntry() {
|
|
196
|
+
if (this.options.entry && existsSync(this.options.entry)) {
|
|
197
|
+
return this.options.entry;
|
|
198
|
+
}
|
|
199
|
+
const cwd = this.options.cwd;
|
|
200
|
+
const currentDir = dirname(fileURLToPath(import.meta.url));
|
|
201
|
+
const appEntry = join(currentDir, "app.mjs");
|
|
202
|
+
if (existsSync(appEntry)) {
|
|
203
|
+
return appEntry;
|
|
204
|
+
}
|
|
205
|
+
const possibleEntries = [
|
|
206
|
+
join(cwd, "dist", "main.js"),
|
|
207
|
+
join(cwd, "dist", "index.js"),
|
|
208
|
+
join(cwd, "index.js"),
|
|
209
|
+
join(cwd, "src", "main.ts"),
|
|
210
|
+
join(cwd, "src", "index.ts"),
|
|
211
|
+
join(cwd, "index.ts")
|
|
212
|
+
];
|
|
213
|
+
for (const entry of possibleEntries) {
|
|
214
|
+
if (existsSync(entry)) {
|
|
215
|
+
return entry;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return null;
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
async function launch(options) {
|
|
222
|
+
const launcher = new ProcessLauncher(options);
|
|
223
|
+
await launcher.start();
|
|
224
|
+
return launcher;
|
|
225
|
+
}
|
|
226
|
+
var currentFile = fileURLToPath(import.meta.url);
|
|
227
|
+
var entryFile = process.argv[1] ? fileURLToPath(`file:///${process.argv[1].replace(/\\/g, "/")}`) : "";
|
|
228
|
+
if (currentFile === entryFile || process.argv[1]?.includes("start/index.mjs")) {
|
|
229
|
+
launch().catch((error) => {
|
|
230
|
+
logger.error("\u542F\u52A8\u5931\u8D25:", error);
|
|
231
|
+
process.exit(1);
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export {
|
|
236
|
+
__require,
|
|
237
|
+
ProcessLauncher,
|
|
238
|
+
launch
|
|
239
|
+
};
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
2
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
3
|
+
}) : x)(function(x) {
|
|
4
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
5
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
// src/start/index.ts
|
|
9
|
+
import { fork } from "child_process";
|
|
10
|
+
import { existsSync } from "fs";
|
|
11
|
+
import { join, dirname } from "path";
|
|
12
|
+
import { fileURLToPath } from "url";
|
|
13
|
+
import { logger } from "@plume/logger";
|
|
14
|
+
var ProcessLauncher = class _ProcessLauncher {
|
|
15
|
+
process = null;
|
|
16
|
+
state = "idle";
|
|
17
|
+
stats = {
|
|
18
|
+
startTime: 0,
|
|
19
|
+
restartCount: 0,
|
|
20
|
+
lastRestartTime: 0
|
|
21
|
+
};
|
|
22
|
+
eventHandlers = /* @__PURE__ */ new Map();
|
|
23
|
+
options;
|
|
24
|
+
shutdownPromise = null;
|
|
25
|
+
resolveShutdown = null;
|
|
26
|
+
static DEFAULT_OPTIONS = {
|
|
27
|
+
entry: "",
|
|
28
|
+
cwd: process.cwd(),
|
|
29
|
+
watch: false,
|
|
30
|
+
env: {}
|
|
31
|
+
};
|
|
32
|
+
constructor(options = {}) {
|
|
33
|
+
this.options = { ..._ProcessLauncher.DEFAULT_OPTIONS, ...options };
|
|
34
|
+
this.setupSignalHandlers();
|
|
35
|
+
}
|
|
36
|
+
setupSignalHandlers() {
|
|
37
|
+
const signals = ["SIGINT", "SIGTERM", "SIGUSR2"];
|
|
38
|
+
signals.forEach((signal) => {
|
|
39
|
+
process.on(signal, () => {
|
|
40
|
+
logger.info(`\u6536\u5230\u4FE1\u53F7 ${signal}\uFF0C\u6B63\u5728\u5173\u95ED...`);
|
|
41
|
+
this.stop();
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
process.on("exit", () => {
|
|
45
|
+
this.kill();
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
async start() {
|
|
49
|
+
if (this.state === "running" && this.process) {
|
|
50
|
+
logger.warn("\u8FDB\u7A0B\u5DF2\u5728\u8FD0\u884C\u4E2D");
|
|
51
|
+
return this.process;
|
|
52
|
+
}
|
|
53
|
+
this.state = "starting";
|
|
54
|
+
const entry = this.resolveEntry();
|
|
55
|
+
if (!entry) {
|
|
56
|
+
const error = new Error("\u672A\u627E\u5230\u6709\u6548\u7684\u5165\u53E3\u6587\u4EF6");
|
|
57
|
+
this.emit("error", error);
|
|
58
|
+
throw error;
|
|
59
|
+
}
|
|
60
|
+
logger.info("============================================================");
|
|
61
|
+
logger.info("Plume Process Launcher");
|
|
62
|
+
logger.info(`\u5DE5\u4F5C\u76EE\u5F55 | ${this.options.cwd}`);
|
|
63
|
+
logger.info(`\u8FD0\u884C\u6A21\u5F0F | ${this.options.watch ? "watch" : "foreground"}`);
|
|
64
|
+
logger.info(`\u6B63\u5728\u542F\u52A8\u5E94\u7528 | \u5165\u53E3: ${entry}`);
|
|
65
|
+
this.process = fork(entry, [], {
|
|
66
|
+
cwd: this.options.cwd,
|
|
67
|
+
env: { ...process.env, ...this.options.env },
|
|
68
|
+
stdio: "inherit"
|
|
69
|
+
});
|
|
70
|
+
this.stats.startTime = Date.now();
|
|
71
|
+
this.stats.lastRestartTime = this.stats.startTime;
|
|
72
|
+
this.state = "running";
|
|
73
|
+
this.setupProcessEventHandlers();
|
|
74
|
+
this.emit("start", { pid: this.process.pid, entry });
|
|
75
|
+
logger.info(`\u5E94\u7528\u5DF2\u542F\u52A8 | PID: ${this.process.pid}`);
|
|
76
|
+
logger.info("============================================================");
|
|
77
|
+
return this.process;
|
|
78
|
+
}
|
|
79
|
+
async restart() {
|
|
80
|
+
if (this.state !== "running") {
|
|
81
|
+
logger.warn("\u8FDB\u7A0B\u672A\u8FD0\u884C\uFF0C\u76F4\u63A5\u542F\u52A8");
|
|
82
|
+
return this.start();
|
|
83
|
+
}
|
|
84
|
+
logger.info("\u6B63\u5728\u91CD\u542F\u5E94\u7528...");
|
|
85
|
+
this.stats.restartCount++;
|
|
86
|
+
this.stats.lastRestartTime = Date.now();
|
|
87
|
+
await this.stop();
|
|
88
|
+
this.state = "idle";
|
|
89
|
+
const newProcess = await this.start();
|
|
90
|
+
this.emit("restart", {
|
|
91
|
+
pid: newProcess.pid,
|
|
92
|
+
restartCount: this.stats.restartCount
|
|
93
|
+
});
|
|
94
|
+
return newProcess;
|
|
95
|
+
}
|
|
96
|
+
async stop() {
|
|
97
|
+
if (this.state !== "running" || !this.process) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
this.state = "stopping";
|
|
101
|
+
logger.info("\u6B63\u5728\u505C\u6B62\u5E94\u7528...");
|
|
102
|
+
this.shutdownPromise = new Promise((resolve) => {
|
|
103
|
+
this.resolveShutdown = resolve;
|
|
104
|
+
});
|
|
105
|
+
this.process.kill("SIGTERM");
|
|
106
|
+
const timeout = setTimeout(() => {
|
|
107
|
+
if (this.process && this.process.pid) {
|
|
108
|
+
logger.warn("\u8FDB\u7A0B\u672A\u5728\u8D85\u65F6\u65F6\u95F4\u5185\u9000\u51FA\uFF0C\u5F3A\u5236\u7EC8\u6B62");
|
|
109
|
+
this.process.kill("SIGKILL");
|
|
110
|
+
}
|
|
111
|
+
}, 1e4);
|
|
112
|
+
await this.shutdownPromise;
|
|
113
|
+
clearTimeout(timeout);
|
|
114
|
+
this.state = "stopped";
|
|
115
|
+
this.emit("stop");
|
|
116
|
+
logger.info("\u5E94\u7528\u5DF2\u505C\u6B62");
|
|
117
|
+
}
|
|
118
|
+
kill() {
|
|
119
|
+
if (this.process && this.process.pid) {
|
|
120
|
+
try {
|
|
121
|
+
process.kill(this.process.pid, "SIGKILL");
|
|
122
|
+
} catch {
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
this.process = null;
|
|
126
|
+
this.state = "stopped";
|
|
127
|
+
}
|
|
128
|
+
getState() {
|
|
129
|
+
return this.state;
|
|
130
|
+
}
|
|
131
|
+
getStats() {
|
|
132
|
+
return { ...this.stats };
|
|
133
|
+
}
|
|
134
|
+
getUptime() {
|
|
135
|
+
if (this.state !== "running") return 0;
|
|
136
|
+
return Date.now() - this.stats.startTime;
|
|
137
|
+
}
|
|
138
|
+
on(event, handler) {
|
|
139
|
+
if (!this.eventHandlers.has(event)) {
|
|
140
|
+
this.eventHandlers.set(event, /* @__PURE__ */ new Set());
|
|
141
|
+
}
|
|
142
|
+
this.eventHandlers.get(event).add(handler);
|
|
143
|
+
return this;
|
|
144
|
+
}
|
|
145
|
+
off(event, handler) {
|
|
146
|
+
this.eventHandlers.get(event)?.delete(handler);
|
|
147
|
+
return this;
|
|
148
|
+
}
|
|
149
|
+
emit(event, data) {
|
|
150
|
+
this.eventHandlers.get(event)?.forEach((handler) => {
|
|
151
|
+
try {
|
|
152
|
+
handler(event, data);
|
|
153
|
+
} catch (error) {
|
|
154
|
+
logger.error(`\u4E8B\u4EF6\u5904\u7406\u5668\u9519\u8BEF [${event}]:`, error);
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
setupProcessEventHandlers() {
|
|
159
|
+
if (!this.process) return;
|
|
160
|
+
this.process.on("exit", (code, signal) => {
|
|
161
|
+
this.handleProcessExit(code, signal);
|
|
162
|
+
});
|
|
163
|
+
this.process.on("error", (error) => {
|
|
164
|
+
this.handleProcessError(error);
|
|
165
|
+
});
|
|
166
|
+
this.process.on("message", (message) => {
|
|
167
|
+
this.handleProcessMessage(message);
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
handleProcessExit(code, signal) {
|
|
171
|
+
const uptime = this.getUptime();
|
|
172
|
+
if (signal) {
|
|
173
|
+
logger.info(`\u8FDB\u7A0B\u9000\u51FA | \u4FE1\u53F7: ${signal} | \u8FD0\u884C\u65F6\u95F4: ${uptime}ms`);
|
|
174
|
+
} else {
|
|
175
|
+
logger.info(`\u8FDB\u7A0B\u9000\u51FA | \u9000\u51FA\u7801: ${code ?? "\u672A\u77E5"} | \u8FD0\u884C\u65F6\u95F4: ${uptime}ms`);
|
|
176
|
+
}
|
|
177
|
+
this.emit("exit", { code, signal, uptime });
|
|
178
|
+
if (this.resolveShutdown) {
|
|
179
|
+
this.resolveShutdown();
|
|
180
|
+
this.shutdownPromise = null;
|
|
181
|
+
this.resolveShutdown = null;
|
|
182
|
+
}
|
|
183
|
+
this.process = null;
|
|
184
|
+
this.state = "stopped";
|
|
185
|
+
}
|
|
186
|
+
handleProcessError(error) {
|
|
187
|
+
logger.error("\u8FDB\u7A0B\u9519\u8BEF:", error);
|
|
188
|
+
this.emit("error", error);
|
|
189
|
+
}
|
|
190
|
+
handleProcessMessage(message) {
|
|
191
|
+
if (typeof message === "object" && message !== null) {
|
|
192
|
+
const msg = message;
|
|
193
|
+
if (msg.type === "plume:restart") {
|
|
194
|
+
this.restart().catch((err) => logger.error("\u91CD\u542F\u5931\u8D25:", err));
|
|
195
|
+
} else if (msg.type === "plume:stop") {
|
|
196
|
+
this.stop().catch((err) => logger.error("\u505C\u6B62\u5931\u8D25:", err));
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
resolveEntry() {
|
|
201
|
+
if (this.options.entry && existsSync(this.options.entry)) {
|
|
202
|
+
return this.options.entry;
|
|
203
|
+
}
|
|
204
|
+
const cwd = this.options.cwd;
|
|
205
|
+
const currentDir = dirname(fileURLToPath(import.meta.url));
|
|
206
|
+
const appEntry = join(currentDir, "app.mjs");
|
|
207
|
+
if (existsSync(appEntry)) {
|
|
208
|
+
return appEntry;
|
|
209
|
+
}
|
|
210
|
+
const possibleEntries = [
|
|
211
|
+
join(cwd, "dist", "main.js"),
|
|
212
|
+
join(cwd, "dist", "index.js"),
|
|
213
|
+
join(cwd, "index.js"),
|
|
214
|
+
join(cwd, "src", "main.ts"),
|
|
215
|
+
join(cwd, "src", "index.ts"),
|
|
216
|
+
join(cwd, "index.ts")
|
|
217
|
+
];
|
|
218
|
+
for (const entry of possibleEntries) {
|
|
219
|
+
if (existsSync(entry)) {
|
|
220
|
+
return entry;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
return null;
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
async function launch(options) {
|
|
227
|
+
const launcher = new ProcessLauncher(options);
|
|
228
|
+
await launcher.start();
|
|
229
|
+
return launcher;
|
|
230
|
+
}
|
|
231
|
+
var currentFile = fileURLToPath(import.meta.url);
|
|
232
|
+
var entryFile = process.argv[1] ? fileURLToPath(`file:///${process.argv[1].replace(/\\/g, "/")}`) : "";
|
|
233
|
+
if (currentFile === entryFile || process.argv[1]?.includes("start/index.mjs")) {
|
|
234
|
+
launch().catch((error) => {
|
|
235
|
+
logger.error("\u542F\u52A8\u5931\u8D25:", error);
|
|
236
|
+
process.exit(1);
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
export {
|
|
241
|
+
__require,
|
|
242
|
+
ProcessLauncher,
|
|
243
|
+
launch
|
|
244
|
+
};
|