import assert from "node:assert/strict"; import { spawnSync } from "node:child_process"; import { createHash } from "node:crypto"; import { mkdirSync, writeFileSync } from "node:fs"; import { join } from "node:path"; import { installLockedPackedCli } from "./locked-packed-cli.mjs"; export const UPGRADE_FROM_VERSION = "0.49.1"; const releaseCommit = "2e74a02b3a8ddadd462d3259b5d5d3474ce7a05e"; const sourceRepository = "https://github.com/kunickiaj/codemem.git"; const registry = "https://registry.npmjs.org/"; // These are the npm tarball SHA-512 values for the published v0.49.1 artifacts. const publishedPackages = { cli: { name: "codemem", integrity: "sha512-SG+S+osECxKKRe6I+rkBZMLKhqQ7WOV5v2o2Sa+h2tHuQA23iQW/MvvlnMI0wl1x2JbBpdt4D8OTSAN+ZsBRLQ==", }, core: { name: "@codemem/core", integrity: "sha512-y5bgDRufFCk1zKKXIPe/bRRQ3pGdWZgUoigIbvHPHy1/LnOLhY575l1VHHh2nd7nOEnvVwGBRhSWWL/G+uFf6w==", }, "mcp-server": { name: "@codemem/mcp", integrity: "sha512-nRLOdX2NPHTFAj/6liu3RuWP+XQvgm4kYcFtER1cuwlIdF+nnFmp9AH+99Gz1CpoeGxY/ib+ITGluGOQdQKk2w==", }, "viewer-server": { name: "@codemem/server", integrity: "sha512-D97mahzI52i4hSdwQ5pNANATAm9NEi1cS3cC9i0RKXOo/svtuWQCVXp9vqB7xqLxWpfJLIQW6rDMDdk18beqSw==", }, }; const releaseManifests = [...Object.keys(publishedPackages), "embeddings"].map( (directory) => `packages/${directory}/package.json`, ); const sourceFiles = [".npmrc", "package.json", "pnpm-workspace.yaml", "pnpm-lock.yaml", ...releaseManifests]; function runInstaller(command, args, cwd, env) { const result = spawnSync(command, args, { cwd, env, encoding: "utf8", timeout: 120_000 }); if (result.status !== 0) { throw new Error( `Historical CLI fixture: ${command} ${args.join(" ")} failed (${result.status})\n${result.error ?? ""}\n${result.stdout}\n${result.stderr}`, ); } return result; } function stageReleaseSource(root, run) { const repository = join(root, "git"); const source = join(root, "source"); const archive = join(root, "source.tar"); mkdirSync(source); // CI checks out shallow history. Fetch the pinned source in a disposable Git repo. run("git", ["init", "--bare", repository], root); run("git", ["fetch", "--depth=1", "--no-tags", sourceRepository, releaseCommit], repository); const fetched = run("git", ["rev-parse", "FETCH_HEAD"], repository).stdout.trim(); assert.equal(fetched, releaseCommit, "Historical CLI fixture fetched the wrong source commit"); run("git", ["archive", "--output", archive, fetched, ...sourceFiles], repository); run("tar", ["-xf", archive, "-C", source], root); return source; } export async function downloadPublishedTarball({ name, integrity }, destination, fetchArtifact = fetch) { const basename = name.split("/").at(-1); const specifier = `${name}@${UPGRADE_FROM_VERSION}`; const url = `${registry}${name}/-/${basename}-${UPGRADE_FROM_VERSION}.tgz`; const response = await fetchArtifact(url, { redirect: "error", signal: AbortSignal.timeout(60_000) }); assert(response.ok, `Downloading ${specifier} failed: HTTP ${response.status}`); const bytes = Buffer.from(await response.arrayBuffer()); const actual = `sha512-${createHash("sha512").update(bytes).digest("base64")}`; assert.equal(actual, integrity, `Published tarball integrity mismatch: ${specifier}`); writeFileSync(destination, bytes); } export async function installPublishedRelease(isolatedRoot) { const root = join(isolatedRoot, "published-cli"); const home = join(root, "home"); mkdirSync(home, { recursive: true }); const env = { PATH: process.env.PATH, HOME: home, USERPROFILE: home, XDG_CONFIG_HOME: join(home, "config"), XDG_DATA_HOME: join(home, "data"), XDG_CACHE_HOME: join(home, "cache"), CI: "1", GIT_CONFIG_GLOBAL: process.platform === "win32" ? "NUL" : "/dev/null", GIT_CONFIG_NOSYSTEM: "1", GIT_TERMINAL_PROMPT: "0", }; for (const name of ["TMPDIR", "TEMP", "TMP", "SYSTEMROOT", "COMSPEC", "PATHEXT"]) { if (process.env[name]) env[name] = process.env[name]; } const run = (command, args, cwd, childEnv = env) => runInstaller(command, args, cwd, childEnv); const workspaceRoot = stageReleaseSource(root, run); const tarballs = Object.fromEntries(await Promise.all( Object.entries(publishedPackages).map(async ([directory, pkg]) => { const destination = join(root, `${directory}.tgz`); await downloadPublishedTarball(pkg, destination); return [directory, destination]; }), )); const installDir = join(isolatedRoot, `upgrade-from-${UPGRADE_FROM_VERSION}`); installLockedPackedCli({ workspaceRoot, installDir, tarballs, semantic: false, run, env }); return installDir; }