Clone any viral video with AI agents. Not just a script, the whole workflow: swap the face, the words, the B-roll, ship 100 variants in one command, and get your 100M views.
TypeScript agentic-aiaiai-agentsai-videocompilerdslffmpeggenerative-aillmmarkup-languagemonorepoplugin-systemprogramming-languagetext-to-videotypescriptvideovideo-automationvideo-clonevideo-editingvideo-generation
fix(cli): resolve packages status the way a Build resolves (#361)
Sharky Wang32baae6dfd9c
diff --git a/packages/cli/src/commands/environment.ts b/packages/cli/src/commands/environment.tsindex 4ea348e9..e6751aff 100644--- a/packages/cli/src/commands/environment.ts+++ b/packages/cli/src/commands/environment.ts@@ -1,8 +1,9 @@import { readFile } from "node:fs/promises";-import { resolve } from "node:path";+import { join, resolve } from "node:path";+import { distributionPackageDeclaring, locateNodePackage } from "@hypit/package-loader-node";import type { NodeRuntimeHost } from "@hypit/runtime-host-node";-import { hypitHostPackageRoot, inspectHostPackage, prepareHostPackages } from "@hypit/runtime-host-node";+import { hypitHostPackageRoot, inspectHostPackage, parseRegistryPackageSpec, prepareHostPackages } from "@hypit/runtime-host-node";import type { CliCommand, EnvironmentCommand } from "../command.js";import { commandHint } from "../command-hint.js";@@ -14,6 +15,68 @@ import { hypitHostStateRoot, hypitProjectStateRoot } from "../paths.js";import type { CliManagedProgramProgress, CliManagedProgramReport, CliRuntimeController } from "../runtime-port.js";import type { OperationalWriter } from "./types.js";+type PackageStatus = {+ readonly ready: boolean;+ readonly declaredBy?: string;+ readonly installation?: string;+ readonly installedVersion?: string;+ readonly detail?: string;+};++/**+ * Whether `specifier` will resolve when a Build needs it.+ *+ * The loader finds an external package in two places — the node_modules chain above whoever+ * requires it, and the machine home addressed by the version that requirer declares — and both+ * start from the requiring package. So the requirer is located first and the loader asked once,+ * rather than reimplementing either half here: a second opinion is exactly how this command came+ * to disagree with the thing it reports on.+ *+ * Without a Distribution on disk there is no requirer to find, and the machine home remains the+ * only place this command can speak about.+ */+async function packageStatus(+ specifier: string,+ hostRoot: string,+ distributionRoot: string | undefined,+): Promise<PackageStatus> {+ const required = parseRegistryPackageSpec(specifier);+ if (distributionRoot === undefined) {+ const existing = await inspectHostPackage(specifier, hostRoot);+ return existing === undefined+ ? { ready: false, detail: "not installed in the machine package home" }+ : { ready: true, installation: existing.root, installedVersion: required.version };+ }+ const declaring = distributionPackageDeclaring(distributionRoot, required.name, required.version);+ if (declaring === undefined) {+ return { ready: false, detail: `no Distribution package declares ${specifier}` };+ }+ try {+ const located = locateNodePackage(required.name, {+ from: join(declaring, "__hypit_package_status__.mjs"),+ distributionRoots: [distributionRoot],+ externalRoots: [hostRoot],+ allowExternal: true,+ });+ const installed = located.manifest.version;+ return installed === required.version+ ? { ready: true, declaredBy: declaring, installation: located.root, installedVersion: installed }+ : {+ ready: false,+ declaredBy: declaring,+ installation: located.root,+ ...(installed === undefined ? {} : { installedVersion: installed }),+ detail: `resolved version is ${installed ?? "unknown"}`,+ };+ } catch (error) {+ return {+ ready: false,+ declaredBy: declaring,+ detail: error instanceof Error ? error.message : String(error),+ };+ }+}+function programRecord(item: CliManagedProgramReport) {return {...item,@@ -119,28 +182,51 @@ export async function runEnvironmentCommand(input: {if (args.command === "packages") {const root = hypitHostPackageRoot();- const existing = await inspectHostPackage(args.package, root);- const reports = args.action === "install"- ? await prepareHostPackages([args.package], {+ if (args.action === "install") {+ const reports = await prepareHostPackages([args.package], {root,...(reportPackageProgress === undefined ? {} : { onProgress: reportPackageProgress }),- })- : existing === undefined ? [] : [existing];- const ready = reports.length === 1;+ });+ const ready = reports.length === 1;+ write({+ format: "hypit.cli-package@1",+ action: args.action,+ package: args.package,+ ready,+ ...(reports[0] === undefined ? {} : { installation: reports[0].root }),+ ...(reports[0]?.logPath === undefined ? {} : { logPath: reports[0].logPath }),+ }, "Machine package is ready", ready ? "success" : "warning", [+ ["Package", args.package],+ ["Ready", String(ready)],+ ...(args.presentation.verbose && reports[0] !== undefined ? [["Installation", reports[0].root] as const] : []),+ ]);+ if (!ready) io.setExitCode?.(1);+ return;+ }++ // Status answers the question a Build asks: will the dependency resolve, at+ // the declared version? It therefore asks the loader rather than checking+ // one of the places the loader looks. Reading only the machine home called a+ // package that resolves perfectly well from the Distribution's own+ // node_modules "Ready false", and exited 1 saying so.+ const status = await packageStatus(args.package, root, distribution.packageRoot);write({format: "hypit.cli-package@1",action: args.action,package: args.package,- ready,- ...(reports[0] === undefined ? {} : { installation: reports[0].root }),- ...(reports[0]?.logPath === undefined ? {} : { logPath: reports[0].logPath }),- }, args.action === "install" ? "Machine package is ready" : "Machine package status",- ready ? "success" : "warning", [+ ready: status.ready,+ ...(status.installation === undefined ? {} : { installation: status.installation }),+ ...(status.declaredBy === undefined ? {} : { declaredBy: status.declaredBy }),+ ...(status.installedVersion === undefined ? {} : { installedVersion: status.installedVersion }),+ ...(status.detail === undefined ? {} : { detail: status.detail }),+ }, "Machine package status", status.ready ? "success" : "warning", [["Package", args.package],- ["Ready", String(ready)],- ...(args.presentation.verbose && reports[0] !== undefined ? [["Installation", reports[0].root] as const] : []),+ ["Ready", String(status.ready)],+ ...(status.declaredBy === undefined ? [] : [["Required by", status.declaredBy] as const]),+ ...(status.detail === undefined ? [] : [["Detail", status.detail] as const]),+ ...(status.installation === undefined ? [] : [["Installation", status.installation] as const]),]);- if (!ready) io.setExitCode?.(1);+ if (!status.ready) io.setExitCode?.(1);return;}diff --git a/packages/package-loader-node/src/index.ts b/packages/package-loader-node/src/index.tsindex 9e2469ec..1270b787 100644--- a/packages/package-loader-node/src/index.ts+++ b/packages/package-loader-node/src/index.ts@@ -16,6 +16,7 @@ export {export {locateNodePackage,NodePackageNotFoundError,+ distributionPackageDeclaring,externalPackageInstallRoot,resolveNodePackageExecutable,resolveNodePackageResource,diff --git a/packages/package-loader-node/src/location.ts b/packages/package-loader-node/src/location.tsindex b1afcbf9..256493d4 100644--- a/packages/package-loader-node/src/location.ts+++ b/packages/package-loader-node/src/location.ts@@ -190,6 +190,47 @@ export function distributionPackageDirectory(root: string, name: string): stringreturn undefined;}+/**+ * The Distribution package that declares `name@version` as an external dependency, or undefined+ * when none does.+ *+ * Resolution of an external package starts from whoever requires it — the ancestor walk begins at+ * that package's directory, and the machine home is addressed by the version that package+ * declares. A caller holding only `name@version`, as `hypit packages status` does, cannot ask the+ * question the loader answers until it knows the requirer. Finding it here keeps that one answer+ * in one place instead of letting each caller approximate it.+ */+export function distributionPackageDeclaring(+ root: string,+ name: string,+ version: string,+): string | undefined {+ for (const directory of distributionPackageDirectories(root)) {+ const manifest = join(directory, "package.json");+ if (!existsSync(manifest)) continue;+ const value = JSON.parse(readFileSync(manifest, "utf8")) as {+ dependencies?: Record<string, string>;+ optionalDependencies?: Record<string, string>;+ };+ // npm lets optionalDependencies override dependencies of the same name, and+ // declaredExternalPackageRoot reads them in that order; match it.+ const declared = value.optionalDependencies?.[name] ?? value.dependencies?.[name];+ if (declared === version) return directory;+ }+ return undefined;+}++function* distributionPackageDirectories(root: string): Generator<string> {+ const base = resolve(root);+ for (const group of ["packages", "services"]) {+ const directory = join(base, group);+ if (!existsSync(directory)) continue;+ for (const entry of readdirSync(directory, { withFileTypes: true })) {+ if (entry.isDirectory()) yield join(directory, entry.name);+ }+ }+}+function distributionPackage(root: string, name: string): LocatedNodePackage | undefined {const directory = distributionPackageDirectory(root, name);return directory === undefined ? nodeModulesPackage(root, name) : readPackage(directory, name);