@jonmilner/local-ci 0.3.1 → 0.3.2

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. 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,13 @@ 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/;
16
19
  const id = ({ name, version }) => `${name}@${version}`;
17
20
  function alive(pid) {
18
21
  if (!Number.isInteger(pid) || pid <= 0)
@@ -115,12 +118,34 @@ function publish(dir, token, signal) {
115
118
  return new Promise((resolve, reject) => {
116
119
  const child = spawn("pnpm", ["release"], {
117
120
  cwd: dir,
118
- // An interrupt kills it, and the close below settles only once it has exited.
119
- signal,
121
+ // Its own session and group, so an interrupt stops the publish it started, not just pnpm.
122
+ detached: true,
120
123
  // CI makes changesets fail on an OTP request instead of prompting on an ignored stdin.
121
124
  env: { ...process.env, CI: "true", [TOKEN_ENV]: token },
122
125
  stdio: ["ignore", "pipe", "pipe"],
123
126
  });
127
+ // The group no longer gets the terminal's signals, so these are the only things that stop it.
128
+ // An interrupt asks it to stop and the close below settles once it has; exiting first kills it.
129
+ const killGroup = (name) => {
130
+ try {
131
+ if (child.pid)
132
+ process.kill(-child.pid, name);
133
+ }
134
+ catch {
135
+ // Already gone.
136
+ }
137
+ };
138
+ const stop = () => killGroup("SIGTERM");
139
+ const kill = () => killGroup("SIGKILL");
140
+ if (signal.aborted)
141
+ stop();
142
+ else
143
+ signal.addEventListener("abort", stop, { once: true });
144
+ process.once("exit", kill);
145
+ const settled = () => {
146
+ signal.removeEventListener("abort", stop);
147
+ process.off("exit", kill);
148
+ };
124
149
  let authError = false;
125
150
  const tee = (stream, sink) => {
126
151
  // Keep a tail so a match split across chunks is still seen.
@@ -136,10 +161,14 @@ function publish(dir, token, signal) {
136
161
  tee(child.stdout, process.stdout);
137
162
  tee(child.stderr, process.stderr);
138
163
  child.on("error", (error) => {
139
- if (!signal.aborted)
140
- reject(error);
164
+ settled();
165
+ reject(error);
141
166
  });
142
167
  child.on("close", (code, killed) => {
168
+ settled();
169
+ // Any of the group that ignored the interrupt and let go of the pipes must not outlive the run.
170
+ if (signal.aborted)
171
+ kill();
143
172
  if (code === 0)
144
173
  return resolve();
145
174
  reject(new Error(authError
@@ -148,14 +177,33 @@ function publish(dir, token, signal) {
148
177
  });
149
178
  });
150
179
  }
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;
180
+ /**
181
+ * Polls npm until each package is visible. The registry can take minutes to list a version
182
+ * after a publish, so a timeout here means the publish ran but npm hasn't caught up yet.
183
+ */
184
+ export async function verify(exec, packages, options = {}) {
185
+ const { intervalMs = 5_000, timeoutMs = NPM_WAIT_MS, sleep = defaultSleep } = options;
186
+ const ranHint = (pkg) => `The publish ran and may still appear; check with: npm view ${id(pkg)} version`;
187
+ // One budget for every package, so several slow ones still give up after timeoutMs.
188
+ let waited = 0;
154
189
  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`);
190
+ for (;;) {
191
+ let published;
192
+ try {
193
+ published = await isPublished(exec, pkg);
194
+ }
195
+ catch (error) {
196
+ throw new Error(`could not check ${id(pkg)} on npm: ${error.message}. ${ranHint(pkg)}`, {
197
+ cause: error,
198
+ });
199
+ }
200
+ if (published)
201
+ break;
202
+ if (waited + intervalMs > timeoutMs) {
203
+ throw new Error(`${id(pkg)} is not on npm after waiting ${timeoutMs / 1000}s. ${ranHint(pkg)}`);
204
+ }
158
205
  await sleep(intervalMs);
206
+ waited += intervalMs;
159
207
  }
160
208
  }
161
209
  }
@@ -200,8 +248,8 @@ async function pruneStale(git, root, own, branch) {
200
248
  }
201
249
  /**
202
250
  * 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.
251
+ * verifies the publish. Resolves with a one-line summary. SIGINT, SIGTERM, SIGHUP or SIGQUIT runs
252
+ * the same cleanup as a failure, then exits 128 + the signal number.
205
253
  */
206
254
  export async function release(exec, context) {
207
255
  const { root, log } = context;
@@ -258,8 +306,11 @@ export async function release(exec, context) {
258
306
  rmSync(temp, { recursive: true, force: true });
259
307
  unlock();
260
308
  })());
309
+ let signals = 0;
261
310
  const onSignal = (signal) => {
262
- // `once` hands a second signal back to Node's default, which exits at once.
311
+ // A second signal exits at once, through process.exit so the publish's exit hook still kills it.
312
+ if (signals++)
313
+ process.exit(128 + constants.signals[signal]);
263
314
  if (finishing)
264
315
  return;
265
316
  interrupted = signal;
@@ -270,7 +321,7 @@ export async function release(exec, context) {
270
321
  .finally(() => process.exit(128 + constants.signals[signal]));
271
322
  };
272
323
  for (const signal of SIGNALS)
273
- process.once(signal, onSignal);
324
+ process.on(signal, onSignal);
274
325
  try {
275
326
  await live();
276
327
  // Unguarded, like the token read below, so the fallback can't swallow an interrupt.
@@ -346,6 +397,8 @@ export async function release(exec, context) {
346
397
  await track(publish(dir, token, abort.signal));
347
398
  // Every current version, so a tag an earlier half-failed run left unpushed goes too.
348
399
  await pushTags(stepGit, stepRun, dir, await publicPackages(step, dir));
400
+ if (packages.length)
401
+ log(`waiting up to ${NPM_WAIT_MS / 60_000} minutes for npm to list ${packages.map(id).join(", ")}`);
349
402
  await verify(step, packages);
350
403
  return packages.length ? `released ${packages.map(id).join(", ")}` : "nothing to release";
351
404
  }
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.2",
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": {