feat(update): update Bun global installations (#1735)
* feat(update): update Bun global installations `rea update` only recognized npm global installations and looked up releases with `npm view`, so a `bun add --global rea-agents` install on a machine without npm failed with `spawn npm ENOENT`. Detect the running package inside Bun's global directory (BUN_INSTALL_GLOBAL_DIR, $BUN_INSTALL/install/global, $XDG_CACHE_HOME/.bun/install/global, ~/.bun/install/global), resolve the latest release with `bun info`, install with `bun add --global --ignore-scripts`, then verify the installed entry point and plan integration maintenance exactly as for npm. Bun commands run inside the owning global directory with BUN_INSTALL_GLOBAL_DIR pinned, so the caller's current project is never selected. A global directory without package.json is not treated as an owned installation: Bun then installs into the nearest ancestor project. npm installations keep their existing commands and messages. * fix(update): gate Bun self-updates on supported CLI versions --------- Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
+53
-22
@@ -7,34 +7,47 @@ export { isUpdateFailure } from "./UpdateFailure.js";
|
||||
|
||||
/** Global npm installation that owns the running package and its entry point. */
|
||||
export interface NpmInstallation {
|
||||
readonly manager: "npm";
|
||||
readonly prefix: string;
|
||||
readonly packageRoot: string;
|
||||
}
|
||||
|
||||
/** Bun global installation that owns the running package and its entry point. */
|
||||
export interface BunInstallation {
|
||||
readonly manager: "bun";
|
||||
/** Bun's global project directory, which owns `node_modules/rea-agents`. */
|
||||
readonly globalDirectory: string;
|
||||
readonly packageRoot: string;
|
||||
}
|
||||
|
||||
/** Global package-manager installation that owns the running package. */
|
||||
export type UpdateInstallation = NpmInstallation | BunInstallation;
|
||||
|
||||
/** Output policy for the package manager subprocess. */
|
||||
export type UpdateOutput = "human" | "structured";
|
||||
|
||||
/** Why release lookup failed before a version could be compared. */
|
||||
export type ReleaseLookupFailure =
|
||||
| { readonly kind: "unavailable"; readonly detail: string }
|
||||
| { readonly kind: "unsupported-package-manager"; readonly detail: string }
|
||||
| { readonly kind: "invalid-metadata"; readonly detail: string };
|
||||
|
||||
/** Effects used to update and verify one identified REA installation. */
|
||||
export interface UpdateHost {
|
||||
installation(): Promise<NpmInstallation | undefined>;
|
||||
installation(): Promise<UpdateInstallation | undefined>;
|
||||
latestVersion(
|
||||
installation: NpmInstallation | undefined,
|
||||
installation: UpdateInstallation | undefined,
|
||||
): Promise<Result<string, ReleaseLookupFailure>>;
|
||||
installVersion(
|
||||
installation: NpmInstallation,
|
||||
installation: UpdateInstallation,
|
||||
version: string,
|
||||
output: UpdateOutput,
|
||||
): Promise<Result<void, string>>;
|
||||
installedVersion(
|
||||
installation: NpmInstallation,
|
||||
installation: UpdateInstallation,
|
||||
): Promise<Result<string, string>>;
|
||||
planMaintenance(
|
||||
installation: NpmInstallation,
|
||||
installation: UpdateInstallation,
|
||||
): Promise<IntegrationMaintenance>;
|
||||
}
|
||||
|
||||
@@ -44,14 +57,14 @@ export type UpdateResult =
|
||||
readonly status: "current";
|
||||
readonly currentVersion: string;
|
||||
readonly latestVersion: string;
|
||||
readonly installMethod: "npm" | "unknown";
|
||||
readonly installMethod: UpdateInstallation["manager"] | "unknown";
|
||||
}
|
||||
| {
|
||||
readonly status: "updated";
|
||||
readonly previousVersion: string;
|
||||
readonly latestVersion: string;
|
||||
readonly installedVersion: string;
|
||||
readonly installMethod: "npm";
|
||||
readonly installMethod: UpdateInstallation["manager"];
|
||||
readonly command: readonly string[];
|
||||
readonly maintenance: IntegrationMaintenance;
|
||||
readonly remediation: string;
|
||||
@@ -69,19 +82,35 @@ export type UpdateResult =
|
||||
readonly remediation: string;
|
||||
};
|
||||
|
||||
/** Resolve the exact npm command for the identified prefix and release. */
|
||||
/** Resolve the exact package-manager command for the identified installation and release. */
|
||||
export const updateInstallCommand = (
|
||||
installation: NpmInstallation,
|
||||
installation: UpdateInstallation,
|
||||
version: string,
|
||||
): readonly string[] => [
|
||||
"npm",
|
||||
"install",
|
||||
"--global",
|
||||
"--ignore-scripts",
|
||||
"--prefix",
|
||||
installation.prefix,
|
||||
`${PRODUCT_IDENTITY.packageName}@${version}`,
|
||||
];
|
||||
): readonly string[] => {
|
||||
const release = `${PRODUCT_IDENTITY.packageName}@${version}`;
|
||||
const manager = installation.manager;
|
||||
switch (manager) {
|
||||
case "npm":
|
||||
return [
|
||||
"npm",
|
||||
"install",
|
||||
"--global",
|
||||
"--ignore-scripts",
|
||||
"--prefix",
|
||||
installation.prefix,
|
||||
release,
|
||||
];
|
||||
case "bun":
|
||||
// The host pins Bun's global directory and working directory for this command.
|
||||
return ["bun", "add", "--global", "--ignore-scripts", release];
|
||||
default: {
|
||||
const exhaustive: never = manager;
|
||||
throw new TypeError(
|
||||
`Unhandled installation manager: ${String(exhaustive)}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const versionCheckRemediation = (
|
||||
release: Result<string, ReleaseLookupFailure>,
|
||||
@@ -92,6 +121,8 @@ const versionCheckRemediation = (
|
||||
const failure = release.error;
|
||||
const kind = failure.kind;
|
||||
switch (kind) {
|
||||
case "unsupported-package-manager":
|
||||
return failure.detail;
|
||||
case "unavailable":
|
||||
return `REA could not resolve the latest release: ${failure.detail}. Retry rea update when registry access is available.`;
|
||||
case "invalid-metadata":
|
||||
@@ -128,7 +159,7 @@ export const runUpdate = async (
|
||||
status: "current",
|
||||
currentVersion,
|
||||
latestVersion: targetVersion,
|
||||
installMethod: installation === undefined ? "unknown" : "npm",
|
||||
installMethod: installation?.manager ?? "unknown",
|
||||
};
|
||||
if (installation === undefined)
|
||||
return {
|
||||
@@ -150,7 +181,7 @@ export const runUpdate = async (
|
||||
currentVersion,
|
||||
latestVersion: targetVersion,
|
||||
reason: "install",
|
||||
remediation: `REA could not install ${targetVersion} in ${installation.prefix}: ${installed.error}. Repair this npm installation, then rerun rea update.`,
|
||||
remediation: `REA could not install ${targetVersion} in ${installation.manager === "npm" ? installation.prefix : installation.globalDirectory}: ${installed.error}. Repair this ${installation.manager} installation, then rerun rea update.`,
|
||||
};
|
||||
const verified = await host.installedVersion(installation);
|
||||
if (!verified.ok || verified.value !== targetVersion)
|
||||
@@ -160,7 +191,7 @@ export const runUpdate = async (
|
||||
latestVersion: targetVersion,
|
||||
reason: "verification",
|
||||
...(verified.ok ? { installedVersion: verified.value } : {}),
|
||||
remediation: `npm completed, but REA could not verify ${targetVersion} in ${installation.packageRoot}: ${verified.ok ? `the executable reported ${verified.value}` : verified.error}. The installation may have changed; repair it before retrying.`,
|
||||
remediation: `${installation.manager} completed, but REA could not verify ${targetVersion} in ${installation.packageRoot}: ${verified.ok ? `the executable reported ${verified.value}` : verified.error}. The installation may have changed; repair it before retrying.`,
|
||||
};
|
||||
const maintenance = await host.planMaintenance(installation);
|
||||
return {
|
||||
@@ -168,7 +199,7 @@ export const runUpdate = async (
|
||||
previousVersion: currentVersion,
|
||||
latestVersion: targetVersion,
|
||||
installedVersion: verified.value,
|
||||
installMethod: "npm",
|
||||
installMethod: installation.manager,
|
||||
command: updateInstallCommand(installation, targetVersion),
|
||||
maintenance,
|
||||
remediation:
|
||||
|
||||
Reference in New Issue
Block a user