Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/PROJECT_PLAN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down
4 changes: 2 additions & 2 deletions packages/billing-core/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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"
}
}
6 changes: 3 additions & 3 deletions packages/billing-stripe/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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": {
Expand All @@ -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"
}
}
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions scripts/release-packages.d.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
39 changes: 30 additions & 9 deletions scripts/release-packages.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,29 @@ 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: 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/...
* 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 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(withoutBanners.slice(start));
}

function unquoteYamlScalar(value) {
Expand Down Expand Up @@ -389,7 +407,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`);
}
Expand Down Expand Up @@ -671,7 +692,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`);
}
Expand Down Expand Up @@ -716,7 +737,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) {
Expand All @@ -730,7 +751,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 ||
Expand Down
66 changes: 60 additions & 6 deletions tests/release-packages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
isNormalReleaseVersion,
lockDependencyMatches,
parseArguments,
parseNpmJsonStdout,
parsePnpmLockfileImporters,
resolvedVersionSatisfies,
validateRepository,
Expand Down Expand Up @@ -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(
Expand All @@ -52,17 +53,21 @@ describe("release package metadata", () => {
name: string;
version: string;
dependencies?: Record<string, string>;
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", () => {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -202,6 +207,55 @@ 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");
});

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);
});
});

Expand Down
Loading