@jonmilner/local-ci 0.3.1 → 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -26,9 +26,9 @@ exec pnpm exec local-ci pre-push "$@"
26
26
  - `local-ci release`: publishes the repo's packages from this machine. Off unless `package.json` has `"local-ci": { "release": true }`; otherwise it prints `release not enabled` and exits 0. It needs a clean working tree, then fetches `origin` and takes a lock, so a second run exits with `release in progress`. In a temporary worktree at `origin`'s default branch:
27
27
  1. With pending changesets, it creates `release/<short-sha>`, runs the root `version-packages` script if `package.json` has one (for steps such as a size-history entry that belong in the version PR) or `pnpm changeset version` otherwise, commits `chore: version packages`, pushes (the hook runs the check and posts `check`), opens the PR, posts `pr-title`, waits for both statuses, and squash-merges it.
28
28
  2. At the merge commit (or the default branch, with no changesets pending), it runs `local-ci status`, then `pnpm release` with the npm token, then pushes the tags `changeset publish` created.
29
- 3. It checks each published `name@version` with `npm view`.
29
+ 3. It checks each published `name@version` with `npm view`, waiting up to 5 minutes in all for the registry to list them. A timeout or `npm view` error here exits non-zero, but the publish already ran: check with the `npm view` command the error gives before rerunning.
30
30
 
31
- A failure after the push closes the PR and deletes `release/<short-sha>`; Ctrl-C (SIGINT) or SIGTERM stops the running step (killing `pnpm release`), does the same cleanup, then exits 130 or 143 (a second signal exits at once). A run killed outright leaves its temp worktree and local branch, which the next run removes; its PR and remote branch stay open for you to close. Step 2 runs even with no changesets pending, so a rerun after a failed publish finishes the job. With nothing pending and every version already on npm it prints `nothing to release`. The repo needs changesets and a root `release` script (for example `pnpm build && changeset publish`). The npm token comes from the macOS Keychain (service `local-ci-npm`) and is passed only in `pnpm release`'s environment, never written to `.npmrc` or logged; see [npm publish token](../../docs/new-machine.md#npm-publish-token).
31
+ A failure after the push closes the PR and deletes `release/<short-sha>`; Ctrl-C (SIGINT), SIGTERM, SIGHUP (closing the terminal) or Ctrl-\\ (SIGQUIT) stops the running step (killing `pnpm release` and the publish it started), does the same cleanup, then exits 128 + the signal number, e.g. 130 for Ctrl-C (a second signal exits at once, killing the publish outright). A run killed outright leaves its temp worktree and local branch, which the next run removes; its PR and remote branch stay open for you to close. Step 2 runs even with no changesets pending, so a rerun after a failed publish finishes the job. If npm refuses the publish because it already has the version (a rerun while the registry still lags), it says so with the `npm view` command to check, instead of blaming the token; rerun once npm lists it. With nothing pending and every version already on npm it prints `nothing to release`. The repo needs changesets and a root `release` script (for example `pnpm build && changeset publish`). The npm token comes from the macOS Keychain (service `local-ci-npm`) and is passed only in `pnpm release`'s environment, never written to `.npmrc` or logged; see [npm publish token](../../docs/new-machine.md#npm-publish-token).
32
32
 
33
33
  ## Check command
34
34
 
package/dist/release.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import type { Exec } from "./exec.ts";
2
+ import { type WaitOptions } from "./github.ts";
2
3
  /** The macOS Keychain generic password that holds the npm token. */
3
4
  export declare const TOKEN_SERVICE = "local-ci-npm";
4
5
  /** npm and pnpm read a registry token from this variable; it is set only for `pnpm release`. */
@@ -19,6 +20,11 @@ export declare function publicPackages(exec: Exec, dir: string): Promise<Package
19
20
  export declare function isPublished(exec: Exec, pkg: Package): Promise<boolean>;
20
21
  /** Reads the npm token from the Keychain, failing with the setup doc when it is missing. */
21
22
  export declare function readToken(exec: Exec): Promise<string>;
23
+ /**
24
+ * Polls npm until each package is visible. The registry can take minutes to list a version
25
+ * after a publish, so a timeout here means the publish ran but npm hasn't caught up yet.
26
+ */
27
+ export declare function verify(exec: Exec, packages: Package[], options?: WaitOptions): Promise<void>;
22
28
  export interface ReleaseContext {
23
29
  /** The checkout the command runs from. */
24
30
  root: string;
@@ -31,7 +37,7 @@ export interface ReleaseContext {
31
37
  }
32
38
  /**
33
39
  * Versions pending changesets through a merged PR, publishes what npm lacks, pushes the tags and
34
- * verifies the publish. Resolves with a one-line summary. SIGINT or SIGTERM runs the same cleanup
35
- * as a failure, then exits 128 + the signal number.
40
+ * verifies the publish. Resolves with a one-line summary. SIGINT, SIGTERM, SIGHUP or SIGQUIT runs
41
+ * the same cleanup as a failure, then exits 128 + the signal number.
36
42
  */
37
43
  export declare function release(exec: Exec, context: ReleaseContext): Promise<string>;
package/dist/release.js CHANGED
@@ -9,10 +9,15 @@ export const TOKEN_SERVICE = "local-ci-npm";
9
9
  export const TOKEN_ENV = "NPM_CONFIG_//registry.npmjs.org/:_authToken";
10
10
  const SETUP_DOC = "https://github.com/jonmilner/config/blob/main/docs/new-machine.md#npm-publish-token";
11
11
  const VERSION_TITLE = "chore: version packages";
12
+ // How long verify() waits for npm to list what was just published.
13
+ const NPM_WAIT_MS = 300_000;
12
14
  const TEMP_PREFIX = "local-ci-release-";
13
- const SIGNALS = ["SIGINT", "SIGTERM"];
15
+ // SIGHUP and SIGQUIT too, since the terminal sends them to node's group and `pnpm release` has its own.
16
+ const SIGNALS = ["SIGINT", "SIGTERM", "SIGHUP", "SIGQUIT"];
14
17
  // What npm and pnpm print when the registry rejects the token, or wants an OTP it can't bypass.
15
- const AUTH_ERROR = /\b(?:E401|E403|EOTP|401 Unauthorized|403 Forbidden)\b/;
18
+ const AUTH_ERROR = /\b(?:E401|E403|EOTP|ERR_PNPM_OTP_NON_INTERACTIVE|401 Unauthorized|403 Forbidden)\b/;
19
+ // What npm prints when the version is already on the registry. It comes with an E403, so it is checked before AUTH_ERROR.
20
+ const PUBLISH_OVER = /cannot publish over the previously published version/;
16
21
  const id = ({ name, version }) => `${name}@${version}`;
17
22
  function alive(pid) {
18
23
  if (!Number.isInteger(pid) || pid <= 0)
@@ -110,52 +115,109 @@ function run(file, args, cwd) {
110
115
  throw new Error(`${file} ${args.join(" ")} failed (${cause})`);
111
116
  }
112
117
  }
113
- /** Runs `pnpm release` with the token in its env only, teeing its output to spot a rejected token. */
114
- function publish(dir, token, signal) {
118
+ /** The hint for checking each package on npm by hand. */
119
+ const npmView = (packages) => packages.length
120
+ ? `check with: ${packages.map((pkg) => `npm view ${id(pkg)} version`).join("; ")}`
121
+ : "check with: npm view <name>@<version> version";
122
+ /**
123
+ * Runs `pnpm release` with the token in its env only, teeing its output to spot a rejected token
124
+ * or a version npm already has. `packages` are the ones it should publish, for the hints.
125
+ */
126
+ function publish(dir, token, packages, signal) {
115
127
  return new Promise((resolve, reject) => {
116
128
  const child = spawn("pnpm", ["release"], {
117
129
  cwd: dir,
118
- // An interrupt kills it, and the close below settles only once it has exited.
119
- signal,
130
+ // Its own session and group, so an interrupt stops the publish it started, not just pnpm.
131
+ detached: true,
120
132
  // CI makes changesets fail on an OTP request instead of prompting on an ignored stdin.
121
133
  env: { ...process.env, CI: "true", [TOKEN_ENV]: token },
122
134
  stdio: ["ignore", "pipe", "pipe"],
123
135
  });
136
+ // The group no longer gets the terminal's signals, so these are the only things that stop it.
137
+ // An interrupt asks it to stop and the close below settles once it has; exiting first kills it.
138
+ const killGroup = (name) => {
139
+ try {
140
+ if (child.pid)
141
+ process.kill(-child.pid, name);
142
+ }
143
+ catch {
144
+ // Already gone.
145
+ }
146
+ };
147
+ const stop = () => killGroup("SIGTERM");
148
+ const kill = () => killGroup("SIGKILL");
149
+ if (signal.aborted)
150
+ stop();
151
+ else
152
+ signal.addEventListener("abort", stop, { once: true });
153
+ process.once("exit", kill);
154
+ const settled = () => {
155
+ signal.removeEventListener("abort", stop);
156
+ process.off("exit", kill);
157
+ };
124
158
  let authError = false;
159
+ let publishOver = false;
125
160
  const tee = (stream, sink) => {
126
161
  // Keep a tail so a match split across chunks is still seen.
127
162
  let tail = "";
128
163
  stream.on("data", (chunk) => {
129
164
  sink.write(chunk);
130
165
  const text = tail + chunk.toString();
166
+ if (PUBLISH_OVER.test(text))
167
+ publishOver = true;
131
168
  if (AUTH_ERROR.test(text))
132
169
  authError = true;
133
- tail = text.slice(-32);
170
+ tail = text.slice(-64);
134
171
  });
135
172
  };
136
173
  tee(child.stdout, process.stdout);
137
174
  tee(child.stderr, process.stderr);
138
175
  child.on("error", (error) => {
139
- if (!signal.aborted)
140
- reject(error);
176
+ settled();
177
+ reject(error);
141
178
  });
142
179
  child.on("close", (code, killed) => {
180
+ settled();
181
+ // Any of the group that ignored the interrupt and let go of the pipes must not outlive the run.
182
+ if (signal.aborted)
183
+ kill();
143
184
  if (code === 0)
144
185
  return resolve();
145
- reject(new Error(authError
146
- ? `npm rejected the token in the Keychain (service ${TOKEN_SERVICE}); it may have expired or lack "bypass 2FA". Rotate it: ${SETUP_DOC}`
147
- : `pnpm release failed (${code === null ? killed : `exit ${code}`})`));
186
+ reject(new Error(publishOver
187
+ ? `npm already has a version this run tried to publish, so it refused to publish over it; the registry may still be catching up on an earlier publish. Rerun once npm lists it; ${npmView(packages)}`
188
+ : authError
189
+ ? `npm rejected the token in the Keychain (service ${TOKEN_SERVICE}); it may have expired or lack "bypass 2FA". Rotate it: ${SETUP_DOC}`
190
+ : `pnpm release failed (${code === null ? killed : `exit ${code}`})`));
148
191
  });
149
192
  });
150
193
  }
151
- /** Polls npm until each package is visible, since the registry lags a publish slightly. */
152
- async function verify(exec, packages, options = {}) {
153
- const { intervalMs = 5_000, timeoutMs = 60_000, sleep = defaultSleep } = options;
194
+ /**
195
+ * Polls npm until each package is visible. The registry can take minutes to list a version
196
+ * after a publish, so a timeout here means the publish ran but npm hasn't caught up yet.
197
+ */
198
+ export async function verify(exec, packages, options = {}) {
199
+ const { intervalMs = 5_000, timeoutMs = NPM_WAIT_MS, sleep = defaultSleep } = options;
200
+ const ranHint = (pkg) => `The publish ran and may still appear; check with: npm view ${id(pkg)} version`;
201
+ // One budget for every package, so several slow ones still give up after timeoutMs.
202
+ let waited = 0;
154
203
  for (const pkg of packages) {
155
- for (let waited = 0; !(await isPublished(exec, pkg)); waited += intervalMs) {
156
- if (waited + intervalMs > timeoutMs)
157
- throw new Error(`${id(pkg)} is not on npm after publishing`);
204
+ for (;;) {
205
+ let published;
206
+ try {
207
+ published = await isPublished(exec, pkg);
208
+ }
209
+ catch (error) {
210
+ throw new Error(`could not check ${id(pkg)} on npm: ${error.message}. ${ranHint(pkg)}`, {
211
+ cause: error,
212
+ });
213
+ }
214
+ if (published)
215
+ break;
216
+ if (waited + intervalMs > timeoutMs) {
217
+ throw new Error(`${id(pkg)} is not on npm after waiting ${timeoutMs / 1000}s. ${ranHint(pkg)}`);
218
+ }
158
219
  await sleep(intervalMs);
220
+ waited += intervalMs;
159
221
  }
160
222
  }
161
223
  }
@@ -200,8 +262,8 @@ async function pruneStale(git, root, own, branch) {
200
262
  }
201
263
  /**
202
264
  * Versions pending changesets through a merged PR, publishes what npm lacks, pushes the tags and
203
- * verifies the publish. Resolves with a one-line summary. SIGINT or SIGTERM runs the same cleanup
204
- * as a failure, then exits 128 + the signal number.
265
+ * verifies the publish. Resolves with a one-line summary. SIGINT, SIGTERM, SIGHUP or SIGQUIT runs
266
+ * the same cleanup as a failure, then exits 128 + the signal number.
205
267
  */
206
268
  export async function release(exec, context) {
207
269
  const { root, log } = context;
@@ -258,8 +320,11 @@ export async function release(exec, context) {
258
320
  rmSync(temp, { recursive: true, force: true });
259
321
  unlock();
260
322
  })());
323
+ let signals = 0;
261
324
  const onSignal = (signal) => {
262
- // `once` hands a second signal back to Node's default, which exits at once.
325
+ // A second signal exits at once, through process.exit so the publish's exit hook still kills it.
326
+ if (signals++)
327
+ process.exit(128 + constants.signals[signal]);
263
328
  if (finishing)
264
329
  return;
265
330
  interrupted = signal;
@@ -270,7 +335,7 @@ export async function release(exec, context) {
270
335
  .finally(() => process.exit(128 + constants.signals[signal]));
271
336
  };
272
337
  for (const signal of SIGNALS)
273
- process.once(signal, onSignal);
338
+ process.on(signal, onSignal);
274
339
  try {
275
340
  await live();
276
341
  // Unguarded, like the token read below, so the fallback can't swallow an interrupt.
@@ -343,9 +408,11 @@ export async function release(exec, context) {
343
408
  await stepRun(process.execPath, [context.cli, "status"], dir);
344
409
  await live();
345
410
  log(`publishing ${packages.map(id).join(", ") || "nothing new"}`);
346
- await track(publish(dir, token, abort.signal));
411
+ await track(publish(dir, token, packages, abort.signal));
347
412
  // Every current version, so a tag an earlier half-failed run left unpushed goes too.
348
413
  await pushTags(stepGit, stepRun, dir, await publicPackages(step, dir));
414
+ if (packages.length)
415
+ log(`waiting up to ${NPM_WAIT_MS / 60_000} minutes for npm to list ${packages.map(id).join(", ")}`);
349
416
  await verify(step, packages);
350
417
  return packages.length ? `released ${packages.map(id).join(", ")}` : "nothing to release";
351
418
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jonmilner/local-ci",
3
- "version": "0.3.1",
3
+ "version": "0.3.3",
4
4
  "description": "Run a repo's check in a pre-push hook and post the result as GitHub commit statuses",
5
5
  "license": "MIT",
6
6
  "repository": {