Add dual-mode self-update and multi-target binary build
This commit is contained in:
1 parent
85ca31f4ad
commit
c86ee6a235
4 files changed
+207
-19
No files matched your search
+143
-7
@@ -1,3 +1,4 @@
|
||||
import { chmodSync, renameSync, writeFileSync } from "node:fs";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { isCompiledBinary } from "../../core/runtime.js";
|
||||
import { VERSION } from "../../version.js";
|
||||
@@ -6,7 +7,10 @@ import { printJson } from "../printer.js";
|
||||
import { STATE_DIR } from "./service.js";
|
||||
import type { CliRuntime } from "../types.js";
|
||||
|
||||
export async function handleUpdateCommand(runtime: CliRuntime) {
|
||||
const DEFAULT_GITHUB_REPO = "yunyingk/seal-home";
|
||||
const UPDATE_URL_ENV = "SEAL_HOME_UPDATE_URL";
|
||||
|
||||
export async function handleUpdateCommand(runtime: CliRuntime, options: { checkOnly?: boolean } = {}) {
|
||||
const serviceConfig = {
|
||||
appRoot: runtime.appRoot,
|
||||
entryFile: runtime.entryFile,
|
||||
@@ -14,23 +18,155 @@ export async function handleUpdateCommand(runtime: CliRuntime) {
|
||||
version: VERSION,
|
||||
launcher: runtime.launcher
|
||||
};
|
||||
const wasRunning = serviceStatus(serviceConfig).running;
|
||||
if (wasRunning) await runServiceCommand("stop", serviceConfig);
|
||||
|
||||
if (!isCompiledBinary()) {
|
||||
const wasRunning = serviceStatus(serviceConfig).running;
|
||||
if (wasRunning) await runServiceCommand("stop", serviceConfig);
|
||||
|
||||
runCommand("git", ["-C", runtime.appRoot, "pull", "--ff-only"]);
|
||||
runCommand("bun", ["install"], { cwd: runtime.appRoot });
|
||||
|
||||
if (wasRunning) await runServiceCommand("start", serviceConfig);
|
||||
|
||||
printJson({
|
||||
updated: true,
|
||||
serviceRestarted: wasRunning,
|
||||
version: VERSION,
|
||||
mode: "source"
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (wasRunning) await runServiceCommand("start", serviceConfig);
|
||||
// Standalone binary update
|
||||
const currentPlatform = detectPlatformArch();
|
||||
const updateInfo = await fetchLatestReleaseInfo();
|
||||
|
||||
if (!updateInfo) {
|
||||
printJson({
|
||||
updated: false,
|
||||
version: VERSION,
|
||||
mode: "binary",
|
||||
message: "No update source configured or release server unreachable"
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (isNewerVersion(updateInfo.version, VERSION)) {
|
||||
if (options.checkOnly) {
|
||||
printJson({
|
||||
updated: false,
|
||||
updateAvailable: true,
|
||||
currentVersion: VERSION,
|
||||
latestVersion: updateInfo.version,
|
||||
downloadUrl: updateInfo.downloads[currentPlatform]?.url
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const downloadUrl = updateInfo.downloads[currentPlatform]?.url;
|
||||
if (!downloadUrl) {
|
||||
throw new Error(`No prebuilt binary available for platform: ${currentPlatform} in release ${updateInfo.version}`);
|
||||
}
|
||||
|
||||
const wasRunning = serviceStatus(serviceConfig).running;
|
||||
if (wasRunning) await runServiceCommand("stop", serviceConfig);
|
||||
|
||||
const tmpBinary = `${process.execPath}.tmp.${Date.now()}`;
|
||||
const response = await fetch(downloadUrl);
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to download binary update: ${response.status} ${response.statusText}`);
|
||||
}
|
||||
const buffer = Buffer.from(await response.arrayBuffer());
|
||||
writeFileSync(tmpBinary, buffer);
|
||||
chmodSync(tmpBinary, 0o755);
|
||||
renameSync(tmpBinary, process.execPath);
|
||||
|
||||
if (wasRunning) await runServiceCommand("start", serviceConfig);
|
||||
|
||||
printJson({
|
||||
updated: true,
|
||||
fromVersion: VERSION,
|
||||
toVersion: updateInfo.version,
|
||||
serviceRestarted: wasRunning,
|
||||
mode: "binary"
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
printJson({
|
||||
updated: true,
|
||||
serviceRestarted: wasRunning,
|
||||
version: VERSION
|
||||
updated: false,
|
||||
version: VERSION,
|
||||
mode: "binary",
|
||||
message: "seal-home is already up to date"
|
||||
});
|
||||
}
|
||||
|
||||
function detectPlatformArch(): string {
|
||||
const platform = process.platform === "darwin" ? "darwin" : process.platform === "win32" ? "windows" : "linux";
|
||||
const arch = process.arch === "arm64" ? "arm64" : "x64";
|
||||
return `${platform}-${arch}`;
|
||||
}
|
||||
|
||||
type ReleaseInfo = {
|
||||
version: string;
|
||||
downloads: Record<string, { url: string; sha256?: string }>;
|
||||
};
|
||||
|
||||
async function fetchLatestReleaseInfo(): Promise<ReleaseInfo | undefined> {
|
||||
const customUrl = process.env[UPDATE_URL_ENV]?.trim();
|
||||
if (customUrl) {
|
||||
try {
|
||||
const res = await fetch(customUrl);
|
||||
if (res.ok) {
|
||||
return (await res.json()) as ReleaseInfo;
|
||||
}
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback to GitHub API
|
||||
try {
|
||||
const res = await fetch(`https://api.github.com/repos/${DEFAULT_GITHUB_REPO}/releases/latest`, {
|
||||
headers: {
|
||||
"User-Agent": `seal-home/${VERSION}`
|
||||
}
|
||||
});
|
||||
if (!res.ok) return undefined;
|
||||
const json = (await res.json()) as {
|
||||
tag_name?: string;
|
||||
assets?: Array<{ name: string; browser_download_url: string }>;
|
||||
};
|
||||
const version = (json.tag_name ?? "").replace(/^v/, "");
|
||||
if (!version) return undefined;
|
||||
|
||||
const downloads: Record<string, { url: string }> = {};
|
||||
for (const asset of json.assets ?? []) {
|
||||
for (const target of ["darwin-arm64", "darwin-x64", "linux-x64", "linux-arm64", "windows-x64"]) {
|
||||
if (asset.name.includes(target)) {
|
||||
downloads[target] = { url: asset.browser_download_url };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { version, downloads };
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
function isNewerVersion(latest: string, current: string): boolean {
|
||||
const lParts = latest.split(".").map(Number);
|
||||
const cParts = current.split(".").map(Number);
|
||||
for (let i = 0; i < Math.max(lParts.length, cParts.length); i++) {
|
||||
const l = lParts[i] ?? 0;
|
||||
const c = cParts[i] ?? 0;
|
||||
if (l > c) return true;
|
||||
if (l < c) return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function runCommand(command: string, args: string[], options: { cwd?: string } = {}) {
|
||||
const result = spawnSync(command, args, {
|
||||
cwd: options.cwd,
|
||||
|
||||
+4
-2
@@ -25,7 +25,7 @@ import { handleUpdateCommand } from "./commands/update.js";
|
||||
import { handleUsersCommand } from "./commands/users.js";
|
||||
import { handleWebhookCommand } from "./commands/webhook.js";
|
||||
import { selectCorp } from "./helpers.js";
|
||||
import { parseArgs, stringOption } from "./parser.js";
|
||||
import { booleanOption, parseArgs, stringOption } from "./parser.js";
|
||||
import {
|
||||
formatConsoleOutput,
|
||||
joinOutput,
|
||||
@@ -162,7 +162,9 @@ async function dispatch(argv: string[], runtime: CliRuntime) {
|
||||
}
|
||||
|
||||
if (area === "update") {
|
||||
await handleUpdateCommand(runtime);
|
||||
await handleUpdateCommand(runtime, {
|
||||
checkOnly: booleanOption(args.options.check) ?? booleanOption(args.options["check-only"])
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user