Fix Homebrew update detection and skill guidance
This commit is contained in:
1 parent
53453cf512
commit
48ebccd086
10 files changed
+274
-222
No files matched your search
@@ -1,5 +1,6 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { readFileSync } from "node:fs";
|
||||
import { mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { compareSemVer, getMirrorMode, isHomebrewInstall, isNewerVersion, matchAssetForPlatform } from "./update.js";
|
||||
|
||||
@@ -187,20 +188,31 @@ describe("update command bgSave and cache integration", () => {
|
||||
});
|
||||
|
||||
describe("isHomebrewInstall", () => {
|
||||
test("detects Homebrew Cellar paths on Apple Silicon and Intel macOS", () => {
|
||||
expect(isHomebrewInstall("/opt/homebrew/Cellar/seal-use/0.10.33/bin/seal-use")).toBe(true);
|
||||
expect(isHomebrewInstall("/usr/local/Cellar/seal-use/0.10.33/bin/seal-use")).toBe(true);
|
||||
expect(isHomebrewInstall("/opt/homebrew/bin/seal-use")).toBe(true);
|
||||
test("requires the executable to resolve inside Homebrew's registered formula prefix", () => {
|
||||
const root = mkdtempSync(join(tmpdir(), "seal-use-homebrew-detect-"));
|
||||
try {
|
||||
const formulaPrefix = join(root, "opt", "seal-use");
|
||||
const cellarBin = join(root, "Cellar", "seal-use", "0.10.33", "bin");
|
||||
const nativeBin = join(root, "bin");
|
||||
const formulaExecutable = join(cellarBin, "seal-use");
|
||||
const nativeExecutable = join(nativeBin, "seal-use");
|
||||
|
||||
mkdirSync(cellarBin, { recursive: true });
|
||||
mkdirSync(nativeBin, { recursive: true });
|
||||
mkdirSync(join(root, "opt"), { recursive: true });
|
||||
writeFileSync(formulaExecutable, "homebrew binary");
|
||||
writeFileSync(nativeExecutable, "native binary");
|
||||
symlinkSync(join(root, "Cellar", "seal-use", "0.10.33"), formulaPrefix);
|
||||
|
||||
expect(isHomebrewInstall(formulaExecutable, formulaPrefix)).toBe(true);
|
||||
expect(isHomebrewInstall(nativeExecutable, formulaPrefix)).toBe(false);
|
||||
} finally {
|
||||
rmSync(root, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("detects Homebrew Cellar paths on Linuxbrew", () => {
|
||||
expect(isHomebrewInstall("/home/linuxbrew/.linuxbrew/Cellar/seal-use/0.10.33/bin/seal-use")).toBe(true);
|
||||
});
|
||||
|
||||
test("returns false for non-Homebrew paths", () => {
|
||||
expect(isHomebrewInstall("/usr/local/bin/seal-use")).toBe(false);
|
||||
expect(isHomebrewInstall("/Users/alice/.local/bin/seal-use")).toBe(false);
|
||||
expect(isHomebrewInstall("/tmp/seal-use")).toBe(false);
|
||||
test("does not infer Homebrew from a matching directory name alone", () => {
|
||||
expect(isHomebrewInstall("/opt/homebrew/bin/seal-use", "/tmp/not-installed-seal-use")).toBe(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
+86
-44
@@ -1,5 +1,5 @@
|
||||
import { chmodSync, existsSync, mkdirSync, readdirSync, readFileSync, realpathSync, renameSync, rmSync, unlinkSync, writeFileSync } from "node:fs";
|
||||
import { basename, dirname, join } from "node:path";
|
||||
import { basename, dirname, isAbsolute, join, relative } from "node:path";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { isCompiledBinary } from "../../core/runtime.js";
|
||||
import { readInstalledSkillVersion } from "../../core/embedded-skill.js";
|
||||
@@ -136,7 +136,7 @@ export async function handleUpdateCommand(runtime: CliRuntime, options: UpdateOp
|
||||
releaseNotes: latestRelease?.notes ?? "",
|
||||
releaseUrl: latestRelease?.htmlUrl ?? "",
|
||||
mode: "source",
|
||||
message: "Running in source development mode. Run 'seal-use update' to git pull & bun install."
|
||||
message: "当前运行的是开发版。正式用户请安装正式版后使用 seal-use update。"
|
||||
});
|
||||
return;
|
||||
}
|
||||
@@ -147,7 +147,7 @@ export async function handleUpdateCommand(runtime: CliRuntime, options: UpdateOp
|
||||
try {
|
||||
runCommand("git", ["-C", runtime.appRoot, "pull", "--ff-only"]);
|
||||
} catch (err) {
|
||||
console.error(`git pull skipped: ${err instanceof Error ? err.message : String(err)}`);
|
||||
console.error(`⚠️ 开发版更新同步失败,继续刷新本地依赖: ${err instanceof Error ? err.message : String(err)}`);
|
||||
}
|
||||
runCommand("bun", ["install"], { cwd: runtime.appRoot });
|
||||
|
||||
@@ -212,6 +212,18 @@ export async function handleUpdateCommand(runtime: CliRuntime, options: UpdateOp
|
||||
return;
|
||||
}
|
||||
|
||||
if (targetVersion && !options.checkOnly) {
|
||||
printJson({
|
||||
updated: false,
|
||||
currentVersion: VERSION,
|
||||
targetVersion,
|
||||
latestVersion,
|
||||
mode: "homebrew",
|
||||
message: "Homebrew-managed installations follow the formula version and do not support selecting a historical release. Use Homebrew directly for version pinning."
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (options.checkOnly) {
|
||||
printJson({
|
||||
updated: false,
|
||||
@@ -390,21 +402,34 @@ function detectArch(): "arm64" | "x64" {
|
||||
return process.arch === "arm64" ? "arm64" : "x64";
|
||||
}
|
||||
|
||||
export function isHomebrewInstall(execPath = process.execPath): boolean {
|
||||
function homebrewFormulaPrefix(): string | undefined {
|
||||
try {
|
||||
const resolved = realpathSync(execPath);
|
||||
return (
|
||||
resolved.includes("/Cellar/seal-use/") ||
|
||||
resolved.includes("/homebrew/Cellar/seal-use/") ||
|
||||
resolved.includes("/.linuxbrew/Cellar/seal-use/") ||
|
||||
execPath.includes("/Cellar/seal-use/") ||
|
||||
execPath.includes("/homebrew/bin/seal-use")
|
||||
);
|
||||
const result = spawnSync("brew", ["--prefix", "seal-use"], {
|
||||
stdio: ["ignore", "pipe", "ignore"],
|
||||
encoding: "utf-8"
|
||||
});
|
||||
if (result.status !== 0 || typeof result.stdout !== "string") return undefined;
|
||||
const prefix = result.stdout.trim();
|
||||
return prefix || undefined;
|
||||
} catch {
|
||||
return (
|
||||
execPath.includes("/Cellar/seal-use/") ||
|
||||
execPath.includes("/homebrew/bin/seal-use")
|
||||
);
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
export function isHomebrewInstall(
|
||||
execPath = process.execPath,
|
||||
formulaPrefix = homebrewFormulaPrefix()
|
||||
): boolean {
|
||||
if (!formulaPrefix) return false;
|
||||
|
||||
try {
|
||||
const resolvedExec = realpathSync(execPath);
|
||||
const resolvedPrefix = realpathSync(formulaPrefix);
|
||||
const fromFormula = relative(resolvedPrefix, resolvedExec);
|
||||
const parentPrefix = `..${process.platform === "win32" ? "\\" : "/"}`;
|
||||
return Boolean(fromFormula && fromFormula !== ".." && !fromFormula.startsWith(parentPrefix) && !isAbsolute(fromFormula));
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -480,37 +505,54 @@ async function handleHomebrewUpdate(
|
||||
const wasRunning = serviceStatus(serviceConfig).running;
|
||||
if (wasRunning) await runServiceCommand("stop", serviceConfig);
|
||||
|
||||
// 1. Fast update of the dedicated tap
|
||||
const tapUpdate = spawnSync("brew", ["update", "yunyingk/tap"], {
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
encoding: "utf-8",
|
||||
env: { ...process.env, NONINTERACTIVE: "1" }
|
||||
});
|
||||
let alreadyLatest = false;
|
||||
let updateError: unknown;
|
||||
try {
|
||||
// 1. Fast update of the dedicated tap
|
||||
const tapUpdate = spawnSync("brew", ["update", "yunyingk/tap"], {
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
encoding: "utf-8",
|
||||
env: { ...process.env, NONINTERACTIVE: "1" }
|
||||
});
|
||||
|
||||
if (tapUpdate.status !== 0) {
|
||||
const msg = tapUpdate.stderr?.trim() || tapUpdate.stdout?.trim();
|
||||
if (msg) console.error(`⚠️ brew update yunyingk/tap 提示: ${msg}`);
|
||||
if (tapUpdate.status !== 0) {
|
||||
const msg = tapUpdate.stderr?.trim() || tapUpdate.stdout?.trim();
|
||||
if (msg) console.error(`⚠️ brew update yunyingk/tap 提示: ${msg}`);
|
||||
}
|
||||
|
||||
// 2. Upgrade the formula
|
||||
const upgradeRes = spawnSync("brew", ["upgrade", "seal-use"], {
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
encoding: "utf-8",
|
||||
env: { ...process.env, NONINTERACTIVE: "1" }
|
||||
});
|
||||
|
||||
const outCombined = `${upgradeRes.stdout || ""} ${upgradeRes.stderr || ""}`;
|
||||
alreadyLatest =
|
||||
outCombined.includes("already installed") || outCombined.includes("latest version already installed");
|
||||
|
||||
if (upgradeRes.status !== 0 && !alreadyLatest) {
|
||||
throw new Error(
|
||||
`brew upgrade seal-use failed (exit ${upgradeRes.status}): ${upgradeRes.stderr?.trim() || upgradeRes.stdout?.trim()}`
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
updateError = err;
|
||||
throw err;
|
||||
} finally {
|
||||
if (wasRunning) {
|
||||
try {
|
||||
await runServiceCommand("start", serviceConfig);
|
||||
} catch (startError) {
|
||||
if (updateError) {
|
||||
console.error(`⚠️ 更新失败后服务恢复启动也失败: ${startError instanceof Error ? startError.message : String(startError)}`);
|
||||
} else {
|
||||
throw startError;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Upgrade the formula
|
||||
const upgradeRes = spawnSync("brew", ["upgrade", "seal-use"], {
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
encoding: "utf-8",
|
||||
env: { ...process.env, NONINTERACTIVE: "1" }
|
||||
});
|
||||
|
||||
const outCombined = `${upgradeRes.stdout || ""} ${upgradeRes.stderr || ""}`;
|
||||
const alreadyLatest =
|
||||
outCombined.includes("already installed") || outCombined.includes("latest version already installed");
|
||||
|
||||
if (upgradeRes.status !== 0 && !alreadyLatest) {
|
||||
throw new Error(
|
||||
`brew upgrade seal-use failed (exit ${upgradeRes.status}): ${upgradeRes.stderr?.trim() || upgradeRes.stdout?.trim()}`
|
||||
);
|
||||
}
|
||||
|
||||
if (wasRunning) await runServiceCommand("start", serviceConfig);
|
||||
|
||||
// Refresh installed skill through new binary
|
||||
try {
|
||||
spawnSync("seal-use", ["skill", "install"], { stdio: "ignore" });
|
||||
|
||||
+1
-1
@@ -273,7 +273,7 @@ export const HELP_REGISTRY: AreaHelp[] = [
|
||||
{
|
||||
name: "push",
|
||||
summary: "Push rules from local JSON file to enterprise draft space",
|
||||
usage: ["seal-use rules push --file <path> [--mode replace|incremental] [--yes] [--corp <corpId>]"]
|
||||
usage: ["seal-use rules push --file <path> [--publish] [--version-name <name>] [--dry-run|--yes] [--corp <corpId>]"]
|
||||
}
|
||||
]
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user