Skip to content

Commit 99e476a

Browse files
authored
Merge pull request #51 from nestm-dev/codex/npm-registry-propagation
fix(release): tolerate npm registry propagation
2 parents e1e8935 + e0d29a2 commit 99e476a

1 file changed

Lines changed: 21 additions & 5 deletions

File tree

scripts/publish.mjs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { spawnSync } from "node:child_process";
2-
import { existsSync, readdirSync, readFileSync } from "node:fs";
2+
import { appendFileSync, existsSync, readdirSync, readFileSync } from "node:fs";
33
import { dirname, join, resolve } from "node:path";
44

55
import { assertFixedGroup, assertFixedVersions } from "./publish-state.mjs";
66

77
const expectedRef = "refs/heads/main";
8+
const registryPropagationAttempts = 60;
9+
const registryPropagationDelayMs = 5_000;
810

911
if (
1012
process.env.GITHUB_ACTIONS !== "true" ||
@@ -90,10 +92,14 @@ for (const entry of sortForPublishing(workspacePackages)) {
9092
const state = await registryVersionState(name, version);
9193
if (state === "available") {
9294
console.log(`Already published: ${name}@${version}`);
95+
recordPublishedVersion(name, version);
9396
continue;
9497
}
9598
if (state === "incomplete") {
96-
throw new Error(`${name}@${version} exists in npm metadata but its tarball is unavailable`);
99+
console.log(`Waiting for npm to finish publishing: ${name}@${version}`);
100+
await waitForAvailableVersion(name, version);
101+
recordPublishedVersion(name, version);
102+
continue;
97103
}
98104

99105
console.log(`Publishing: ${name}@${version}`);
@@ -130,6 +136,7 @@ for (const entry of sortForPublishing(workspacePackages)) {
130136
}
131137

132138
await waitForAvailableVersion(name, version);
139+
recordPublishedVersion(name, version);
133140
}
134141

135142
function sortForPublishing(entries) {
@@ -175,11 +182,20 @@ async function registryVersionState(name, version) {
175182
}
176183

177184
async function waitForAvailableVersion(name, version) {
178-
for (let attempt = 0; attempt < 12; attempt += 1) {
185+
for (let attempt = 0; attempt < registryPropagationAttempts; attempt += 1) {
179186
if ((await registryVersionState(name, version)) === "available") return;
180-
await new Promise((resolveDelay) => setTimeout(resolveDelay, 5_000));
187+
await new Promise((resolveDelay) => setTimeout(resolveDelay, registryPropagationDelayMs));
181188
}
182-
throw new Error(`${name}@${version} did not become installable within 60 seconds`);
189+
throw new Error(`${name}@${version} did not become installable within five minutes`);
190+
}
191+
192+
function recordPublishedVersion(name, version) {
193+
const outputPath = process.env.CHANGESETS_OUTPUT;
194+
if (typeof outputPath !== "string" || outputPath.length === 0) return;
195+
appendFileSync(
196+
outputPath,
197+
`${JSON.stringify({ type: "git-tag", tag: `${name}@${version}`, packageName: name })}\n`,
198+
);
183199
}
184200

185201
function withoutOtp(environment) {

0 commit comments

Comments
 (0)