Refine version command with async update cache and background check
This commit is contained in:
1 parent
68a858b4b9
commit
6ad01893b3
6 files changed
+410
-5
No files matched your search
@@ -0,0 +1,108 @@
|
||||
import { existsSync, mkdirSync, readFileSync, renameSync, unlinkSync, 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";
|
||||
|
||||
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 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 ?? new Date().toISOString(),
|
||||
latestVersion: data.latestVersion,
|
||||
publishedAt: data.publishedAt,
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user