Skip to content

Commit 255e3fa

Browse files
committed
fix: support npm 12 pack output [policy]
Change-Id: I35767a804287d2b18c59cb2eeac825ce137794bd
1 parent 9deb215 commit 255e3fa

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { describe, expect, test } from "bun:test";
2+
import { packedFilename } from "./smoke-packed.ts";
3+
4+
describe("npm pack output", () => {
5+
test("accepts the npm 10 and 11 array format", () => {
6+
expect(packedFilename('[{"filename":"sdk.tgz"}]', "sdk")).toBe("sdk.tgz");
7+
});
8+
9+
test("accepts the npm 12 package-map format", () => {
10+
expect(packedFilename('{"@openagentpack/sdk":{"filename":"sdk.tgz"}}', "sdk")).toBe("sdk.tgz");
11+
});
12+
13+
test("rejects ambiguous output", () => {
14+
expect(() => packedFilename("[]", "sdk")).toThrow("Unexpected npm pack output for sdk");
15+
});
16+
});

scripts/release/smoke-packed.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ const root = resolve(import.meta.dirname, "../..");
2020

2121
type PackedPackage = { filename: string };
2222

23+
export function packedFilename(raw: string, pkg: string): string {
24+
const parsed = JSON.parse(raw) as PackedPackage[] | Record<string, PackedPackage>;
25+
const packed = Array.isArray(parsed) ? parsed : Object.values(parsed);
26+
if (packed.length !== 1 || !packed[0]?.filename) throw new Error(`Unexpected npm pack output for ${pkg}`);
27+
return packed[0].filename;
28+
}
29+
2330
function run(command: string[], cwd: string, stdout: "inherit" | "pipe" = "inherit"): string {
2431
const result = Bun.spawnSync(command, { cwd, stdout, stderr: "inherit" });
2532
if (result.exitCode !== 0) throw new Error(`Command failed (${result.exitCode}): ${command.join(" ")}`);
@@ -38,9 +45,7 @@ function packPackages(destination: string): string[] {
3845
licenseStaged = true;
3946
originalManifest = rewriteManifestForPublish(pkgDir, versions);
4047
const raw = run(["npm", "pack", "--json", "--pack-destination", destination], pkgDir, "pipe");
41-
const packed = JSON.parse(raw) as PackedPackage[];
42-
if (packed.length !== 1 || !packed[0]?.filename) throw new Error(`Unexpected npm pack output for ${pkg}`);
43-
return join(destination, packed[0].filename);
48+
return join(destination, packedFilename(raw, pkg));
4449
} finally {
4550
if (originalManifest !== undefined) restorePackageJson(pkgDir, originalManifest);
4651
if (licenseStaged) restoreLicense(pkgDir, originalLicense);

0 commit comments

Comments
 (0)