import assert from "node:assert/strict"; import { copyFileSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"; import { join } from "node:path"; const dependencyFields = ["dependencies", "optionalDependencies", "peerDependencies"]; function publishedSpecifier(specifier, name, { catalog, versions }) { if (specifier === "catalog:") { assert(catalog[name], `Missing catalog entry for ${name}`); return catalog[name]; } assert(versions[name], `Missing workspace version for ${name}`); if (specifier === "workspace:^") return `^${versions[name]}`; if (specifier === "workspace:*") return versions[name]; throw new Error(`Unsupported packed workspace specifier for ${name}: ${specifier}`); } export function manifestForFrozenInstall(packedManifest, sourceManifest, context) { assert.equal(packedManifest.name, sourceManifest.name, "Packed package name mismatch"); assert.equal(packedManifest.version, sourceManifest.version, "Packed package version mismatch"); const installerManifest = { ...packedManifest, devDependencies: sourceManifest.devDependencies }; for (const field of dependencyFields) { for (const [name, specifier] of Object.entries(sourceManifest[field] ?? {})) { if (!specifier.startsWith("workspace:") && !specifier.startsWith("catalog:")) continue; assert.equal( packedManifest[field]?.[name], publishedSpecifier(specifier, name, context), `${packedManifest.name} packed ${field} entry ${name} differs from the workspace specifier`, ); installerManifest[field] = { ...installerManifest[field], [name]: specifier }; } } return installerManifest; } function stageImporter(installDir, pkg, context, run) { const packageDir = join(installDir, "packages", pkg.directory); mkdirSync(packageDir, { recursive: true }); run("tar", ["-xf", pkg.tarball, "--strip-components=1", "-C", packageDir], installDir); const path = join(packageDir, "package.json"); const packedBytes = readFileSync(path); const installerManifest = manifestForFrozenInstall(JSON.parse(packedBytes), pkg.manifest, context); writeFileSync(path, JSON.stringify(installerManifest)); return { path, packedBytes }; } export function installLockedPackedCli({ workspaceRoot, installDir, tarballs, semantic, run, env = process.env, }) { mkdirSync(installDir, { recursive: true }); for (const file of ["package.json", "pnpm-workspace.yaml", "pnpm-lock.yaml", ".npmrc"]) { copyFileSync(join(workspaceRoot, file), join(installDir, file)); } // The lexical historical fixture only needs the embeddings manifest to // validate the version declared by the CLI. It does not fetch that tarball. const directories = new Set([...Object.keys(tarballs), "embeddings"]); const packages = [...directories].map((directory) => ({ directory, tarball: tarballs[directory], manifest: JSON.parse(readFileSync(join(workspaceRoot, "packages", directory, "package.json"), "utf8")), })); const context = { versions: Object.fromEntries(packages.map(({ manifest }) => [manifest.name, manifest.version])), catalog: JSON.parse(run("pnpm", ["config", "get", "catalog", "--json"], installDir).stdout), }; const selected = packages.filter(({ directory }) => semantic || directory !== "embeddings"); const staged = selected.map((pkg) => stageImporter(installDir, pkg, context, run)); // Hoisting exposes the installed packages and bin at the same paths as the npm consumer. const filters = selected.flatMap(({ manifest }) => ["--filter", manifest.name]); const options = ["--prod", "--frozen-lockfile", "--node-linker=hoisted"]; if (!semantic) options.push("--no-optional"); run("pnpm", [...filters, "install", ...options], installDir, { ...env, ONNXRUNTIME_NODE_INSTALL: "skip", }); // Keep the unpacked release manifests for all runtime probes. for (const { path, packedBytes } of staged) writeFileSync(path, packedBytes); }