From e5e9dac119e1fec5b880040be57ed2d7bd2fef76 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sat, 15 Aug 2026 23:44:43 +0000 Subject: [PATCH 1/3] Fix prepareRelease JSON parse of pnpm's pack banner. npm pack --json captured a prepack `pnpm run build` banner on stdout, so JSON.parse threw Unexpected token '>'. Parse from the first JSON token, build and pack through npm, and run tsc in prepack. Bump public packages to 0.1.1 in lockstep after the failed v0.1.0 publish. Co-authored-by: Fly Over Coder --- docs/PROJECT_PLAN.md | 2 +- packages/billing-core/package.json | 4 +- packages/billing-stripe/package.json | 6 +-- pnpm-lock.yaml | 2 +- scripts/release-packages.mjs | 31 +++++++++++----- tests/release-packages.test.ts | 55 +++++++++++++++++++++++++--- 6 files changed, 78 insertions(+), 22 deletions(-) diff --git a/docs/PROJECT_PLAN.md b/docs/PROJECT_PLAN.md index dfe6414..829e3a3 100644 --- a/docs/PROJECT_PLAN.md +++ b/docs/PROJECT_PLAN.md @@ -3,7 +3,7 @@ ## Status **Stage:** Phase 4 — Stripe adapter in-tree. Nothing is -published. (`0.1.0`, unpublished.) +published. (`0.1.1`, unpublished.) **Extraction source:** the RetireGolden account API's subscription ledger — `api/src/lib/repo.js` (`shouldApplyEvent`, `applySubscriptionSnapshot`, diff --git a/packages/billing-core/package.json b/packages/billing-core/package.json index 85102d7..7ac0fae 100644 --- a/packages/billing-core/package.json +++ b/packages/billing-core/package.json @@ -1,6 +1,6 @@ { "name": "@pegma/billing-core", - "version": "0.1.0", + "version": "0.1.1", "description": "Provider-agnostic subscription ledger: event watermark, lifecycle-rank arbitration, declared invariants, checkout reservation, and snapshot reconciliation.", "license": "MIT", "type": "module", @@ -35,6 +35,6 @@ "scripts": { "build": "tsc -p tsconfig.json", "check": "tsc -p tsconfig.json --noEmit", - "prepack": "pnpm run build" + "prepack": "tsc -p tsconfig.json" } } diff --git a/packages/billing-stripe/package.json b/packages/billing-stripe/package.json index af7b903..75522a6 100644 --- a/packages/billing-stripe/package.json +++ b/packages/billing-stripe/package.json @@ -1,6 +1,6 @@ { "name": "@pegma/billing-stripe", - "version": "0.1.0", + "version": "0.1.1", "description": "Stripe event and snapshot translation for the Pegma billing ledger.", "license": "MIT", "type": "module", @@ -29,7 +29,7 @@ "directory": "packages/billing-stripe" }, "dependencies": { - "@pegma/billing-core": "0.1.0", + "@pegma/billing-core": "0.1.1", "@pegma/spine": "0.1.2" }, "devDependencies": { @@ -38,6 +38,6 @@ "scripts": { "build": "tsc -p tsconfig.json", "check": "tsc -p tsconfig.json --noEmit", - "prepack": "pnpm run build" + "prepack": "tsc -p tsconfig.json" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4ad5956..a55b41d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -47,7 +47,7 @@ importers: packages/billing-stripe: dependencies: '@pegma/billing-core': - specifier: 0.1.0 + specifier: 0.1.1 version: link:../billing-core '@pegma/spine': specifier: 0.1.2 diff --git a/scripts/release-packages.mjs b/scripts/release-packages.mjs index 0b998e6..a35487c 100644 --- a/scripts/release-packages.mjs +++ b/scripts/release-packages.mjs @@ -101,11 +101,21 @@ function runNpm(arguments_, options = {}) { }); } -function runPnpm(arguments_, options = {}) { - return run(process.platform === "win32" ? "pnpm.cmd" : "pnpm", arguments_, { - ...options, - shell: process.platform === "win32", - }); +/** + * `npm --json` writes a JSON value. Lifecycle scripts invoked during + * `npm pack` (including a package `prepack` of `pnpm run build`) may + * print a human banner on the same stdout: + * > @pegma/...@0.1.1 build /home/runner/work/... + * Parse from the first JSON token so that banner is never fed to + * JSON.parse. + */ +export function parseNpmJsonStdout(stdout) { + const text = typeof stdout === "string" ? stdout : ""; + const start = text.search(/[\[{]/u); + if (start === -1) { + fail("npm did not write JSON output"); + } + return JSON.parse(text.slice(start)); } function unquoteYamlScalar(value) { @@ -389,7 +399,10 @@ async function validatePackage(root, definition, lockfile) { } if ( typeof manifest.scripts?.prepack !== "string" || - !manifest.scripts.prepack.includes("build") + !( + manifest.scripts.prepack.includes("build") || + manifest.scripts.prepack.includes("tsc") + ) ) { fail(`${definition.name} must build during prepack`); } @@ -671,7 +684,7 @@ function queryRegistryIntegrity(name, version) { allowFailure: true, }); if (result.status === 0) { - const integrity = JSON.parse(result.stdout); + const integrity = parseNpmJsonStdout(result.stdout); if (typeof integrity !== "string" || integrity.length === 0) { fail(`${spec} exists without dist.integrity`); } @@ -716,7 +729,7 @@ export async function prepareRelease(options = {}) { fail(`release output directory must be empty: ${output}`); } - runPnpm(["run", "build"], { cwd: root }); + runNpm(["run", "build"], { cwd: root }); const records = []; const tagVersion = releaseTag?.slice(1); for (const { definition, manifest } of packages) { @@ -730,7 +743,7 @@ export async function prepareRelease(options = {}) { ], { cwd: root, capture: true }, ); - const [packed] = JSON.parse(result.stdout); + const [packed] = parseNpmJsonStdout(result.stdout); if ( packed?.name !== definition.name || packed?.version !== manifest.version || diff --git a/tests/release-packages.test.ts b/tests/release-packages.test.ts index a60cc58..c4069f5 100644 --- a/tests/release-packages.test.ts +++ b/tests/release-packages.test.ts @@ -8,6 +8,7 @@ import { isNormalReleaseVersion, lockDependencyMatches, parseArguments, + parseNpmJsonStdout, parsePnpmLockfileImporters, resolvedVersionSatisfies, validateRepository, @@ -40,7 +41,7 @@ describe("release package metadata", () => { ]); }); - it("ships the unpublished 0.1.0 package with exact Pegma pins", () => { + it("ships the unpublished 0.1.1 package with exact Pegma pins", () => { const manifests = RELEASE_PACKAGES.map(({ directory }) => JSON.parse( readFileSync( @@ -52,17 +53,21 @@ describe("release package metadata", () => { name: string; version: string; dependencies?: Record; + scripts?: { prepack?: string }; }>; expect(manifests.map(({ name, version }) => ({ name, version }))).toEqual([ - { name: "@pegma/billing-core", version: "0.1.0" }, - { name: "@pegma/billing-stripe", version: "0.1.0" }, + { name: "@pegma/billing-core", version: "0.1.1" }, + { name: "@pegma/billing-stripe", version: "0.1.1" }, ]); expect(manifests[0]?.dependencies?.["@pegma/spine"]).toBe("0.1.2"); expect(manifests[0]?.dependencies?.["@pegma/storage-core"]).toBe("0.4.0"); - expect(manifests[1]?.dependencies?.["@pegma/billing-core"]).toBe("0.1.0"); + expect(manifests[1]?.dependencies?.["@pegma/billing-core"]).toBe("0.1.1"); expect(manifests[1]?.dependencies?.["@pegma/spine"]).toBe("0.1.2"); - expect(packageVersion).toBe("0.1.0"); + expect(packageVersion).toBe("0.1.1"); + for (const manifest of manifests) { + expect(manifest.scripts?.prepack).toBe("tsc -p tsconfig.json"); + } }); it("rejects the bootstrap range from the normal release lane", () => { @@ -134,7 +139,7 @@ packages: expect( lockDependencyMatches( live["packages/billing-stripe"]?.dependencies?.["@pegma/billing-core"], - "0.1.0", + "0.1.1", { workspace: true }, ), ).toBe(true); @@ -202,6 +207,44 @@ packages: expect(source).toMatch( /function runNpm\([\s\S]*?process\.platform === "win32" \? "npm\.cmd" : "npm"/u, ); + expect(source).toMatch(/runNpm\(\["run", "build"\]/u); + expect(source).not.toMatch(/runPnpm\(\["run", "build"\]/u); + }); + + it("does not JSON.parse pnpm's human script banner during prepareRelease", () => { + const source = readFileSync( + join(process.cwd(), "scripts/release-packages.mjs"), + "utf8", + ); + const prepareStart = source.indexOf("export async function prepareRelease"); + const prepareEnd = source.indexOf( + "async function verifyPreparedManifest", + prepareStart, + ); + expect(prepareStart).toBeGreaterThanOrEqual(0); + expect(prepareEnd).toBeGreaterThan(prepareStart); + const prepare = source.slice(prepareStart, prepareEnd); + expect(prepare).not.toMatch(/JSON\.parse\(\s*result\.stdout/u); + expect(prepare).toMatch(/parseNpmJsonStdout\(\s*result\.stdout/u); + + const banner = `> @pegma/billing-core@0.1.1 build /home/runner/work/billing-core/billing-core/packages/billing-core +> tsc -p tsconfig.json + +`; + const packed = [ + { + name: "@pegma/billing-core", + version: "0.1.1", + filename: "pegma-billing-core-0.1.1.tgz", + files: [{ path: "package.json", size: 1 }], + }, + ]; + expect(() => JSON.parse(banner + JSON.stringify(packed))).toThrow( + SyntaxError, + ); + expect(parseNpmJsonStdout(banner + JSON.stringify(packed))).toEqual(packed); + expect(parseNpmJsonStdout(JSON.stringify(packed))).toEqual(packed); + expect(() => parseNpmJsonStdout(banner)).toThrow("npm did not write JSON"); }); }); From 1cae58910e3699fcc468f90ad6191b157428dded Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sat, 15 Aug 2026 23:45:25 +0000 Subject: [PATCH 2/3] Declare parseNpmJsonStdout for the release test harness. Co-authored-by: Fly Over Coder --- scripts/release-packages.d.mts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/scripts/release-packages.d.mts b/scripts/release-packages.d.mts index bc2e344..35d672a 100644 --- a/scripts/release-packages.d.mts +++ b/scripts/release-packages.d.mts @@ -58,6 +58,8 @@ export function lockDependencyMatches( export function isNormalReleaseVersion(version: string): boolean; +export function parseNpmJsonStdout(stdout: string): unknown; + export function parseArguments( arguments_: readonly string[], ): ReleaseCommandOptions; From 4121161f2724033011c609000e9bcde101ac36da Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sat, 15 Aug 2026 23:51:15 +0000 Subject: [PATCH 3/3] Parse scalar npm --json values including integrity strings. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit parseNpmJsonStdout only sought [ or {, so npm view dist.integrity --json stdout ("sha512-…") never parsed and registry skip/confirm paths failed. Drop > banners, then parse any JSON value. Co-authored-by: Fly Over Coder --- scripts/release-packages.mjs | 22 +++++++++++++++------- tests/release-packages.test.ts | 11 +++++++++++ 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/scripts/release-packages.mjs b/scripts/release-packages.mjs index a35487c..c279189 100644 --- a/scripts/release-packages.mjs +++ b/scripts/release-packages.mjs @@ -102,20 +102,28 @@ function runNpm(arguments_, options = {}) { } /** - * `npm --json` writes a JSON value. Lifecycle scripts invoked during - * `npm pack` (including a package `prepack` of `pnpm run build`) may - * print a human banner on the same stdout: + * `npm --json` writes a JSON value: objects, arrays, or scalars + * (`"sha512-…"`, numbers, true/false/null). Lifecycle scripts invoked + * during `npm pack` may print a human banner on the same stdout: * > @pegma/...@0.1.1 build /home/runner/work/... - * Parse from the first JSON token so that banner is never fed to - * JSON.parse. + * Drop those `>` lines, then parse from the first JSON token so the + * banner is never fed to JSON.parse and `npm view dist.integrity --json` + * still yields a string. */ +const JSON_VALUE_START = + /[\[{"]|-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?|\btrue\b|\bfalse\b|\bnull\b/u; + export function parseNpmJsonStdout(stdout) { const text = typeof stdout === "string" ? stdout : ""; - const start = text.search(/[\[{]/u); + const withoutBanners = text + .split(/\r?\n/u) + .filter((line) => !/^\s*>/u.test(line)) + .join("\n"); + const start = withoutBanners.search(JSON_VALUE_START); if (start === -1) { fail("npm did not write JSON output"); } - return JSON.parse(text.slice(start)); + return JSON.parse(withoutBanners.slice(start)); } function unquoteYamlScalar(value) { diff --git a/tests/release-packages.test.ts b/tests/release-packages.test.ts index c4069f5..863986c 100644 --- a/tests/release-packages.test.ts +++ b/tests/release-packages.test.ts @@ -246,6 +246,17 @@ packages: expect(parseNpmJsonStdout(JSON.stringify(packed))).toEqual(packed); expect(() => parseNpmJsonStdout(banner)).toThrow("npm did not write JSON"); }); + + it("parses npm view dist.integrity --json scalar stdout", () => { + expect(parseNpmJsonStdout('"sha512-abc"')).toBe("sha512-abc"); + expect(parseNpmJsonStdout(' "sha512-abc"\n')).toBe("sha512-abc"); + expect(parseNpmJsonStdout(JSON.stringify("sha512-abc"))).toBe("sha512-abc"); + expect(parseNpmJsonStdout("0")).toBe(0); + expect(parseNpmJsonStdout("-2.5")).toBe(-2.5); + expect(parseNpmJsonStdout("true")).toBe(true); + expect(parseNpmJsonStdout("false")).toBe(false); + expect(parseNpmJsonStdout("null")).toBe(null); + }); }); describe("release source authentication", () => {