lantern-harness 0.1.5 → 0.1.7
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/CHANGELOG.md +60 -0
- package/README.md +7 -7
- package/builtin/session/lh-nav-groups.mjs +3 -0
- package/builtin/session/update-check.mjs +517 -0
- package/cli/lantern.mjs +123 -22
- package/cli/server.mjs +7 -2
- package/config/app.defaults.json +4 -0
- package/dist/500.html +17 -3
- package/dist/_astro/Base.Bum3N3jp.css +1 -0
- package/dist/_astro/Base.astro_astro_type_script_index_0_lang.D0snmAJ_.js +1 -0
- package/dist/_astro/CaseExecBoard.BAV4EE3o.js +1 -0
- package/dist/_astro/{CaseGenerateApp.CGAvw04r.js → CaseGenerateApp.Bz4ryNJG.js} +1 -1
- package/dist/_astro/{CasesLibrary.Bz6ol7zL.js → CasesLibrary.BPifyHUA.js} +1 -1
- package/dist/_astro/ChatApp.DLo31SYZ.js +276 -0
- package/dist/_astro/DashboardBoard.DtuJX0Oi.js +5 -0
- package/dist/_astro/{DocsFileBrowser.CBXAmRPO.js → DocsFileBrowser.D3cKurQE.js} +1 -1
- package/dist/_astro/{DocsPager.CPwAnvW_.js → DocsPager.rzu4mnzw.js} +1 -1
- package/dist/_astro/DrawerField.CWMLhcPX.js +1 -0
- package/dist/_astro/{KnowledgeLibrary._QhB3GHk.js → KnowledgeLibrary.B0sO4QR1.js} +1 -1
- package/dist/_astro/NavLibraries.Cvg3wery.js +1 -0
- package/dist/_astro/PluginsPage.DIltPILi.js +39 -0
- package/dist/_astro/PluginsPage.uvmSuXzi.js +1 -0
- package/dist/_astro/PmBugsBoard.CiaIhCHv.js +1 -0
- package/dist/_astro/PmIterationsBoard.H67QwKFw.js +1 -0
- package/dist/_astro/PmProjectDetail.DqDoIg8a.js +1 -0
- package/dist/_astro/PmProjectsBoard.nOC1Hugt.js +1 -0
- package/dist/_astro/PmRequirementsBoard.BPjZ7khP.js +1 -0
- package/dist/_astro/PmRequirementsBoard.CanxvcKg.js +1 -0
- package/dist/_astro/PmShared.CZnXaUq-.js +1 -0
- package/dist/_astro/{PromptsLibrary.BLPVH34I.js → PromptsLibrary.DU9EuSim.js} +1 -1
- package/dist/_astro/{ReportsPage.DMk4sUqZ.js → ReportsPage.Cr88iz8c.js} +1 -1
- package/dist/_astro/{RequirementsLibrary.CyYBiauz.js → RequirementsLibrary.BwriF8F0.js} +1 -1
- package/dist/_astro/{SearchableSelect.yFomIhKQ.js → SearchableSelect.Ctg8V1Z1.js} +1 -1
- package/dist/_astro/SelectDropdown.Buv7rQJ_.js +1 -0
- package/dist/_astro/SettingsDialog.kpJwoEl3.js +1 -0
- package/dist/_astro/SlideOverDrawer.TPbGEoWn.js +1 -0
- package/dist/_astro/SpaceSwitcher.BEJo5KFi.js +7 -0
- package/dist/_astro/TasksBoard.BZi_GBPG.js +1 -0
- package/dist/_astro/{TemplatesLibrary.DV_MUASP.js → TemplatesLibrary.T4K9jUex.js} +1 -1
- package/dist/_astro/TestStatsBoard.Dd1Kcmx6.js +1 -0
- package/dist/_astro/TodosBoard.C4yd_JFb.js +1 -0
- package/dist/_astro/app-config.DKMxjv3g.js +1 -0
- package/dist/_astro/case-exec.BipZCK6G.js +1 -0
- package/dist/_astro/i18n.BXeGGutk.js +7 -0
- package/dist/_astro/{open-docs-in-session.D7Hxuu0K.js → open-docs-in-session.B0ZdZNj2.js} +1 -1
- package/dist/_astro/plugins.D5ypJIXn.js +1 -0
- package/dist/_astro/pm.BsnTv63L.js +4 -0
- package/dist/_astro/{settings.1aF-h9sh.js → settings.CnDCvwnQ.js} +2 -2
- package/dist/_astro/spaces-client.Cai2tHEI.js +1 -0
- package/dist/_astro/tasks.otRBFydq.js +1 -0
- package/dist/_astro/{test-stats.BIHEJz5c.js → test-stats.MEm3K2rB.js} +1 -1
- package/dist/_astro/update.DXCPfU4a.js +1 -0
- package/dist/_astro/{view-prefs.DHPN5Yq6.js → view-prefs.HCowOWXL.js} +1 -1
- package/dist/case-exec/index.html +18 -4
- package/dist/cases/generate/index.html +18 -4
- package/dist/cases/library/index.html +18 -4
- package/dist/dashboard/index.html +18 -4
- package/dist/docs/changelog/index.html +74 -5
- package/dist/docs/install/index.html +17 -3
- package/dist/docs/manual/index.html +99 -26
- package/dist/index.html +18 -4
- package/dist/knowledge/index.html +18 -4
- package/dist/knowledge//345/212/237/350/203/275/346/265/213/350/257/225/347/224/250/344/276/213/347/274/226/345/206/231/350/247/204/350/214/203/index.html +17 -3
- package/dist/plugins/index.html +18 -4
- package/dist/pm/bugs/index.html +281 -0
- package/dist/pm/iterations/index.html +281 -0
- package/dist/pm/projects/detail/index.html +281 -0
- package/dist/pm/projects/index.html +281 -0
- package/dist/pm/requirements/index.html +281 -0
- package/dist/prompts/index.html +18 -4
- package/dist/reports/index.html +18 -4
- package/dist/requirements/index.html +18 -4
- package/dist/tasks/index.html +18 -4
- package/dist/templates/index.html +18 -4
- package/dist/templates//345/212/237/350/203/275/346/265/213/350/257/225/346/212/245/345/221/212//345/212/237/350/203/275/346/265/213/350/257/225/346/212/245/345/221/212/346/250/241/346/235/277/index.html +17 -3
- package/dist/templates//346/200/247/350/203/275/346/265/213/350/257/225/346/212/245/345/221/212//346/200/247/350/203/275/346/265/213/350/257/225/346/212/245/345/221/212/346/250/241/346/235/277/index.html +17 -3
- package/dist/templates//346/200/247/350/203/275/346/265/213/350/257/225/347/224/250/344/276/213//346/200/247/350/203/275/346/265/213/350/257/225/347/224/250/344/276/213/346/250/241/346/235/277/index.html +17 -3
- package/dist/templates//346/216/245/345/217/243/346/265/213/350/257/225/346/212/245/345/221/212//346/216/245/345/217/243/346/265/213/350/257/225/346/212/245/345/221/212/346/250/241/346/235/277/index.html +17 -3
- package/dist/templates//350/207/252/345/212/250/345/214/226/346/265/213/350/257/225/347/224/250/344/276/213//350/207/252/345/212/250/345/214/226/346/265/213/350/257/225/347/224/250/344/276/213/346/250/241/346/235/277/index.html +17 -3
- package/dist/test-stats/index.html +18 -4
- package/dist/todos/index.html +18 -4
- package/docs//345/205/250/345/261/200/346/217/222/344/273/266.md +1 -1
- package/docs//346/223/215/344/275/234/346/211/213/345/206/214.md +82 -41
- package/lh-plugins/lh-memory.mjs +1 -3
- package/lh-plugins/lh-pm.mjs +963 -0
- package/lh-plugins/lh-reports.mjs +12 -12
- package/lh-plugins/lh-stats.mjs +6 -2
- package/lh-plugins/lh-tasks.mjs +5 -7
- package/lh-plugins/lh-todos.mjs +35 -8
- package/lh-plugins/lh-update.mjs +171 -0
- package/package.json +1 -1
- package/dist/_astro/Base.CVuoZiOB.css +0 -1
- package/dist/_astro/Base.astro_astro_type_script_index_0_lang.BY4OccDH.js +0 -1
- package/dist/_astro/CaseExecBoard.GIKQ6ccZ.js +0 -1
- package/dist/_astro/ChatApp.DZtDZRst.js +0 -273
- package/dist/_astro/DashboardBoard.CiTrVBqY.js +0 -5
- package/dist/_astro/DrawerField.KANqQwFl.js +0 -1
- package/dist/_astro/NavLibraries.fxwcz0b0.js +0 -1
- package/dist/_astro/PluginsPage.BfG3nVYJ.js +0 -1
- package/dist/_astro/PluginsPage.n_0T_ww4.js +0 -39
- package/dist/_astro/SelectDropdown.flhHcwyd.js +0 -1
- package/dist/_astro/SettingsDialog.DrVaJAbf.js +0 -1
- package/dist/_astro/SpaceSwitcher.C84a-OBd.js +0 -7
- package/dist/_astro/TasksBoard.Dwva6gEQ.js +0 -1
- package/dist/_astro/TestStatsBoard.Cl6mBowv.js +0 -1
- package/dist/_astro/TodosBoard.BTl5qvQt.js +0 -1
- package/dist/_astro/app-config.BAFE-Tim.js +0 -1
- package/dist/_astro/case-exec.HjGrOY_O.js +0 -1
- package/dist/_astro/i18n.DFwWWvim.js +0 -7
- package/dist/_astro/plugins.BHRarX88.js +0 -1
- package/dist/_astro/tasks.hIKi1ek-.js +0 -1
|
@@ -96,15 +96,10 @@ function kindDir(spaceDir, kind) {
|
|
|
96
96
|
return name ? path.join(reportsRoot(spaceDir), name) : null;
|
|
97
97
|
}
|
|
98
98
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
for (const name of Object.values(REPORT_KINDS)) {
|
|
104
|
-
fs.mkdirSync(path.join(root, name), { recursive: true });
|
|
105
|
-
}
|
|
106
|
-
return root;
|
|
107
|
-
}
|
|
99
|
+
/*
|
|
100
|
+
* 汇报目录(工作汇报/日报|周报|月报)不在打开插件时预建:
|
|
101
|
+
* 只有真正写进汇报文件时,才由写入方(fs 工具等)铺出对应目录。
|
|
102
|
+
*/
|
|
108
103
|
|
|
109
104
|
/* ── 插件状态:开关 + 选中的提示词文件名 ── */
|
|
110
105
|
|
|
@@ -208,9 +203,15 @@ function fileUrl(kind, name) {
|
|
|
208
203
|
function listReports(spaceDir, kind) {
|
|
209
204
|
const dir = kindDir(spaceDir, kind);
|
|
210
205
|
if (!dir) return null;
|
|
211
|
-
ensureReportDirs(spaceDir);
|
|
212
206
|
const items = [];
|
|
213
|
-
|
|
207
|
+
let entries;
|
|
208
|
+
try {
|
|
209
|
+
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
210
|
+
} catch {
|
|
211
|
+
// 还没有任何汇报文件:目录不存在就是空列表,不顺手铺空目录
|
|
212
|
+
return items;
|
|
213
|
+
}
|
|
214
|
+
for (const ent of entries) {
|
|
214
215
|
if (!ent.isFile() || ent.name.startsWith('.')) continue;
|
|
215
216
|
const ext = path.extname(ent.name).toLowerCase();
|
|
216
217
|
if (!TEXT_EXTS.has(ext)) continue;
|
|
@@ -328,7 +329,6 @@ export function lhReportsPlugin() {
|
|
|
328
329
|
const body = JSON.parse((await readBody(req)) || '{}');
|
|
329
330
|
if (body.enabled === true) {
|
|
330
331
|
const out = ensurePrompt(spaceDir, state);
|
|
331
|
-
ensureReportDirs(spaceDir);
|
|
332
332
|
writeState(spaceDir, { enabled: true, promptFile: out.name });
|
|
333
333
|
return sendJson(res, 200, {
|
|
334
334
|
ok: true,
|
package/lh-plugins/lh-stats.mjs
CHANGED
|
@@ -308,10 +308,14 @@ export function lhStatsPlugin() {
|
|
|
308
308
|
return sendJson(res, 200, { ok: true, ...store });
|
|
309
309
|
}
|
|
310
310
|
|
|
311
|
-
// 「+
|
|
311
|
+
// 「+ 添加记录」的空占位行也要落库,否则刷新就没了。
|
|
312
|
+
// 打开插件 / 进入页面时也会调一次 sync:没有数据就什么都不写,
|
|
313
|
+
// 避免只是开个开关就在数据空间里铺出空目录和空文件。
|
|
312
314
|
if (method === 'POST' && itemId === 'sync') {
|
|
313
315
|
const store = readStore(root, kind);
|
|
314
|
-
|
|
316
|
+
if (store.items.length > 0 || fs.existsSync(storeFile(root, kind))) {
|
|
317
|
+
writeStore(root, kind, store);
|
|
318
|
+
}
|
|
315
319
|
return sendJson(res, 200, {
|
|
316
320
|
ok: true,
|
|
317
321
|
file: storeFile(root, kind),
|
package/lh-plugins/lh-tasks.mjs
CHANGED
|
@@ -10,7 +10,7 @@ import fs from 'node:fs';
|
|
|
10
10
|
import path from 'node:path';
|
|
11
11
|
|
|
12
12
|
import { getDataRoot } from '../builtin/session/space-context.mjs';
|
|
13
|
-
import { prepareStorePath, workStorePath } from './lh-work-store.mjs';
|
|
13
|
+
import { prepareStorePath, readableStorePath, workStorePath } from './lh-work-store.mjs';
|
|
14
14
|
|
|
15
15
|
const PREFIX = '/__tb_tasks';
|
|
16
16
|
const VERSION = 1;
|
|
@@ -35,12 +35,9 @@ function readBody(req) {
|
|
|
35
35
|
});
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
function storeFile(root) {
|
|
39
|
-
return prepareStorePath(root, 'tasks');
|
|
40
|
-
}
|
|
41
|
-
|
|
42
38
|
function readStore(root) {
|
|
43
|
-
|
|
39
|
+
// 只读不建目录:打开页面 / 打开插件不该在数据空间里铺出空文件夹
|
|
40
|
+
const fp = readableStorePath(root, 'tasks');
|
|
44
41
|
try {
|
|
45
42
|
const raw = JSON.parse(fs.readFileSync(fp, 'utf8'));
|
|
46
43
|
const items = Array.isArray(raw?.items) ? raw.items.map(normalizeStored).filter(Boolean) : [];
|
|
@@ -52,7 +49,8 @@ function readStore(root) {
|
|
|
52
49
|
|
|
53
50
|
/** 先写临时文件再 rename:中途失败也不会留下半份 JSON */
|
|
54
51
|
function writeStore(root, store) {
|
|
55
|
-
|
|
52
|
+
// 真正落库时才建目录(并把 .lantern/local 里的旧文件迁出来)
|
|
53
|
+
const fp = prepareStorePath(root, 'tasks');
|
|
56
54
|
fs.mkdirSync(path.dirname(fp), { recursive: true });
|
|
57
55
|
const tmp = `${fp}.${process.pid}.tmp`;
|
|
58
56
|
fs.writeFileSync(tmp, `${JSON.stringify(store, null, 2)}\n`, 'utf8');
|
package/lh-plugins/lh-todos.mjs
CHANGED
|
@@ -16,7 +16,7 @@ import { prepareStorePath, readableStorePath, workStorePath } from './lh-work-st
|
|
|
16
16
|
const PREFIX = '/__tb_todos';
|
|
17
17
|
const VERSION = 1;
|
|
18
18
|
|
|
19
|
-
export const TODO_STATUSES = ['pending', 'in_progress', 'done', 'cancelled'];
|
|
19
|
+
export const TODO_STATUSES = ['pending', 'in_progress', 'done', 'overdue', 'cancelled'];
|
|
20
20
|
export const TODO_PRIORITIES = ['urgent_important', 'important', 'urgent', 'normal'];
|
|
21
21
|
|
|
22
22
|
const DATE_RE = /^\d{4}-\d{2}-\d{2}$/;
|
|
@@ -36,12 +36,9 @@ function readBody(req) {
|
|
|
36
36
|
});
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
function storeFile(root) {
|
|
40
|
-
return prepareStorePath(root, 'todos');
|
|
41
|
-
}
|
|
42
|
-
|
|
43
39
|
function readStore(root) {
|
|
44
|
-
|
|
40
|
+
// 只读不建目录:打开页面 / 打开插件不该在数据空间里铺出空文件夹
|
|
41
|
+
const fp = readableStorePath(root, 'todos');
|
|
45
42
|
try {
|
|
46
43
|
const raw = JSON.parse(fs.readFileSync(fp, 'utf8'));
|
|
47
44
|
const items = Array.isArray(raw?.items) ? raw.items.map(normalizeStored).filter(Boolean) : [];
|
|
@@ -53,7 +50,8 @@ function readStore(root) {
|
|
|
53
50
|
|
|
54
51
|
/** 先写临时文件再 rename:中途失败也不会留下半份 JSON */
|
|
55
52
|
function writeStore(root, store) {
|
|
56
|
-
|
|
53
|
+
// 真正落库时才建目录(并把 .lantern/local 里的旧文件迁出来)
|
|
54
|
+
const fp = prepareStorePath(root, 'todos');
|
|
57
55
|
fs.mkdirSync(path.dirname(fp), { recursive: true });
|
|
58
56
|
const tmp = `${fp}.${process.pid}.tmp`;
|
|
59
57
|
fs.writeFileSync(tmp, `${JSON.stringify(store, null, 2)}\n`, 'utf8');
|
|
@@ -102,6 +100,26 @@ function endDateFor(status, existing) {
|
|
|
102
100
|
return existing || todayLocal();
|
|
103
101
|
}
|
|
104
102
|
|
|
103
|
+
/**
|
|
104
|
+
* 自动标记「已延期」:未结束且截止日期早于今天 → status = overdue。
|
|
105
|
+
* 只在读取 / 写入时顺着日期单向升级,不往回改(用户把截止日推后可以自己改回进行中)。
|
|
106
|
+
* 返回是否真的改了,调用方据此决定要不要落盘。
|
|
107
|
+
* @param {Array<{status: string, dueDate: string}>} items
|
|
108
|
+
*/
|
|
109
|
+
function markOverdue(items) {
|
|
110
|
+
const today = todayLocal();
|
|
111
|
+
let changed = false;
|
|
112
|
+
for (const item of items) {
|
|
113
|
+
if (CLOSED_STATUSES.has(item.status) || item.status === 'overdue') continue;
|
|
114
|
+
if (item.dueDate && item.dueDate < today) {
|
|
115
|
+
item.status = 'overdue';
|
|
116
|
+
item.taskEndDate = '';
|
|
117
|
+
changed = true;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return changed;
|
|
121
|
+
}
|
|
122
|
+
|
|
105
123
|
/**
|
|
106
124
|
* 待办关联任务后,把任务从「任务池 / 已完成」拉回「进行中」:
|
|
107
125
|
* 关联上就说明这件事已经在做了。已关闭的任务不动(也不在选择范围内)。
|
|
@@ -221,7 +239,10 @@ export function lhTodosPlugin() {
|
|
|
221
239
|
}
|
|
222
240
|
|
|
223
241
|
if (method === 'GET' && !itemId) {
|
|
224
|
-
|
|
242
|
+
const store = readStore(root);
|
|
243
|
+
// 过了截止日期的自动升级成「已延期」并落库,页面与 AI 读到的是同一个状态
|
|
244
|
+
if (markOverdue(store.items)) writeStore(root, store);
|
|
245
|
+
return sendJson(res, 200, { ok: true, ...store });
|
|
225
246
|
}
|
|
226
247
|
|
|
227
248
|
if (method === 'POST') {
|
|
@@ -238,6 +259,7 @@ export function lhTodosPlugin() {
|
|
|
238
259
|
created.push(item);
|
|
239
260
|
}
|
|
240
261
|
store.items.push(...created);
|
|
262
|
+
markOverdue(store.items);
|
|
241
263
|
writeStore(root, store);
|
|
242
264
|
// 新建时就带了关联任务:任务池 / 已完成 → 进行中
|
|
243
265
|
for (const item of created) promoteLinkedTask(root, item.taskId);
|
|
@@ -264,6 +286,11 @@ export function lhTodosPlugin() {
|
|
|
264
286
|
// 已经是结束态时沿用原值,改别的字段不会把结束日期刷新成今天。
|
|
265
287
|
const wasClosed = CLOSED_STATUSES.has(base.status);
|
|
266
288
|
next.taskEndDate = endDateFor(next.status, wasClosed ? base.taskEndDate : '');
|
|
289
|
+
// 改完截止日期已经过期了:顺手标成「已延期」,不用等下次刷新
|
|
290
|
+
if (!CLOSED_STATUSES.has(next.status) && next.dueDate && next.dueDate < todayLocal()) {
|
|
291
|
+
next.status = 'overdue';
|
|
292
|
+
next.taskEndDate = '';
|
|
293
|
+
}
|
|
267
294
|
store.items[idx] = next;
|
|
268
295
|
writeStore(root, store);
|
|
269
296
|
// 只有「这次新关联了任务」才把任务拉回进行中,改别的字段不碰任务状态
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 版本检测与一键更新接口(`/__tb_update`)。
|
|
3
|
+
*
|
|
4
|
+
* - `GET /__tb_update[?refresh=1]`:当前版本、npm 最新版本、安装方式、上次更新状态
|
|
5
|
+
* - `POST /__tb_update`:停服 → 全局安装 → 自动重启(body 可带 `version`)
|
|
6
|
+
*
|
|
7
|
+
* 只允许「本进程就是在跑的那个服务」触发更新:
|
|
8
|
+
* 同一台机器可能有多个 lantern 实例(不同端口 / 不同数据目录),
|
|
9
|
+
* 更新会停掉 run.json 里记录的那个 pid,误触会打断别人的会话。
|
|
10
|
+
*/
|
|
11
|
+
import fs from 'node:fs';
|
|
12
|
+
import path from 'node:path';
|
|
13
|
+
import { spawn } from 'node:child_process';
|
|
14
|
+
import { fileURLToPath } from 'node:url';
|
|
15
|
+
|
|
16
|
+
import { isPidAlive, loadRunState } from '../cli/user-config.mjs';
|
|
17
|
+
import {
|
|
18
|
+
detachedCwd,
|
|
19
|
+
detectInstallMode,
|
|
20
|
+
fetchLatestVersion,
|
|
21
|
+
hasUpdate,
|
|
22
|
+
readUpdateStatus,
|
|
23
|
+
resolvePackageManager,
|
|
24
|
+
updateLogHint,
|
|
25
|
+
writeUpdateStatus,
|
|
26
|
+
} from '../builtin/session/update-check.mjs';
|
|
27
|
+
|
|
28
|
+
const PREFIX = '/__tb_update';
|
|
29
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
30
|
+
const PKG_ROOT = path.resolve(__dirname, '..');
|
|
31
|
+
const CLI_PATH = path.join(PKG_ROOT, 'cli', 'lantern.mjs');
|
|
32
|
+
|
|
33
|
+
/** 本进程读到的版本:更新替换的是另一个目录,这里始终是旧版本 */
|
|
34
|
+
const CURRENT_VERSION = (() => {
|
|
35
|
+
try {
|
|
36
|
+
const raw = JSON.parse(fs.readFileSync(path.join(PKG_ROOT, 'package.json'), 'utf8'));
|
|
37
|
+
return typeof raw.version === 'string' ? raw.version : '0.0.0';
|
|
38
|
+
} catch {
|
|
39
|
+
return '0.0.0';
|
|
40
|
+
}
|
|
41
|
+
})();
|
|
42
|
+
|
|
43
|
+
function sendJson(res, status, body) {
|
|
44
|
+
res.statusCode = status;
|
|
45
|
+
res.setHeader('Content-Type', 'application/json; charset=utf-8');
|
|
46
|
+
res.setHeader('Cache-Control', 'no-store');
|
|
47
|
+
res.end(JSON.stringify(body));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async function readJsonBody(req) {
|
|
51
|
+
const chunks = [];
|
|
52
|
+
for await (const chunk of req) chunks.push(chunk);
|
|
53
|
+
const raw = Buffer.concat(chunks).toString('utf8').trim();
|
|
54
|
+
if (!raw) return {};
|
|
55
|
+
try {
|
|
56
|
+
const parsed = JSON.parse(raw);
|
|
57
|
+
return parsed && typeof parsed === 'object' ? parsed : {};
|
|
58
|
+
} catch {
|
|
59
|
+
return {};
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** 当前进程是否就是 run.json 里记录的服务进程 */
|
|
64
|
+
function runsThisProcess() {
|
|
65
|
+
const run = loadRunState();
|
|
66
|
+
if (!run?.pid) return false;
|
|
67
|
+
const pid = Number(run.pid);
|
|
68
|
+
if (!Number.isFinite(pid) || pid !== process.pid) return false;
|
|
69
|
+
return isPidAlive(pid);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* 分离进程执行 `lantern update`:
|
|
74
|
+
* 子进程会先停掉当前服务(也就是本进程),所以必须 detached + unref。
|
|
75
|
+
* cwd 用临时目录:Windows 上从包目录启动会锁住要被替换的目录。
|
|
76
|
+
*/
|
|
77
|
+
function spawnUpdate(version) {
|
|
78
|
+
const args = [CLI_PATH, 'update'];
|
|
79
|
+
if (version) args.push(version);
|
|
80
|
+
const child = spawn(process.execPath, args, {
|
|
81
|
+
detached: true,
|
|
82
|
+
stdio: 'ignore',
|
|
83
|
+
cwd: detachedCwd(),
|
|
84
|
+
env: { ...process.env, LANTERN_UPDATE_FROM_UI: '1' },
|
|
85
|
+
});
|
|
86
|
+
child.unref();
|
|
87
|
+
return child.pid ?? 0;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function lhUpdatePlugin() {
|
|
91
|
+
return {
|
|
92
|
+
name: 'lh-update',
|
|
93
|
+
configureServer(server) {
|
|
94
|
+
server.middlewares.use(async (req, res, next) => {
|
|
95
|
+
const url = req.url?.split('?')[0] ?? '';
|
|
96
|
+
if (url !== PREFIX) return next();
|
|
97
|
+
|
|
98
|
+
const method = req.method ?? 'GET';
|
|
99
|
+
|
|
100
|
+
if (method === 'GET') {
|
|
101
|
+
const q = new URLSearchParams(req.url.split('?')[1] ?? '');
|
|
102
|
+
const force = q.get('refresh') === '1';
|
|
103
|
+
const pm = resolvePackageManager();
|
|
104
|
+
const mode = detectInstallMode(PKG_ROOT, pm);
|
|
105
|
+
const latest = await fetchLatestVersion({ force });
|
|
106
|
+
return sendJson(res, 200, {
|
|
107
|
+
ok: true,
|
|
108
|
+
current: CURRENT_VERSION,
|
|
109
|
+
latest: latest.latest,
|
|
110
|
+
hasUpdate: hasUpdate(latest.latest, CURRENT_VERSION),
|
|
111
|
+
mode,
|
|
112
|
+
pm,
|
|
113
|
+
registry: latest.registry,
|
|
114
|
+
checkedAt: latest.checkedAt ? new Date(latest.checkedAt).toISOString() : '',
|
|
115
|
+
error: latest.error,
|
|
116
|
+
status: readUpdateStatus(),
|
|
117
|
+
logPath: updateLogHint(),
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (method === 'POST') {
|
|
122
|
+
const pm = resolvePackageManager();
|
|
123
|
+
const mode = detectInstallMode(PKG_ROOT, pm);
|
|
124
|
+
if (mode === 'dev') {
|
|
125
|
+
return sendJson(res, 409, {
|
|
126
|
+
ok: false,
|
|
127
|
+
error: '当前是源码 / 开发模式,请在仓库里 git pull 后重新构建(pnpm build)',
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
if (!runsThisProcess()) {
|
|
131
|
+
return sendJson(res, 409, {
|
|
132
|
+
ok: false,
|
|
133
|
+
error:
|
|
134
|
+
'当前页面连接的不是 lantern 启动的服务,无法在这里更新;请在 lantern 打开的页面操作,或用 lantern update',
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// 连点两次不要起两个安装进程(第二个会去停掉第一个正在重启的服务)
|
|
139
|
+
const existing = readUpdateStatus();
|
|
140
|
+
if (existing?.phase === 'installing' || existing?.phase === 'restarting') {
|
|
141
|
+
return sendJson(res, 409, { ok: false, error: '已有更新正在进行中,请稍候' });
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const body = await readJsonBody(req);
|
|
145
|
+
const version = typeof body.version === 'string' ? body.version.trim() : '';
|
|
146
|
+
|
|
147
|
+
writeUpdateStatus({ phase: 'installing', from: CURRENT_VERSION, to: version });
|
|
148
|
+
// 先返回成功:子进程会停掉当前服务,响应必须在 SIGTERM 之前刷出去
|
|
149
|
+
sendJson(res, 202, { ok: true, from: CURRENT_VERSION, to: version });
|
|
150
|
+
// 留一点时间让响应与页面轮询都落定,再让子进程停服
|
|
151
|
+
const timer = setTimeout(() => {
|
|
152
|
+
try {
|
|
153
|
+
spawnUpdate(version);
|
|
154
|
+
} catch (e) {
|
|
155
|
+
writeUpdateStatus({
|
|
156
|
+
phase: 'failed',
|
|
157
|
+
from: CURRENT_VERSION,
|
|
158
|
+
error: e instanceof Error ? e.message : String(e),
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
}, 600);
|
|
162
|
+
timer.unref?.();
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
res.statusCode = 405;
|
|
167
|
+
res.end('Method Not Allowed');
|
|
168
|
+
});
|
|
169
|
+
},
|
|
170
|
+
};
|
|
171
|
+
}
|