Turn on noUnusedLocals/noUnusedParameters so the compiler keeps flagging dead symbols, then clear the 737 findings it reports: unused imports across src/, unused parameters, unreachable locals, and the copy-pasted 135-line import prologues in the seal tool modules (each tool file now imports only what it uses). Also applies the equivalent-safe lint rewrites (Array.from instead of new Array(n).fill, non-null assertions in tests after toBeDefined, simplified spreads, unused catch params) that oxlint will gate in a later commit.
114 lines
3.4 KiB
TypeScript
114 lines
3.4 KiB
TypeScript
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
|
|
import { homedir } from "node:os";
|
|
import { dirname, join } from "node:path";
|
|
import { spawn } from "node:child_process";
|
|
import type { LauncherConfig } from "./runtime.js";
|
|
import { formatBeijingIso, normalizeBeijingIso } from "./config/timestamps.js";
|
|
|
|
export const UPDATE_CACHE_FILE_ENV = "SEAL_USE_UPDATE_CACHE_FILE";
|
|
export const DEFAULT_UPDATE_CACHE_TTL_MS = 24 * 60 * 60 * 1000; // 24 hours
|
|
|
|
const DEFAULT_UPDATE_CACHE_FILE = join(homedir(), ".config", "seal-use", "update-cache.json");
|
|
|
|
export interface UpdateCacheData {
|
|
checkedAt: string;
|
|
latestVersion: string;
|
|
publishedAt?: string;
|
|
releaseUrl?: string;
|
|
}
|
|
|
|
export function resolveUpdateCacheFilePath(): string {
|
|
const custom = process.env[UPDATE_CACHE_FILE_ENV]?.trim();
|
|
if (custom) return custom;
|
|
return DEFAULT_UPDATE_CACHE_FILE;
|
|
}
|
|
|
|
export function loadUpdateCache(filePath = resolveUpdateCacheFilePath()): UpdateCacheData | null {
|
|
if (!existsSync(filePath)) {
|
|
return null;
|
|
}
|
|
try {
|
|
const raw = readFileSync(filePath, "utf-8");
|
|
const parsed = JSON.parse(raw);
|
|
if (
|
|
parsed &&
|
|
typeof parsed === "object" &&
|
|
typeof parsed.latestVersion === "string" &&
|
|
typeof parsed.checkedAt === "string"
|
|
) {
|
|
return {
|
|
...parsed,
|
|
checkedAt: normalizeBeijingIso(parsed.checkedAt),
|
|
...(typeof parsed.publishedAt === "string" ? { publishedAt: normalizeBeijingIso(parsed.publishedAt) } : {})
|
|
} as UpdateCacheData;
|
|
}
|
|
return null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export function saveUpdateCache(
|
|
data: Omit<UpdateCacheData, "checkedAt"> & { checkedAt?: string },
|
|
filePath = resolveUpdateCacheFilePath()
|
|
): void {
|
|
try {
|
|
const dir = dirname(filePath);
|
|
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
|
|
const payload: UpdateCacheData = {
|
|
checkedAt: data.checkedAt ? normalizeBeijingIso(data.checkedAt) : formatBeijingIso(),
|
|
latestVersion: data.latestVersion,
|
|
publishedAt: data.publishedAt ? normalizeBeijingIso(data.publishedAt) : undefined,
|
|
releaseUrl: data.releaseUrl
|
|
};
|
|
|
|
const tmpPath = `${filePath}.tmp.${Date.now()}.${Math.random().toString(36).slice(2, 8)}`;
|
|
writeFileSync(tmpPath, JSON.stringify(payload, null, 2), { mode: 0o600 });
|
|
renameSync(tmpPath, filePath);
|
|
} catch {
|
|
// Ignore cache persistence errors to guarantee non-blocking execution
|
|
}
|
|
}
|
|
|
|
export function isUpdateCacheExpired(
|
|
cache: UpdateCacheData | null | undefined,
|
|
ttlMs = DEFAULT_UPDATE_CACHE_TTL_MS
|
|
): boolean {
|
|
if (!cache || !cache.checkedAt) return true;
|
|
const checkedTime = new Date(cache.checkedAt).getTime();
|
|
if (Number.isNaN(checkedTime)) return true;
|
|
return Date.now() - checkedTime > ttlMs;
|
|
}
|
|
|
|
export function triggerBackgroundUpdateCheck(launcher?: LauncherConfig): boolean {
|
|
if (
|
|
process.env.SEAL_DISABLE_BG_UPDATE === "1" ||
|
|
process.env.NODE_ENV === "test" ||
|
|
process.env.CI
|
|
) {
|
|
return false;
|
|
}
|
|
|
|
if (!launcher || !launcher.command) {
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
const cmd = launcher.command;
|
|
const args = [...(launcher.args ?? []), "update", "check", "--bg-save"];
|
|
const child = spawn(cmd, args, {
|
|
detached: true,
|
|
stdio: "ignore",
|
|
env: {
|
|
...process.env,
|
|
SEAL_DISABLE_BG_UPDATE: "1" // Prevent nested spawns
|
|
}
|
|
});
|
|
child.unref();
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|