From d116418e65b73923994f0bb42cf2aca4c103ce60 Mon Sep 17 00:00:00 2001 From: QQ597901027 Date: Thu, 5 Nov 2026 09:19:43 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=AE=BE=E7=BD=AE=E9=A1=B5=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E5=B7=A5=E4=BD=9C=E5=8C=BA=E5=8F=96=E6=9C=80=E8=BF=91?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=94=E4=BB=8D=E9=85=8D=E7=BD=AE=E4=BA=86?= =?UTF-8?q?=E7=9B=AE=E7=9A=84=E5=9C=B0=E7=9A=84=E5=B7=A5=E4=BD=9C=E5=8C=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 09b6e29..ba0d01f 100644 --- a/index.js +++ b/index.js @@ -16,7 +16,7 @@ * the DSH credential service (falling back to the process environment). */ -import { existsSync, readdirSync } from 'node:fs' +import { existsSync, readdirSync, statSync } from 'node:fs' import { mkdir, readFile, writeFile } from 'node:fs/promises' import { request as httpRequest } from 'node:http' import { request as httpsRequest } from 'node:https' @@ -508,8 +508,52 @@ export function apply(ctx, config) { } } + /** + * The workspace whose destinations file was written most recently. An empty + * destinations list (a scratch config, or one where every system was removed) + * loses to any workspace that still lists systems, so the settings page never + * opens on an empty page while a usable config exists elsewhere. + */ + async function lastModifiedWorkspace() { + let best + let bestTime = -Infinity + let any + let anyTime = -Infinity + for (const row of listWorkspaces()) { + if (!row.path) continue + for (const fileName of FILE_NAMES) { + const candidate = join(row.path, CONFIG_DIR, fileName) + let mtimeMs + try { + mtimeMs = statSync(candidate).mtimeMs + } catch { + continue + } + if (mtimeMs > anyTime) { + anyTime = mtimeMs + any = row.path + } + if (mtimeMs <= bestTime) continue + try { + const parsed = await parseDestinations(await readFile(candidate, 'utf8')) + if (!normalizeDestinations(parsed.destinations).length) continue + } catch { + continue + } + bestTime = mtimeMs + best = row.path + } + } + return best || any + } + async function stateOf({ sessionId, workspace }) { - const cwd = workspace || resolveCwd(sessionId) || listWorkspaces()[0]?.path || process.cwd() + const cwd = + workspace || + resolveCwd(sessionId) || + (await lastModifiedWorkspace()) || + listWorkspaces()[0]?.path || + process.cwd() const file = existingWorkspaceFile(cwd) let raw = '' let exists = false