larness 0.4.1 → 0.5.0
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/README.md +6 -5
- package/app.js +22 -105
- package/apps/server/dist/{address-BDs0FfMq.js → address-D0ZG5qR4.js} +5 -2
- package/apps/server/dist/args-CrKTPT06.js +92 -0
- package/apps/server/dist/cli.js +340 -0
- package/apps/server/dist/{client-D88ULjn9.js → client-D_Ni3Zpx.js} +21 -1
- package/apps/server/dist/code-B2JBoV4x.js +24 -0
- package/apps/server/dist/{doctor-dwTQiTef.js → doctor-CE6cONuz.js} +3 -2
- package/apps/server/dist/doctor-sPRlI92N.js +1 -0
- package/apps/server/dist/doctor.js +1 -1
- package/apps/server/dist/index.js +704 -118
- package/apps/server/dist/{paths-DUfwiH2X.js → inspect-XgAc84F9.js} +106 -87
- package/apps/server/dist/lifecycle-C3KV3-Ax.js +263 -0
- package/apps/server/dist/log-files-CFOaEy6O.js +26 -0
- package/apps/server/dist/{managed-Br7VqQiP.js → managed-Cw3MVilO.js} +221 -71
- package/apps/server/dist/paths-BhVn34MT.js +77 -0
- package/apps/server/dist/{status-D2oSIMoj.js → status-CV_zWfs6.js} +16 -3
- package/apps/server/dist/status.js +37 -9
- package/apps/server/dist/supervisor-IYDKL4iU.js +297 -0
- package/apps/server/dist/tui.js +167 -333
- package/apps/server/dist/version-ui_X8H2k.js +13 -0
- package/apps/server/drizzle/0001_auth.sql +109 -109
- package/apps/server/drizzle/0002_projects.sql +12 -12
- package/apps/server/drizzle/0003_workspace.sql +80 -80
- package/apps/server/drizzle/0004_models.sql +30 -30
- package/apps/server/drizzle/0005_resources.sql +54 -54
- package/apps/server/drizzle/0006_commands.sql +23 -23
- package/apps/server/drizzle/0007_terminals.sql +41 -41
- package/apps/server/drizzle/0008_resource_actions.sql +23 -23
- package/apps/server/drizzle/0010_services.sql +31 -31
- package/apps/web/dist/assets/{FileEditor-CxzD-z_w.js → FileEditor-DnQa6Owc.js} +1 -1
- package/apps/web/dist/assets/{FileViewer-4LwgwN4s.js → FileViewer-DKlF-CWF.js} +1 -1
- package/apps/web/dist/assets/{cssMode-BfW9rKao.js → cssMode-C9KmpsHc.js} +1 -1
- package/apps/web/dist/assets/{editor.api-D0PbFQmb.js → editor.api-BwfHyHQ6.js} +1 -1
- package/apps/web/dist/assets/{freemarker2-CUmmnP_9.js → freemarker2-D41RlIQ5.js} +1 -1
- package/apps/web/dist/assets/{handlebars-CV0ntFU6.js → handlebars-D-7R6ic0.js} +1 -1
- package/apps/web/dist/assets/{html-lbv15ZCZ.js → html-CUME4rt-.js} +1 -1
- package/apps/web/dist/assets/{htmlMode-2S1Ml4u-.js → htmlMode-8w7WLhnL.js} +1 -1
- package/apps/web/dist/assets/{index-Bu4wSG15.js → index-BOS3UrP1.js} +2 -2
- package/apps/web/dist/assets/{javascript-s1wW-h_2.js → javascript-_3pdDG34.js} +1 -1
- package/apps/web/dist/assets/{jsonMode-Di8Ytqtu.js → jsonMode-DiqocFCf.js} +1 -1
- package/apps/web/dist/assets/{liquid-CR28rx5y.js → liquid-j0FoiTXN.js} +1 -1
- package/apps/web/dist/assets/{lspLanguageFeatures-uPJRSuKu.js → lspLanguageFeatures-Zj7K2sG4.js} +1 -1
- package/apps/web/dist/assets/main-DZIruO5x.js +157 -0
- package/apps/web/dist/assets/main-gq3n_vn8.css +1 -0
- package/apps/web/dist/assets/{mdx-B_RQg_ko.js → mdx-CGQ8Cw6J.js} +1 -1
- package/apps/web/dist/assets/{monaco-BHRE2AKp.js → monaco-CyMA3azg.js} +3 -3
- package/apps/web/dist/assets/{python-DxrD-zEd.js → python-BtY34Z9A.js} +1 -1
- package/apps/web/dist/assets/{razor-BGSx-heg.js → razor-CzYHpLv5.js} +1 -1
- package/apps/web/dist/assets/{tsMode-CkPGnA4g.js → tsMode-GZx3VEv9.js} +1 -1
- package/apps/web/dist/assets/{typescript-oJa8RtP0.js → typescript-CAfvuTDQ.js} +1 -1
- package/apps/web/dist/assets/{workers-BmwP04Ml.js → workers-C5BOxOmH.js} +1 -1
- package/apps/web/dist/assets/{xml-NoacuPD-.js → xml-BGX4mtH7.js} +1 -1
- package/apps/web/dist/assets/{yaml-PU4CKcMI.js → yaml-B2AgBtJB.js} +1 -1
- package/apps/web/dist/favicon.svg +4 -4
- package/apps/web/dist/index.html +1 -1
- package/docs/README.md +8 -1
- package/docs/guides/install.md +23 -7
- package/package.json +3 -1
- package/apps/server/dist/lock-DyEoZgG_.js +0 -101
- package/apps/web/dist/assets/main-KzEpvsAJ.js +0 -157
- package/apps/web/dist/assets/main-ptChOROf.css +0 -1
package/apps/server/dist/tui.js
CHANGED
|
@@ -1,24 +1,22 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
1
|
+
import { i as inspectInstanceLock, jn as deriveWebAuthn, kn as DEFAULT_APP_CONFIG } from "./inspect-XgAc84F9.js";
|
|
2
|
+
import { c as EntryConfigError, s as DevWebUnavailable } from "./managed-Cw3MVilO.js";
|
|
3
|
+
import { n as connectIpc, t as IpcClientError } from "./client-D_Ni3Zpx.js";
|
|
4
|
+
import { r as resolveDataDir } from "./paths-BhVn34MT.js";
|
|
5
|
+
import { t as SERVER_VERSION } from "./version-ui_X8H2k.js";
|
|
6
|
+
import { s as InitApplyParamsSchema } from "./address-D0ZG5qR4.js";
|
|
7
|
+
import { i as formatPairingCode } from "./code-B2JBoV4x.js";
|
|
8
|
+
import { n as startManagedRuntime } from "./supervisor-IYDKL4iU.js";
|
|
8
9
|
import path from "node:path";
|
|
9
|
-
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
10
10
|
import { z } from "zod";
|
|
11
|
-
import { spawn } from "node:child_process";
|
|
12
11
|
import { stripVTControlCharacters } from "node:util";
|
|
13
|
-
import { chmod, mkdir, readFile,
|
|
12
|
+
import { chmod, mkdir, readFile, writeFile } from "node:fs/promises";
|
|
14
13
|
import { PassThrough, Writable } from "node:stream";
|
|
15
|
-
import { setTimeout
|
|
14
|
+
import { setTimeout } from "node:timers/promises";
|
|
16
15
|
import semver from "semver";
|
|
17
16
|
import { confirm, input } from "@inquirer/prompts";
|
|
18
17
|
import { emitKeypressEvents } from "node:readline";
|
|
19
18
|
import stringWidth from "fast-string-width";
|
|
20
19
|
import { wrapAnsi } from "fast-wrap-ansi";
|
|
21
|
-
import pc from "picocolors";
|
|
22
20
|
// src/modules/updates/npm.ts
|
|
23
21
|
const CHECK_INTERVAL_MS = 864e5;
|
|
24
22
|
const DEFAULT_REGISTRY = "https://registry.npmjs.org";
|
|
@@ -484,6 +482,22 @@ async function runManagedSessions(options) {
|
|
|
484
482
|
if (action !== "restart") return;
|
|
485
483
|
}
|
|
486
484
|
}
|
|
485
|
+
// src/tui/page-error.ts
|
|
486
|
+
function displayError(error) {
|
|
487
|
+
if (error instanceof EntryConfigError) return error.message;
|
|
488
|
+
if (error instanceof DevWebUnavailable) return `${error.code}:${error.message}`;
|
|
489
|
+
if (error instanceof IpcClientError) return `${error.code}:操作未完成,请刷新查看实际状态`;
|
|
490
|
+
return "无法读取或校验数据,请刷新重试";
|
|
491
|
+
}
|
|
492
|
+
function disconnected(error) {
|
|
493
|
+
return error instanceof IpcClientError && error.code === "CONNECTION_CLOSED";
|
|
494
|
+
}
|
|
495
|
+
var ServiceStopped = class extends Error {
|
|
496
|
+
constructor() {
|
|
497
|
+
super("service stopped");
|
|
498
|
+
this.name = "ServiceStopped";
|
|
499
|
+
}
|
|
500
|
+
};
|
|
487
501
|
// src/tui/schemas.ts
|
|
488
502
|
const StatusSchema = z.object({
|
|
489
503
|
version: z.string(),
|
|
@@ -553,16 +567,6 @@ const RevokeSchema = z.object({
|
|
|
553
567
|
revokedSessions: z.number()
|
|
554
568
|
});
|
|
555
569
|
const OkSchema = z.object({ ok: z.literal(true) });
|
|
556
|
-
// src/tui/page-error.ts
|
|
557
|
-
function displayError(error) {
|
|
558
|
-
if (error instanceof EntryConfigError) return error.message;
|
|
559
|
-
if (error instanceof DevWebUnavailable) return `${error.code}:${error.message}`;
|
|
560
|
-
if (error instanceof IpcClientError) return `${error.code}:操作未完成,请刷新查看实际状态`;
|
|
561
|
-
return "无法读取或校验数据,请刷新重试";
|
|
562
|
-
}
|
|
563
|
-
function disconnected(error) {
|
|
564
|
-
return error instanceof IpcClientError && error.code === "CONNECTION_CLOSED";
|
|
565
|
-
}
|
|
566
570
|
// src/tui/reading-page.ts
|
|
567
571
|
async function runReadingPage(kind, deps) {
|
|
568
572
|
const { terminal, client } = deps;
|
|
@@ -1006,13 +1010,22 @@ function runResourcePage(kind, deps) {
|
|
|
1006
1010
|
// src/tui/screen-dashboard.ts
|
|
1007
1011
|
async function runScreenDashboard(deps) {
|
|
1008
1012
|
const { terminal, client } = deps;
|
|
1009
|
-
const
|
|
1013
|
+
const attached = deps.attached === true;
|
|
1014
|
+
const menu = attached ? [
|
|
1015
|
+
"配对",
|
|
1016
|
+
"设备",
|
|
1017
|
+
"日志",
|
|
1018
|
+
"审计",
|
|
1019
|
+
"停止服务",
|
|
1020
|
+
"退出面板"
|
|
1021
|
+
] : [
|
|
1010
1022
|
"配对",
|
|
1011
1023
|
"设备",
|
|
1012
1024
|
"日志",
|
|
1013
1025
|
"审计",
|
|
1014
1026
|
"退出并停止服务"
|
|
1015
1027
|
];
|
|
1028
|
+
const exitIndex = menu.length - 1;
|
|
1016
1029
|
let selected = 0;
|
|
1017
1030
|
let page = "overview";
|
|
1018
1031
|
let status = null;
|
|
@@ -1022,18 +1035,38 @@ async function runScreenDashboard(deps) {
|
|
|
1022
1035
|
let generation = 0;
|
|
1023
1036
|
let loading = false;
|
|
1024
1037
|
let confirmExit = false;
|
|
1038
|
+
let confirmStop = false;
|
|
1025
1039
|
const render = () => {
|
|
1026
1040
|
if (page === "child") return;
|
|
1027
1041
|
if (page === "exit") {
|
|
1028
1042
|
terminal.render({
|
|
1029
1043
|
title: "退出确认",
|
|
1030
|
-
body: [
|
|
1044
|
+
body: attached ? [
|
|
1045
|
+
"退出面板不会停止服务,在途任务会继续运行。",
|
|
1046
|
+
"",
|
|
1047
|
+
`${confirmExit ? " " : "› "}取消`,
|
|
1048
|
+
`${confirmExit ? "› " : " "}退出面板`
|
|
1049
|
+
] : [
|
|
1031
1050
|
"退出将停止 Server 与 Web。",
|
|
1032
1051
|
"",
|
|
1033
1052
|
`${confirmExit ? " " : "› "}取消`,
|
|
1034
1053
|
`${confirmExit ? "› " : " "}确认退出并停止服务`
|
|
1035
1054
|
],
|
|
1036
|
-
footer: "↑↓ 选择 · Enter 确认 · Esc 取消 · Ctrl+C 停止服务"
|
|
1055
|
+
footer: attached ? "↑↓ 选择 · Enter 确认 · Esc 取消 · Ctrl+C 退出面板" : "↑↓ 选择 · Enter 确认 · Esc 取消 · Ctrl+C 停止服务"
|
|
1056
|
+
});
|
|
1057
|
+
return;
|
|
1058
|
+
}
|
|
1059
|
+
if (page === "stop") {
|
|
1060
|
+
terminal.render({
|
|
1061
|
+
title: "停止服务确认",
|
|
1062
|
+
body: [
|
|
1063
|
+
"停止服务会中断在途任务、终端与临时开发服务。",
|
|
1064
|
+
"PM2 正式服务与部署不会停止。",
|
|
1065
|
+
"",
|
|
1066
|
+
`${confirmStop ? " " : "› "}取消`,
|
|
1067
|
+
`${confirmStop ? "› " : " "}确认停止服务`
|
|
1068
|
+
],
|
|
1069
|
+
footer: "↑↓ 选择 · Enter 确认 · Esc 取消"
|
|
1037
1070
|
});
|
|
1038
1071
|
return;
|
|
1039
1072
|
}
|
|
@@ -1043,6 +1076,7 @@ async function runScreenDashboard(deps) {
|
|
|
1043
1076
|
status ? `状态:${status.running ? "运行中" : "已停止"} · ${status.version}` : "正在读取运行状态…",
|
|
1044
1077
|
`Web:${deps.webUrl ?? (status ? `http://localhost:${status.listen.port}` : "读取中")}`,
|
|
1045
1078
|
status ? `待配对 ${status.sessions.pendingPairings} · 活跃会话 ${status.sessions.online}` : "",
|
|
1079
|
+
...attached ? ["附加模式:退出面板后服务继续运行"] : [],
|
|
1046
1080
|
...status?.warnings.map((warning) => `! ${warning}`) ?? [],
|
|
1047
1081
|
...updateNotice ? ["", updateNotice] : [],
|
|
1048
1082
|
"",
|
|
@@ -1050,7 +1084,7 @@ async function runScreenDashboard(deps) {
|
|
|
1050
1084
|
"",
|
|
1051
1085
|
loading ? "正在刷新…" : error
|
|
1052
1086
|
],
|
|
1053
|
-
footer: "↑↓ 选择 · Enter 打开 · p/d/l/a · r 刷新 · q 退出"
|
|
1087
|
+
footer: attached ? "↑↓ 选择 · Enter 打开 · p/d/l/a · r 刷新 · q 退出面板" : "↑↓ 选择 · Enter 打开 · p/d/l/a · r 刷新 · q 退出"
|
|
1054
1088
|
});
|
|
1055
1089
|
};
|
|
1056
1090
|
const refresh = () => {
|
|
@@ -1099,6 +1133,34 @@ async function runScreenDashboard(deps) {
|
|
|
1099
1133
|
}
|
|
1100
1134
|
continue;
|
|
1101
1135
|
}
|
|
1136
|
+
if (page === "stop") {
|
|
1137
|
+
if (key.name === "escape") {
|
|
1138
|
+
page = "overview";
|
|
1139
|
+
render();
|
|
1140
|
+
} else if (key.name === "up" || key.name === "down") {
|
|
1141
|
+
confirmStop = !confirmStop;
|
|
1142
|
+
render();
|
|
1143
|
+
} else if (key.name === "return") {
|
|
1144
|
+
if (!confirmStop) {
|
|
1145
|
+
page = "overview";
|
|
1146
|
+
render();
|
|
1147
|
+
continue;
|
|
1148
|
+
}
|
|
1149
|
+
try {
|
|
1150
|
+
await client.request("server.shutdown", {
|
|
1151
|
+
confirm: true,
|
|
1152
|
+
source: "tui"
|
|
1153
|
+
}, 5e3);
|
|
1154
|
+
} catch (cause) {
|
|
1155
|
+
page = "overview";
|
|
1156
|
+
error = displayError(cause);
|
|
1157
|
+
render();
|
|
1158
|
+
continue;
|
|
1159
|
+
}
|
|
1160
|
+
throw new ServiceStopped();
|
|
1161
|
+
}
|
|
1162
|
+
continue;
|
|
1163
|
+
}
|
|
1102
1164
|
if (key.name === "up" || key.name === "down") {
|
|
1103
1165
|
selected = (selected + (key.name === "up" ? menu.length - 1 : 1)) % menu.length;
|
|
1104
1166
|
render();
|
|
@@ -1108,39 +1170,46 @@ async function runScreenDashboard(deps) {
|
|
|
1108
1170
|
refresh();
|
|
1109
1171
|
continue;
|
|
1110
1172
|
}
|
|
1111
|
-
const shortcut =
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
if (shortcut
|
|
1119
|
-
if (shortcut
|
|
1120
|
-
if (selected ===
|
|
1173
|
+
const shortcut = {
|
|
1174
|
+
p: 0,
|
|
1175
|
+
d: 1,
|
|
1176
|
+
l: 2,
|
|
1177
|
+
a: 3,
|
|
1178
|
+
q: exitIndex
|
|
1179
|
+
}[key.name];
|
|
1180
|
+
if (shortcut === void 0 && key.name !== "return") continue;
|
|
1181
|
+
if (shortcut !== void 0) selected = shortcut;
|
|
1182
|
+
if (selected === exitIndex) {
|
|
1121
1183
|
page = "exit";
|
|
1122
1184
|
confirmExit = false;
|
|
1123
1185
|
render();
|
|
1124
1186
|
continue;
|
|
1125
1187
|
}
|
|
1188
|
+
if (attached && selected === 4) {
|
|
1189
|
+
page = "stop";
|
|
1190
|
+
confirmStop = false;
|
|
1191
|
+
render();
|
|
1192
|
+
continue;
|
|
1193
|
+
}
|
|
1126
1194
|
++generation;
|
|
1127
1195
|
loading = false;
|
|
1128
1196
|
page = "child";
|
|
1129
1197
|
const lines = [];
|
|
1130
1198
|
const title = menu[selected] ?? "功能";
|
|
1199
|
+
const childFooter = attached ? "Esc 返回 · Ctrl+C 退出面板" : "Esc 返回 · Ctrl+C 停止服务";
|
|
1131
1200
|
const out = (text) => {
|
|
1132
1201
|
lines.push(text);
|
|
1133
1202
|
terminal.render({
|
|
1134
1203
|
title,
|
|
1135
1204
|
body: lines,
|
|
1136
|
-
footer:
|
|
1205
|
+
footer: childFooter
|
|
1137
1206
|
});
|
|
1138
1207
|
};
|
|
1139
1208
|
const ask = async (message) => terminal.ask(message);
|
|
1140
1209
|
terminal.render({
|
|
1141
1210
|
title,
|
|
1142
1211
|
body: ["正在读取…"],
|
|
1143
|
-
footer:
|
|
1212
|
+
footer: childFooter
|
|
1144
1213
|
});
|
|
1145
1214
|
try {
|
|
1146
1215
|
if (selected === 0 || selected === 1) await runResourcePage(selected === 0 ? "pairing" : "devices", {
|
|
@@ -1164,297 +1233,16 @@ async function runScreenDashboard(deps) {
|
|
|
1164
1233
|
++generation;
|
|
1165
1234
|
}
|
|
1166
1235
|
}
|
|
1167
|
-
// src/tui/supervisor.ts
|
|
1168
|
-
const READY_TIMEOUT_MS = 9e4;
|
|
1169
|
-
const LOG_LIMIT = 300;
|
|
1170
|
-
function isLoopbackHostname(hostname) {
|
|
1171
|
-
return hostname === "localhost" || hostname === "127.0.0.1" || hostname === "[::1]";
|
|
1172
|
-
}
|
|
1173
|
-
function originPort(origin) {
|
|
1174
|
-
const parsed = new URL(origin);
|
|
1175
|
-
return Number(parsed.port || (parsed.protocol === "https:" ? "443" : "80"));
|
|
1176
|
-
}
|
|
1177
|
-
function resolveWebUrl(options) {
|
|
1178
|
-
const localUrl = `http://localhost:${options.serverPort}`;
|
|
1179
|
-
const canonical = new URL(options.canonicalOrigin);
|
|
1180
|
-
if (canonical.protocol === "http:" && isLoopbackHostname(canonical.hostname) && originPort(options.canonicalOrigin) !== options.serverPort) throw new EntryConfigError(options.canonicalOrigin, options.serverPort);
|
|
1181
|
-
return options.publicUrl ?? localUrl;
|
|
1182
|
-
}
|
|
1183
|
-
function serverRoot() {
|
|
1184
|
-
return findServerRoot(path.dirname(fileURLToPath(import.meta.url)));
|
|
1185
|
-
}
|
|
1186
|
-
function managedCommands(options) {
|
|
1187
|
-
const nodePath = options.nodePath ?? process.execPath;
|
|
1188
|
-
if (!options.dev) return {
|
|
1189
|
-
serverCommand: nodePath,
|
|
1190
|
-
serverArgs: [path.join(options.serverRoot, "dist", "index.js")],
|
|
1191
|
-
serverCwd: options.serverRoot,
|
|
1192
|
-
webCommand: null,
|
|
1193
|
-
webArgs: [],
|
|
1194
|
-
webCwd: null
|
|
1195
|
-
};
|
|
1196
|
-
const serverRequire = createRequire(path.join(options.serverRoot, "package.json"));
|
|
1197
|
-
const tsxLoader = pathToFileURL(serverRequire.resolve("tsx/esm")).href;
|
|
1198
|
-
return {
|
|
1199
|
-
serverCommand: nodePath,
|
|
1200
|
-
serverArgs: [
|
|
1201
|
-
"--import",
|
|
1202
|
-
tsxLoader,
|
|
1203
|
-
path.join(options.serverRoot, "src", "index.ts"),
|
|
1204
|
-
"--managed-dev-web"
|
|
1205
|
-
],
|
|
1206
|
-
serverCwd: options.serverRoot,
|
|
1207
|
-
webCommand: nodePath,
|
|
1208
|
-
webArgs: [
|
|
1209
|
-
"--import",
|
|
1210
|
-
tsxLoader,
|
|
1211
|
-
path.join(options.repoRoot, "apps", "web", "scripts", "managed-vite.ts")
|
|
1212
|
-
],
|
|
1213
|
-
webCwd: path.join(options.repoRoot, "apps", "web")
|
|
1214
|
-
};
|
|
1215
|
-
}
|
|
1216
|
-
function delay(ms) {
|
|
1217
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
1218
|
-
}
|
|
1219
|
-
async function httpOk(url, requireHtml = false, host) {
|
|
1220
|
-
try {
|
|
1221
|
-
const response = await fetch(url, {
|
|
1222
|
-
signal: AbortSignal.timeout(5e3),
|
|
1223
|
-
...host ? { headers: { host } } : {}
|
|
1224
|
-
});
|
|
1225
|
-
const ready = response.ok && (!requireHtml || (response.headers.get("content-type") ?? "").includes("text/html"));
|
|
1226
|
-
await response.arrayBuffer();
|
|
1227
|
-
return ready;
|
|
1228
|
-
} catch {
|
|
1229
|
-
return false;
|
|
1230
|
-
}
|
|
1231
|
-
}
|
|
1232
|
-
async function waitForHttp(url, child, options = {}) {
|
|
1233
|
-
const deadline = Date.now() + (options.timeoutMs ?? READY_TIMEOUT_MS);
|
|
1234
|
-
while (Date.now() < deadline) {
|
|
1235
|
-
if (child.exitCode !== null || child.signalCode !== null || options.failed?.() === true || options.signal?.aborted === true) return false;
|
|
1236
|
-
if (await httpOk(url, options.requireHtml, options.host)) return true;
|
|
1237
|
-
await delay(200);
|
|
1238
|
-
}
|
|
1239
|
-
return false;
|
|
1240
|
-
}
|
|
1241
|
-
function waitForExit(child, timeoutMs) {
|
|
1242
|
-
if (child.exitCode !== null || child.signalCode !== null) return Promise.resolve(true);
|
|
1243
|
-
return new Promise((resolve) => {
|
|
1244
|
-
const timer = setTimeout(() => resolve(false), timeoutMs);
|
|
1245
|
-
child.once("exit", () => {
|
|
1246
|
-
clearTimeout(timer);
|
|
1247
|
-
resolve(true);
|
|
1248
|
-
});
|
|
1249
|
-
});
|
|
1250
|
-
}
|
|
1251
|
-
async function stopChild(child, timeoutMs, graceful = "signal") {
|
|
1252
|
-
if (child === null || child.exitCode !== null || child.signalCode !== null) return;
|
|
1253
|
-
if (process.platform === "win32" && child.pid) {
|
|
1254
|
-
if (graceful === "message" && child.connected) child.send({ type: "larness.shutdown" });
|
|
1255
|
-
else if (graceful === "disconnect" && child.connected) child.disconnect();
|
|
1256
|
-
if (await waitForExit(child, timeoutMs)) return;
|
|
1257
|
-
await terminateProcessTree(child.pid, 2e3);
|
|
1258
|
-
await waitForExit(child, 2e3);
|
|
1259
|
-
return;
|
|
1260
|
-
}
|
|
1261
|
-
child.kill("SIGTERM");
|
|
1262
|
-
if (await waitForExit(child, timeoutMs)) return;
|
|
1263
|
-
child.kill("SIGKILL");
|
|
1264
|
-
await waitForExit(child, 2e3);
|
|
1265
|
-
}
|
|
1266
|
-
async function loadManagedConfig(dataDir, env = process.env) {
|
|
1267
|
-
const pending = inspectPendingInitialization(path.join(dataDir, "larness.db"));
|
|
1268
|
-
return pending === null ? loadConfig({
|
|
1269
|
-
dataDir,
|
|
1270
|
-
env
|
|
1271
|
-
}) : {
|
|
1272
|
-
...resolveConfig(pending, env),
|
|
1273
|
-
source: "file",
|
|
1274
|
-
path: configFilePath(dataDir)
|
|
1275
|
-
};
|
|
1276
|
-
}
|
|
1277
|
-
async function startManagedRuntime(options) {
|
|
1278
|
-
options.signal?.throwIfAborted();
|
|
1279
|
-
const out = options.out ?? console.log;
|
|
1280
|
-
const root = serverRoot();
|
|
1281
|
-
const repoRoot = path.resolve(root, "../..");
|
|
1282
|
-
const dataDir = resolveDataDir();
|
|
1283
|
-
const loaded = await loadManagedConfig(dataDir);
|
|
1284
|
-
const port = loaded.config.server.port;
|
|
1285
|
-
const localBase = `http://127.0.0.1:${port}`;
|
|
1286
|
-
const serverUrl = `http://localhost:${port}`;
|
|
1287
|
-
const canonicalOrigin = loaded.webAuthn.origin;
|
|
1288
|
-
const webUrl = resolveWebUrl({
|
|
1289
|
-
dev: options.dev,
|
|
1290
|
-
serverPort: port,
|
|
1291
|
-
publicUrl: loaded.config.server.publicUrl,
|
|
1292
|
-
canonicalOrigin
|
|
1293
|
-
});
|
|
1294
|
-
if (await httpOk(`${localBase}/api/health`)) throw new Error(`已有 Larness Server 在 ${serverUrl} 运行;请先关闭原 TUI。`);
|
|
1295
|
-
const commands = managedCommands({
|
|
1296
|
-
dev: options.dev,
|
|
1297
|
-
serverRoot: root,
|
|
1298
|
-
repoRoot
|
|
1299
|
-
});
|
|
1300
|
-
const logs = [];
|
|
1301
|
-
const appendLog = (prefix, chunk) => {
|
|
1302
|
-
for (const line of chunk.toString("utf8").split(/\r?\n/)) if (line.trim()) logs.push(`${prefix} ${line}`);
|
|
1303
|
-
if (logs.length > LOG_LIMIT) logs.splice(0, logs.length - LOG_LIMIT);
|
|
1304
|
-
};
|
|
1305
|
-
let server = null;
|
|
1306
|
-
let web = null;
|
|
1307
|
-
let stopped = false;
|
|
1308
|
-
let ownedCache = null;
|
|
1309
|
-
let stopping = null;
|
|
1310
|
-
let resolveServerExit = () => void 0;
|
|
1311
|
-
const serverExited = new Promise((resolve) => {
|
|
1312
|
-
resolveServerExit = resolve;
|
|
1313
|
-
});
|
|
1314
|
-
let resolveDevFailure = () => void 0;
|
|
1315
|
-
const devFailed = new Promise((resolve) => {
|
|
1316
|
-
resolveDevFailure = resolve;
|
|
1317
|
-
});
|
|
1318
|
-
const forceKill = () => {
|
|
1319
|
-
server?.kill("SIGKILL");
|
|
1320
|
-
web?.kill("SIGKILL");
|
|
1321
|
-
};
|
|
1322
|
-
process.once("exit", forceKill);
|
|
1323
|
-
const stop = () => {
|
|
1324
|
-
stopped = true;
|
|
1325
|
-
stopping ??= (async () => {
|
|
1326
|
-
try {
|
|
1327
|
-
await stopChild(server, 1e4, "message");
|
|
1328
|
-
} finally {
|
|
1329
|
-
try {
|
|
1330
|
-
await stopChild(web, 5e3, "disconnect");
|
|
1331
|
-
} finally {
|
|
1332
|
-
try {
|
|
1333
|
-
if (ownedCache) await rm(ownedCache, {
|
|
1334
|
-
recursive: true,
|
|
1335
|
-
force: true
|
|
1336
|
-
});
|
|
1337
|
-
} finally {
|
|
1338
|
-
process.off("exit", forceKill);
|
|
1339
|
-
}
|
|
1340
|
-
}
|
|
1341
|
-
}
|
|
1342
|
-
})();
|
|
1343
|
-
return stopping;
|
|
1344
|
-
};
|
|
1345
|
-
const observe = (child, prefix) => {
|
|
1346
|
-
child.stdout?.on("data", (chunk) => appendLog(prefix, chunk));
|
|
1347
|
-
child.stderr?.on("data", (chunk) => appendLog(prefix, chunk));
|
|
1348
|
-
child.on("error", () => {
|
|
1349
|
-
if (options.dev && !stopped) resolveDevFailure();
|
|
1350
|
-
});
|
|
1351
|
-
child.on("exit", () => {
|
|
1352
|
-
if (options.dev && !stopped) resolveDevFailure();
|
|
1353
|
-
});
|
|
1354
|
-
if (options.dev) child.on("disconnect", () => {
|
|
1355
|
-
if (!stopped) resolveDevFailure();
|
|
1356
|
-
});
|
|
1357
|
-
};
|
|
1358
|
-
try {
|
|
1359
|
-
const generation = randomUUID();
|
|
1360
|
-
let target = null;
|
|
1361
|
-
if (options.dev) {
|
|
1362
|
-
options.signal?.throwIfAborted();
|
|
1363
|
-
web = spawn(commands.webCommand ?? process.execPath, commands.webArgs, {
|
|
1364
|
-
cwd: commands.webCwd ?? repoRoot,
|
|
1365
|
-
env: process.env,
|
|
1366
|
-
stdio: [
|
|
1367
|
-
"ignore",
|
|
1368
|
-
"pipe",
|
|
1369
|
-
"pipe",
|
|
1370
|
-
"ipc"
|
|
1371
|
-
]
|
|
1372
|
-
});
|
|
1373
|
-
observe(web, "[web]");
|
|
1374
|
-
const control = new DevWebControl(web, generation);
|
|
1375
|
-
const child = web;
|
|
1376
|
-
const ready = waitChildMessage(child, (message) => control.accept(child, message) ? control.target : null, options.signal, "Vite");
|
|
1377
|
-
child.send({
|
|
1378
|
-
type: "dev-web.start",
|
|
1379
|
-
generation,
|
|
1380
|
-
origin: canonicalOrigin
|
|
1381
|
-
});
|
|
1382
|
-
target = await ready;
|
|
1383
|
-
ownedCache = devWebCache(target.generation);
|
|
1384
|
-
}
|
|
1385
|
-
options.signal?.throwIfAborted();
|
|
1386
|
-
server = spawn(commands.serverCommand, commands.serverArgs, {
|
|
1387
|
-
cwd: commands.serverCwd,
|
|
1388
|
-
env: {
|
|
1389
|
-
...process.env,
|
|
1390
|
-
LARNESS_LOG_STDERR: "0",
|
|
1391
|
-
LARNESS_TUI_CHILD: "1",
|
|
1392
|
-
LARNESS_PM2_AUTO_INSTALL: process.env.LARNESS_PM2_AUTO_INSTALL ?? "1",
|
|
1393
|
-
...options.dev ? { LARNESS_SERVE_WEB: "0" } : {}
|
|
1394
|
-
},
|
|
1395
|
-
stdio: [
|
|
1396
|
-
"ignore",
|
|
1397
|
-
"pipe",
|
|
1398
|
-
"pipe",
|
|
1399
|
-
"ipc"
|
|
1400
|
-
]
|
|
1401
|
-
});
|
|
1402
|
-
observe(server, "[server]");
|
|
1403
|
-
server.once("exit", resolveServerExit);
|
|
1404
|
-
if (target) {
|
|
1405
|
-
const attached = waitChildMessage(server, (message) => {
|
|
1406
|
-
const parsed = DevWebAttachedSchema.safeParse(message);
|
|
1407
|
-
return parsed.success && parsed.data.generation === generation ? true : null;
|
|
1408
|
-
}, options.signal);
|
|
1409
|
-
server.send({
|
|
1410
|
-
type: "dev-web.attach",
|
|
1411
|
-
...target
|
|
1412
|
-
});
|
|
1413
|
-
await attached;
|
|
1414
|
-
}
|
|
1415
|
-
const serverReady = await waitForHttp(`${localBase}/api/health`, server, { signal: options.signal });
|
|
1416
|
-
const host = new URL(canonicalOrigin).host;
|
|
1417
|
-
const webReady = serverReady && await waitForHttp(`${localBase}/`, server, {
|
|
1418
|
-
requireHtml: true,
|
|
1419
|
-
signal: options.signal,
|
|
1420
|
-
host,
|
|
1421
|
-
timeoutMs: options.dev ? READY_TIMEOUT_MS : 5e3
|
|
1422
|
-
});
|
|
1423
|
-
const resourcesReady = !options.dev || await waitForHttp(`${localBase}/@vite/client`, server, {
|
|
1424
|
-
signal: options.signal,
|
|
1425
|
-
host
|
|
1426
|
-
});
|
|
1427
|
-
if (!serverReady || !webReady || !resourcesReady || web?.exitCode !== null && web !== null || web?.signalCode !== null && web !== null) throw new Error("Server 或 Web 未就绪;请查看本机日志,打包模式请先运行 pnpm build。");
|
|
1428
|
-
if (!existsSync(path.join(dataDir, "ipc", "token"))) throw new Error("IPC 尚未就绪");
|
|
1429
|
-
} catch (error) {
|
|
1430
|
-
await stop();
|
|
1431
|
-
throw error;
|
|
1432
|
-
}
|
|
1433
|
-
out(pc.bold("Larness"));
|
|
1434
|
-
out(`${pc.green("✓")} 数据目录 ${dataDir}`);
|
|
1435
|
-
out(`${pc.green("✓")} Server ${serverUrl}`);
|
|
1436
|
-
out(`${pc.green("✓")} Web ${webUrl}`);
|
|
1437
|
-
out(`${pc.green("✓")} IPC ${path.join(dataDir, "ipc")}`);
|
|
1438
|
-
return {
|
|
1439
|
-
dataDir,
|
|
1440
|
-
config: loaded,
|
|
1441
|
-
dev: options.dev,
|
|
1442
|
-
serverUrl,
|
|
1443
|
-
webUrl,
|
|
1444
|
-
logs: () => [...logs],
|
|
1445
|
-
stop,
|
|
1446
|
-
serverExited,
|
|
1447
|
-
devFailed
|
|
1448
|
-
};
|
|
1449
|
-
}
|
|
1450
1236
|
// src/tui/main.ts
|
|
1451
|
-
async function runSession(client, runtime, terminal) {
|
|
1452
|
-
|
|
1237
|
+
async function runSession(client, runtime, terminal, attached) {
|
|
1238
|
+
const init = InitStatusSchema.parse(await client.request("init.status"));
|
|
1239
|
+
const interruptLabel = attached ? "Ctrl+C 退出面板" : "Ctrl+C 停止服务";
|
|
1240
|
+
if (!init.initialized) {
|
|
1453
1241
|
const current = runtime?.config.config.server;
|
|
1454
1242
|
terminal.render({
|
|
1455
1243
|
title: "初始化",
|
|
1456
1244
|
body: ["确认当前设置后初始化本机工作空间。", `Web:${runtime?.webUrl ?? "本机"}`],
|
|
1457
|
-
footer:
|
|
1245
|
+
footer: `Enter 选择 · Esc 取消 · ${interruptLabel}`
|
|
1458
1246
|
});
|
|
1459
1247
|
if ((await runInitWizard({
|
|
1460
1248
|
advanced: () => terminal.prompt((context) => confirm({
|
|
@@ -1468,7 +1256,7 @@ async function runSession(client, runtime, terminal) {
|
|
|
1468
1256
|
terminal.render({
|
|
1469
1257
|
title: "初始化确认",
|
|
1470
1258
|
body: summary.split("\n"),
|
|
1471
|
-
footer:
|
|
1259
|
+
footer: `Enter 确认 · Esc 取消 · ${interruptLabel}`
|
|
1472
1260
|
});
|
|
1473
1261
|
return terminal.prompt((context) => confirm({
|
|
1474
1262
|
message: "确认写入配置?",
|
|
@@ -1478,19 +1266,33 @@ async function runSession(client, runtime, terminal) {
|
|
|
1478
1266
|
notify: (message) => terminal.render({
|
|
1479
1267
|
title: "初始化",
|
|
1480
1268
|
body: [message],
|
|
1481
|
-
footer:
|
|
1269
|
+
footer: `请重新输入 · Esc 返回 · ${interruptLabel}`
|
|
1482
1270
|
})
|
|
1483
1271
|
}, async (params) => {
|
|
1484
1272
|
terminal.render({
|
|
1485
1273
|
title: "初始化",
|
|
1486
1274
|
body: ["正在保存配置…"],
|
|
1487
|
-
footer:
|
|
1275
|
+
footer: interruptLabel
|
|
1488
1276
|
});
|
|
1489
1277
|
return InitResultSchema.parse(await client.request("init.apply", params));
|
|
1490
1278
|
}, {
|
|
1491
1279
|
config: runtime?.config.config,
|
|
1492
1280
|
env: process.env
|
|
1493
1281
|
})).restartRequired) {
|
|
1282
|
+
if (runtime === void 0) {
|
|
1283
|
+
terminal.render({
|
|
1284
|
+
title: "应用配置",
|
|
1285
|
+
body: [
|
|
1286
|
+
"配置已保存,需要重启管理面才会生效。",
|
|
1287
|
+
"当前控制面板是附加模式,不会自动停止服务。",
|
|
1288
|
+
"",
|
|
1289
|
+
"请在终端执行:larness restart"
|
|
1290
|
+
],
|
|
1291
|
+
footer: "按回车退出面板 · 服务继续运行"
|
|
1292
|
+
});
|
|
1293
|
+
await terminal.nextKey();
|
|
1294
|
+
return "done";
|
|
1295
|
+
}
|
|
1494
1296
|
terminal.render({
|
|
1495
1297
|
title: "应用配置",
|
|
1496
1298
|
body: ["配置已保存,正在重启 Server 与 Web…"],
|
|
@@ -1504,6 +1306,7 @@ async function runSession(client, runtime, terminal) {
|
|
|
1504
1306
|
client,
|
|
1505
1307
|
webUrl: runtime?.webUrl,
|
|
1506
1308
|
localLogs: runtime?.logs,
|
|
1309
|
+
attached,
|
|
1507
1310
|
updateCheck: async () => {
|
|
1508
1311
|
const notice = await checkForUpdate({
|
|
1509
1312
|
version: SERVER_VERSION,
|
|
@@ -1515,9 +1318,18 @@ async function runSession(client, runtime, terminal) {
|
|
|
1515
1318
|
});
|
|
1516
1319
|
return "done";
|
|
1517
1320
|
}
|
|
1321
|
+
async function waitForCleanStop(timeoutMs = 5e3) {
|
|
1322
|
+
const dataDir = resolveDataDir();
|
|
1323
|
+
const deadline = Date.now() + timeoutMs;
|
|
1324
|
+
for (;;) {
|
|
1325
|
+
if (await inspectInstanceLock(dataDir) === null) return true;
|
|
1326
|
+
if (Date.now() >= deadline) return false;
|
|
1327
|
+
await setTimeout(150);
|
|
1328
|
+
}
|
|
1329
|
+
}
|
|
1518
1330
|
async function main() {
|
|
1519
1331
|
const args = process.argv.slice(2);
|
|
1520
|
-
const managed = args.includes("--managed") || process.env.LARNESS_TUI_MANAGED === "1";
|
|
1332
|
+
const managed = !args.includes("--attach") && (args.includes("--managed") || process.env.LARNESS_TUI_MANAGED === "1");
|
|
1521
1333
|
const dev = args.includes("--dev") || process.env.LARNESS_TUI_DEV === "1";
|
|
1522
1334
|
const terminal = new TerminalSession();
|
|
1523
1335
|
const abort = new AbortController();
|
|
@@ -1547,6 +1359,7 @@ async function main() {
|
|
|
1547
1359
|
for (let attempt = 0;; attempt += 1) {
|
|
1548
1360
|
abort.signal.throwIfAborted();
|
|
1549
1361
|
let client = null;
|
|
1362
|
+
let release = () => void 0;
|
|
1550
1363
|
try {
|
|
1551
1364
|
client = await connectIpc({
|
|
1552
1365
|
dataDir: current?.dataDir ?? resolveDataDir(),
|
|
@@ -1557,23 +1370,31 @@ async function main() {
|
|
|
1557
1370
|
throw new IpcClientError("CONNECTION_CLOSED", "无法连接本机 IPC");
|
|
1558
1371
|
});
|
|
1559
1372
|
activeClient = client;
|
|
1560
|
-
|
|
1373
|
+
release = client.onClose(() => {
|
|
1374
|
+
if (abort.signal.aborted) return;
|
|
1375
|
+
serverGone = true;
|
|
1376
|
+
terminal.interrupt();
|
|
1377
|
+
});
|
|
1378
|
+
return await runSession(client, current, terminal, !managed);
|
|
1561
1379
|
} catch (error) {
|
|
1562
1380
|
if (abort.signal.aborted) throw new TerminalInterrupted();
|
|
1563
1381
|
if (!(error instanceof IpcClientError) || !["CONNECTION_CLOSED", "TIMEOUT"].includes(error.code) || attempt >= 2) throw error;
|
|
1564
1382
|
terminal.render({
|
|
1565
1383
|
title: "重新连接",
|
|
1566
1384
|
body: ["本机连接中断,正在重新连接…", "未确认的写操作不会自动重试。"],
|
|
1567
|
-
footer: "Ctrl+C 停止服务"
|
|
1385
|
+
footer: managed ? "Ctrl+C 停止服务" : "Ctrl+C 退出面板"
|
|
1568
1386
|
});
|
|
1569
|
-
await setTimeout
|
|
1387
|
+
await setTimeout(1e3, void 0, { signal: abort.signal });
|
|
1570
1388
|
} finally {
|
|
1389
|
+
release();
|
|
1571
1390
|
client?.close();
|
|
1572
1391
|
activeClient = null;
|
|
1573
1392
|
}
|
|
1574
1393
|
}
|
|
1575
1394
|
};
|
|
1576
1395
|
let failure;
|
|
1396
|
+
let cleanStop = false;
|
|
1397
|
+
let serverGone = false;
|
|
1577
1398
|
try {
|
|
1578
1399
|
if (managed) await runManagedSessions({
|
|
1579
1400
|
start: () => startManagedRuntime({
|
|
@@ -1601,7 +1422,18 @@ async function main() {
|
|
|
1601
1422
|
});
|
|
1602
1423
|
else await attach();
|
|
1603
1424
|
} catch (error) {
|
|
1604
|
-
if (
|
|
1425
|
+
if (serverGone) {
|
|
1426
|
+
cleanStop = await waitForCleanStop();
|
|
1427
|
+
if (!cleanStop) failure = /* @__PURE__ */ new Error("服务连接中断,但实例锁仍存在;请查看日志后重新执行 larness tui。");
|
|
1428
|
+
} else if (!(error instanceof TerminalInterrupted) && !(error instanceof NavigationCancelled) && !(error instanceof InitCancelled) && !abort.signal.aborted) {
|
|
1429
|
+
if (error instanceof ServiceStopped) {
|
|
1430
|
+
cleanStop = await waitForCleanStop();
|
|
1431
|
+
if (!cleanStop) failure = /* @__PURE__ */ new Error("停止请求已发送,但服务在超时前没有释放实例锁;请查看日志。");
|
|
1432
|
+
} else if (error instanceof IpcClientError && error.code === "CONNECTION_CLOSED") {
|
|
1433
|
+
cleanStop = await waitForCleanStop();
|
|
1434
|
+
if (!cleanStop) failure = /* @__PURE__ */ new Error("服务连接中断,但实例锁仍存在;请查看日志后重新执行 larness tui。");
|
|
1435
|
+
} else failure = error;
|
|
1436
|
+
}
|
|
1605
1437
|
} finally {
|
|
1606
1438
|
try {
|
|
1607
1439
|
await stopping;
|
|
@@ -1615,7 +1447,9 @@ async function main() {
|
|
|
1615
1447
|
if (failure !== void 0) {
|
|
1616
1448
|
console.error(`[larness] ${displayError(failure)}`);
|
|
1617
1449
|
process.exitCode = 1;
|
|
1618
|
-
} else console.log("
|
|
1450
|
+
} else if (cleanStop) console.log("服务已停止,控制面板已退出。");
|
|
1451
|
+
else if (!managed) console.log("控制面板已退出,服务继续运行。");
|
|
1452
|
+
else console.log("Larness 已退出,Server 与 Web 已停止。");
|
|
1619
1453
|
}
|
|
1620
1454
|
try {
|
|
1621
1455
|
await main();
|