diff --git a/packages/cli/src/commands/cloudrun.ts b/packages/cli/src/commands/cloudrun.ts index e95ecf7083..8973b640b4 100644 --- a/packages/cli/src/commands/cloudrun.ts +++ b/packages/cli/src/commands/cloudrun.ts @@ -31,6 +31,7 @@ import { import { normalizeErrorMessage } from "../utils/errorMessage.js"; import { defaultCloudFps } from "../utils/compositionFps.js"; import { DISTRIBUTED_FPS, type DistributedFps, isDistributedFps } from "@hyperframes/core"; +import { isMissingPackageError, missingPackageMessage } from "../utils/missingPackage.js"; export const examples: Example[] = [ ["Deploy the Cloud Run render stack", "hyperframes cloudrun deploy --project my-gcp-project"], @@ -101,20 +102,11 @@ function loadCloudRunAdapter(): Promise { } export function isMissingCloudRunAdapterError(error: unknown): boolean { - return ( - (error as NodeJS.ErrnoException)?.code === "ERR_MODULE_NOT_FOUND" && - normalizeErrorMessage(error).includes("@hyperframes/gcp-cloud-run") - ); + return isMissingPackageError(error, "@hyperframes/gcp-cloud-run"); } export function missingCloudRunAdapterMessage(subcommand: string): string { - return ( - `${c.error("@hyperframes/gcp-cloud-run is not installed.")} The ${c.accent(`hyperframes cloudrun ${subcommand}`)} command needs it at runtime.\n` + - `Install it alongside the CLI:\n` + - ` ${c.accent("npm install -g @hyperframes/gcp-cloud-run")}\n` + - `Or, for an opt-in project setup:\n` + - ` ${c.accent("npm install @hyperframes/gcp-cloud-run")}` - ); + return missingPackageMessage("@hyperframes/gcp-cloud-run", `hyperframes cloudrun ${subcommand}`); } export default defineCommand({ diff --git a/packages/cli/src/commands/lambda.ts b/packages/cli/src/commands/lambda.ts index 7a78a9fd95..b60edecd71 100644 --- a/packages/cli/src/commands/lambda.ts +++ b/packages/cli/src/commands/lambda.ts @@ -16,6 +16,7 @@ import { parseOutputResolutionFlag } from "../utils/parseOutputResolution.js"; import type { Example } from "./_examples.js"; import { c } from "../ui/colors.js"; import { defaultCloudFps } from "../utils/compositionFps.js"; +import { isMissingPackageError, missingPackageMessage } from "../utils/missingPackage.js"; export const examples: Example[] = [ ["Deploy the Lambda render stack to AWS", "hyperframes lambda deploy"], @@ -239,13 +240,9 @@ export default defineCommand({ try { await import("@hyperframes/aws-lambda/sdk"); } catch (err) { - if ((err as NodeJS.ErrnoException).code === "ERR_MODULE_NOT_FOUND") { + if (isMissingPackageError(err, "@hyperframes/aws-lambda")) { console.error( - `${c.error("@hyperframes/aws-lambda is not installed.")} The ${c.accent(`hyperframes lambda ${subcommand}`)} command needs it at runtime.\n` + - `Install it alongside the CLI:\n` + - ` ${c.accent("npm install -g @hyperframes/aws-lambda")}\n` + - `Or, for an opt-in dev setup:\n` + - ` ${c.accent("npm install @hyperframes/aws-lambda")}`, + missingPackageMessage("@hyperframes/aws-lambda", `hyperframes lambda ${subcommand}`), ); failCommand(); } diff --git a/packages/cli/src/utils/missingPackage.test.ts b/packages/cli/src/utils/missingPackage.test.ts new file mode 100644 index 0000000000..2c8bd73edc --- /dev/null +++ b/packages/cli/src/utils/missingPackage.test.ts @@ -0,0 +1,35 @@ +import { describe, expect, it } from "vitest"; +import { isMissingPackageError, missingPackageMessage } from "./missingPackage.js"; + +const notFound = (message: string) => + Object.assign(new Error(message), { code: "ERR_MODULE_NOT_FOUND" }); + +describe("isMissingPackageError", () => { + it("is true when the package itself is absent", () => { + const err = notFound("Cannot find package '@hyperframes/aws-lambda' imported from /app/cli.js"); + expect(isMissingPackageError(err, "@hyperframes/aws-lambda")).toBe(true); + }); + + it("is false when an installed package is missing one of its own dependencies", () => { + const err = notFound( + "Cannot find package 'google-auth-library' imported from /app/node_modules/@hyperframes/gcp-cloud-run/dist/sdk.js", + ); + expect(isMissingPackageError(err, "@hyperframes/gcp-cloud-run")).toBe(false); + }); + + it("is false for other error codes", () => { + const err = Object.assign(new Error("Cannot find package '@hyperframes/aws-lambda'"), { + code: "ERR_UNKNOWN_FILE_EXTENSION", + }); + expect(isMissingPackageError(err, "@hyperframes/aws-lambda")).toBe(false); + }); +}); + +describe("missingPackageMessage", () => { + it("names the command and both install commands", () => { + const message = missingPackageMessage("@hyperframes/aws-lambda", "hyperframes lambda deploy"); + expect(message).toContain("hyperframes lambda deploy"); + expect(message).toContain("npm install -g @hyperframes/aws-lambda"); + expect(message).toContain("npm install @hyperframes/aws-lambda"); + }); +}); diff --git a/packages/cli/src/utils/missingPackage.ts b/packages/cli/src/utils/missingPackage.ts new file mode 100644 index 0000000000..dc2d8bab2f --- /dev/null +++ b/packages/cli/src/utils/missingPackage.ts @@ -0,0 +1,23 @@ +import { c } from "../ui/colors.js"; + +/** + * True only when `pkg` itself is absent. Node's message for a missing dependency of an + * installed `pkg` also names `pkg` (in the importer path), so match the quoted specifier. + */ +export function isMissingPackageError(error: unknown, pkg: string): boolean { + return ( + (error as NodeJS.ErrnoException)?.code === "ERR_MODULE_NOT_FOUND" && + error instanceof Error && + error.message.includes(`Cannot find package '${pkg}'`) + ); +} + +export function missingPackageMessage(pkg: string, command: string): string { + return ( + `${c.error(`${pkg} is not installed.`)} The ${c.accent(command)} command needs it at runtime.\n` + + `Install it alongside the CLI:\n` + + ` ${c.accent(`npm install -g ${pkg}`)}\n` + + `Or, for an opt-in project setup:\n` + + ` ${c.accent(`npm install ${pkg}`)}` + ); +}