@jonmilner/local-ci 0.8.0 → 0.10.0

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
@@ -21,9 +21,9 @@ That writes `pre-push` and `commit-msg` shims, each one line that calls local-ci
21
21
  ## Commands
22
22
 
23
23
  - `local-ci hooks install`: writes executable `#!/bin/sh` `pre-push` and `commit-msg` shims into the hooks directory: `core.hooksPath`, resolved against the repo root, with a Vite+ (or husky) `<dir>/_` dispatcher mapped to `<dir>`, and an absolute path into the main checkout mapped onto the current worktree. Each shim is one line: `pnpm exec local-ci pre-push "$@"` and `pnpm exec local-ci commit-msg "$1"` in a repo with a `package.json`, a global `local-ci …` without one, or `"local-ci": { "exec": "<command>" }` in `package.json` in place of `pnpm exec local-ci` (this repo runs local-ci from source that way). An existing shell hook keeps its other lines and gains the shim line, at the end, only when no line already runs `local-ci pre-push` (or `commit-msg`), so a line with an env prefix such as `LOCAL_CI_COMMAND=… local-ci pre-push "$@"` counts. It prints each file it wrote, or `hooks up to date`; a second run changes nothing. It exits 1 without `core.hooksPath` or with one outside the repo's tracked files (including `.git/hooks`), and for an existing hook in another language (a `node` or `python` shebang), naming the line to add by hand. Run it by hand and commit the result; `prepare` does not run it.
24
- - `local-ci pre-push`: the hook. It first checks that both hook files are in place, as `local-ci hooks install` would leave them, and refuses the push otherwise (``hooks out of date: <file> must run local-ci; run `<command> hooks install` and commit the result``), so a repo's hooks can't drift. For each commit pushed to a branch (deletes and tags are skipped) that has not already passed, runs the check command. The pushed commit must be `HEAD` with a clean working tree (`git status --porcelain` empty), so the result reflects the commit; otherwise the push is refused. A failing check blocks the push. When every commit passes, it starts `local-ci post <sha>` in the background, aimed at the pushed remote, and lets the push go ahead.
24
+ - `local-ci pre-push`: the hook. It first checks that both hook files are in place, as `local-ci hooks install` would leave them, and refuses the push otherwise (``hooks out of date: <file> must run local-ci; run `<command> hooks install` and commit the result``), so a repo's hooks can't drift. For each commit pushed to a branch (deletes and tags are skipped) that has not already passed, runs the check command. The pushed commit must be `HEAD` with a clean working tree (`git status --porcelain` empty), so the result reflects the commit; otherwise the push is refused. A failing check blocks the push. When every commit passes, it starts `local-ci post <sha>` in the background, aimed at the pushed remote and told the pid of the `git` process doing the push (`LOCAL_CI_PUSH_PID`), and lets the push go ahead.
25
25
  - `local-ci commit-msg <file>`: lints the commit message in `<file>` with `@jonmilner/commitlint-config`, for a git `commit-msg` hook (git passes `.git/COMMIT_EDITMSG`). `local-ci commit-msg -` reads the message from stdin, for PR titles. Comment lines (starting with `#`) and everything after a `# ------------------------ >8 ------------------------` scissors line are stripped first; merge commits, `fixup!`/`squash!` and reverts are skipped. Each error or warning goes to stderr as `commit-msg: <message> [<rule>]`, followed by the offending header; it exits 1 when there are errors (warnings alone exit 0), and 2 when no file is given. It makes no network, git or GitHub calls.
26
- - `local-ci post <sha>`: waits up to 3 minutes for GitHub to have the commit, posts status `check` = `success` (`<command> passed on <hostname>`), then posts `pr-title` on it for an open PR that contains it (waiting up to 30s for GitHub to link them). A PR opened after the push, or a retitled one, needs `local-ci pr-title` run by hand.
26
+ - `local-ci post <sha>`: when started by `pre-push`, first waits for the pushing `git` to exit (polling each second, up to 60 minutes, logging if it gives up), since git sends the push only after the whole hook exits, so a slow step after local-ci in the hook can't run out the clock; run by hand, it skips that wait. It then waits up to 3 minutes for GitHub to have the commit, posts status `check` = `success` (`<command> passed on <hostname>`), then posts `pr-title` on it for an open PR that contains it (waiting up to 30s for GitHub to link them). A PR opened after the push, or a retitled one, needs `local-ci pr-title` run by hand.
27
27
  - `local-ci pr-title [<pr>]`: lints the PR title (the current branch's PR by default) with `@jonmilner/commitlint-config` and posts status `pr-title` on its head commit, `failure` with the first error when the title does not pass.
28
28
  - `local-ci status [<sha>]`: posts `check` for a commit (default `HEAD`) that already passed, or runs the check first. When the check command fails, it posts `check` = `failure` (`<command> failed on <hostname>`) and exits non-zero; a later passing run posts `success` over it. A dirty tree, a commit that is not `HEAD`, or a `gh` error posts nothing. Use it after a push that skipped hooks (`--no-verify`) or a lost background poster.
29
29
  - `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:
@@ -31,7 +31,7 @@ That writes `pre-push` and `commit-msg` shims, each one line that calls local-ci
31
31
  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.
32
32
  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.
33
33
 
34
- 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).
34
+ 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 is `$NPM_TOKEN_JONMILNER` if set, else `secret get NPM_TOKEN_JONMILNER` (with neither, it stops before changing anything); within 14 days of the expiry date `secret list` gives it, the run prints a one-line warning and carries on. The token 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).
35
35
 
36
36
  - `local-ci renovate`: posts `check` and `pr-title` on Renovate PRs, which no push from this machine ever covers. It finds every open PR the Renovate app opened in the `gh` user's repos (`gh search prs --author app/renovate`) and takes only those on a `renovate/**` branch of the repo itself; any other PR is left to its own push. For each, from the clone at `~/git/<repo>` (`LOCAL_CI_PROJECTS` overrides `~/git`) whose `origin` is that repo, it:
37
37
  - skips a repo with no such clone, logging why;
@@ -60,4 +60,18 @@ Logs, the record of passed commits and the release lock live in `~/.local/state/
60
60
 
61
61
  One check command runs at a time on the machine, across every repo, so overlapping checks can't starve each other's tests: `pre-push`, `status`, and the `status` runs `renovate` starts each take `check.lock` in the same directory around the check command (not the status posts). A run that finds it held prints `local-ci: waiting for the check lock, held by pid <pid> (<repo> <short-sha>)`, again whenever another run takes it first, and polls every second until the holder exits or dies; Ctrl-C stops the wait. After the wait it skips a commit that passed meanwhile and re-applies the clean-tree rule. The lock goes when the check passes, fails or is killed by a signal. Ctrl-C, SIGTERM or SIGHUP to local-ci passes the signal to the check's whole process group (so a check script's children get it too), waits for the check to exit, then releases the lock and dies of the signal, so a check never outlives its lock. A lock whose holder died (SIGKILL, a crash) or that predates the last boot is taken over by the next run. A local-ci check nested inside a check (one that pushes, say) sees its ancestor holds the lock and runs without waiting. Each check run appends its duration and outcome to the log when it ends, e.g. `[<end time>] abc1234: pnpm check passed in 71.2s` (or `failed in`, or `did not finish after` when the check is killed by a signal or can't start); a commit that already passed is skipped and logs nothing. Ctrl-C stops the check, which logs `did not finish after`.
62
62
 
63
+ ### Taking the check lock from your own scripts
64
+
65
+ A heavy job outside `local-ci` (a browser suite run from a hook, say) can queue behind the checks, and make them queue behind it, by taking the same `check.lock`:
66
+
67
+ ```ts
68
+ import { withCheckLock } from "@jonmilner/local-ci/lock";
69
+
70
+ await withCheckLock("my-repo browser", async () => {
71
+ // runs alone with respect to every local-ci check and every other withCheckLock job
72
+ });
73
+ ```
74
+
75
+ `withCheckLock(holder, fn, options?)` waits while another holder has the lock, logging `local-ci: waiting for the check lock, held by pid <pid> (<holder>)` to stderr as the CLI does, runs `fn`, and releases the lock when `fn` returns or throws. `holder` is the label other waiters see. `lockCheck(holder, options?)` does the take and returns the release function, for callers that can't wrap their work in a function. `options.file` overrides the lock path and `options.onWait` replaces the wait message. The path is `check.lock` in the state directory above, so no repo config is needed. A call nested under an ancestor process that holds the lock runs without waiting and leaves the lock alone. SIGINT, SIGTERM or SIGHUP releases the lock and ends the process, so a job that needs its own signal cleanup should run it before calling the release itself. The package has an `exports` map, so deep imports other than `./lock` no longer resolve.
76
+
63
77
  Part of [jonmilner/config](https://github.com/jonmilner/config#readme).
@@ -0,0 +1,25 @@
1
+ import type { ChildProcess } from "node:child_process";
2
+ import { LockHeld } from "./lock.ts";
3
+ /** The check lock this process holds through `lockCheck`, if any, for the CLI to attach its check to. */
4
+ export declare function heldCheckLock(): {
5
+ child?: ChildProcess;
6
+ signal?: NodeJS.Signals;
7
+ unlock: () => void;
8
+ } | undefined;
9
+ export interface LockCheckOptions {
10
+ /** The lock file; default is `check.lock` in local-ci's state directory, the one the CLI uses. */
11
+ file?: string;
12
+ /** Called when the wait starts, and whenever another process takes the lock first. */
13
+ onWait?: (held: LockHeld) => void;
14
+ }
15
+ /** The machine-wide check lock's path, in `$XDG_STATE_HOME/local-ci` (default `~/.local/state`). */
16
+ export declare function checkLockPath(env?: NodeJS.ProcessEnv): string;
17
+ /**
18
+ * Takes the machine-wide check lock, waiting while another check or job holds it, and returns its
19
+ * release. `holder` names the caller in other waiters' messages. A caller nested under an ancestor
20
+ * that holds the lock runs without waiting, and its release leaves the lock alone. SIGINT, SIGTERM
21
+ * and SIGHUP release the lock and end the process.
22
+ */
23
+ export declare function lockCheck(holder: string, { file, onWait }?: LockCheckOptions): Promise<() => void>;
24
+ /** Runs `fn` under the machine-wide check lock, so it queues behind `local-ci` checks and vice versa. */
25
+ export declare function withCheckLock<T>(holder: string, fn: () => Promise<T>, options?: LockCheckOptions): Promise<T>;
@@ -0,0 +1,102 @@
1
+ import { mkdirSync } from "node:fs";
2
+ import { join } from "node:path";
3
+ import { stateDir } from "./config.js";
4
+ import { LockHeld, takeLock, waitForLock } from "./lock.js";
5
+ import { processTable } from "./processes.js";
6
+ import { ancestorsFrom } from "./push.js";
7
+ /** This process's ancestors' pids, or just its parent when `ps` can't list them. */
8
+ function ancestors() {
9
+ return new Set(ancestorsFrom(processTable(), process.ppid));
10
+ }
11
+ const SIGNALS = ["SIGINT", "SIGTERM", "SIGHUP"];
12
+ /** The check lock this process holds, the check running under it, and a signal it deferred. */
13
+ let held;
14
+ /** The check lock this process holds through `lockCheck`, if any, for the CLI to attach its check to. */
15
+ export function heldCheckLock() {
16
+ return held;
17
+ }
18
+ /** Dies of `signal` as if there were no handler for it. */
19
+ function reraise(signal) {
20
+ for (const name of SIGNALS)
21
+ process.off(name, onSignal);
22
+ process.kill(process.pid, signal);
23
+ // The default action has ended the process by now; this only satisfies the type.
24
+ process.exit(1);
25
+ }
26
+ /**
27
+ * While a check runs, passes the signal on and lets the check stop first, so it never outlives
28
+ * the lock; its release then dies of the signal. Otherwise releases the lock and dies of it now.
29
+ */
30
+ function onSignal(signal) {
31
+ if (held?.child) {
32
+ held.signal ??= signal;
33
+ // The check leads its own process group, so the signal reaches a script's children too.
34
+ try {
35
+ process.kill(-held.child.pid, signal);
36
+ }
37
+ catch {
38
+ held.child.kill(signal);
39
+ }
40
+ return;
41
+ }
42
+ held?.unlock();
43
+ held = undefined;
44
+ reraise(signal);
45
+ }
46
+ /** Holds `unlock` until the returned release, releasing it on SIGINT, SIGTERM or SIGHUP too. */
47
+ function releaseOnSignal(unlock) {
48
+ if (!process.listenerCount("SIGINT", onSignal)) {
49
+ for (const name of SIGNALS)
50
+ process.on(name, onSignal);
51
+ }
52
+ const lock = { unlock };
53
+ held = lock;
54
+ return () => {
55
+ if (held === lock)
56
+ held = undefined;
57
+ unlock();
58
+ if (lock.signal)
59
+ reraise(lock.signal);
60
+ };
61
+ }
62
+ /** The machine-wide check lock's path, in `$XDG_STATE_HOME/local-ci` (default `~/.local/state`). */
63
+ export function checkLockPath(env = process.env) {
64
+ const dir = stateDir(env);
65
+ mkdirSync(dir, { recursive: true });
66
+ return join(dir, "check.lock");
67
+ }
68
+ /**
69
+ * Takes the machine-wide check lock, waiting while another check or job holds it, and returns its
70
+ * release. `holder` names the caller in other waiters' messages. A caller nested under an ancestor
71
+ * that holds the lock runs without waiting, and its release leaves the lock alone. SIGINT, SIGTERM
72
+ * and SIGHUP release the lock and end the process.
73
+ */
74
+ export async function lockCheck(holder, { file = checkLockPath(), onWait } = {}) {
75
+ try {
76
+ return releaseOnSignal(takeLock(file, "check", holder));
77
+ }
78
+ catch (error) {
79
+ if (!(error instanceof LockHeld))
80
+ throw error;
81
+ // A check nested in a check (one that pushes, say) runs under its ancestor's lock rather
82
+ // than waiting forever on it.
83
+ if (ancestors().has(error.pid))
84
+ return () => { };
85
+ }
86
+ return releaseOnSignal(await waitForLock(file, {
87
+ what: "check",
88
+ holder,
89
+ onWait: onWait ??
90
+ (({ pid, holder }) => process.stderr.write(`local-ci: waiting for the check lock, held by pid ${pid} (${holder || "unknown"})\n`)),
91
+ }));
92
+ }
93
+ /** Runs `fn` under the machine-wide check lock, so it queues behind `local-ci` checks and vice versa. */
94
+ export async function withCheckLock(holder, fn, options) {
95
+ const unlock = await lockCheck(holder, options);
96
+ try {
97
+ return await fn();
98
+ }
99
+ finally {
100
+ unlock();
101
+ }
102
+ }
package/dist/cli.js CHANGED
@@ -1,18 +1,20 @@
1
1
  #!/usr/bin/env node
2
- import { execFileSync, spawn } from "node:child_process";
2
+ import { spawn } from "node:child_process";
3
3
  import { closeSync, openSync, readFileSync } from "node:fs";
4
4
  import { hostname } from "node:os";
5
5
  import { basename, dirname, join, relative, resolve } from "node:path";
6
6
  import { fileURLToPath } from "node:url";
7
+ import { heldCheckLock, lockCheck } from "./check-lock.js";
7
8
  import { commitMsg } from "./commit-msg.js";
8
9
  import { appendLog, checkRunMessage, logLine, passedCommand, recordPass, releaseEnabled, resolveCommand, shimCommand, statePaths, } from "./config.js";
9
10
  import { exec, run } from "./exec.js";
10
11
  import { assertGhAuth, checkStatus, postStatus, prTitleStatus, viewPr, waitForCommit, waitForOpenPr, } from "./github.js";
11
12
  import { hooksDir, installHooks, missingHooks } from "./hooks.js";
12
13
  import { installRenovateAgent, renovateLog, rotateLog, uninstallRenovateAgent } from "./launchd.js";
13
- import { LockHeld, takeLock, waitForLock } from "./lock.js";
14
- import { cannotCheckInPlace, githubRepo, parsePrePush, shasToCheck } from "./push.js";
15
- import { release } from "./release.js";
14
+ import { waitForExit } from "./lock.js";
15
+ import { processTable } from "./processes.js";
16
+ import { cannotCheckInPlace, githubRepo, nearestGit, parsePrePush, shasToCheck } from "./push.js";
17
+ import { release, takeEnvToken } from "./release.js";
16
18
  import { projectsDir, renovate } from "./renovate.js";
17
19
  import { lintTitle } from "./title.js";
18
20
  const USAGE = `Usage:
@@ -48,97 +50,12 @@ class CheckFailed extends Error {
48
50
  this.command = command;
49
51
  }
50
52
  }
51
- /** This process's ancestors' pids, or none when `ps` can't list them. */
52
- function ancestors() {
53
- const parents = new Map();
54
- try {
55
- for (const line of execFileSync("ps", ["-A", "-o", "pid=,ppid="], { encoding: "utf8" }).split("\n")) {
56
- const [pid, ppid] = line.trim().split(/\s+/).map(Number);
57
- if (pid && ppid !== undefined)
58
- parents.set(pid, ppid);
59
- }
60
- }
61
- catch {
62
- return new Set();
63
- }
64
- const found = new Set();
65
- for (let pid = process.ppid; pid > 1 && !found.has(pid); pid = parents.get(pid) ?? 0)
66
- found.add(pid);
67
- return found;
68
- }
69
- const SIGNALS = ["SIGINT", "SIGTERM", "SIGHUP"];
70
- /** The check lock this process holds, the check running under it, and a signal it deferred. */
71
- let held;
72
- /** Dies of `signal` as if local-ci had no handler for it. */
73
- function reraise(signal) {
74
- for (const name of SIGNALS)
75
- process.off(name, onSignal);
76
- process.kill(process.pid, signal);
77
- // The default action has ended the process by now; this only satisfies the type.
78
- process.exit(1);
79
- }
80
- /**
81
- * While a check runs, passes the signal on and lets the check stop first, so it never outlives
82
- * the lock; its release then dies of the signal. Otherwise releases the lock and dies of it now.
83
- */
84
- function onSignal(signal) {
85
- if (held?.child) {
86
- held.signal ??= signal;
87
- // The check leads its own process group, so the signal reaches a script's children too.
88
- try {
89
- process.kill(-held.child.pid, signal);
90
- }
91
- catch {
92
- held.child.kill(signal);
93
- }
94
- return;
95
- }
96
- held?.unlock();
97
- held = undefined;
98
- reraise(signal);
99
- }
100
- /** Holds `unlock` until the returned release, releasing it on SIGINT, SIGTERM or SIGHUP too. */
101
- function releaseOnSignal(unlock) {
102
- if (!process.listenerCount("SIGINT", onSignal)) {
103
- for (const name of SIGNALS)
104
- process.on(name, onSignal);
105
- }
106
- const lock = { unlock };
107
- held = lock;
108
- return () => {
109
- if (held === lock)
110
- held = undefined;
111
- unlock();
112
- if (lock.signal)
113
- reraise(lock.signal);
114
- };
115
- }
116
53
  /** Throws unless `sha` can be checked in place: it is HEAD and the tree is clean. */
117
54
  async function assertCheckable(sha) {
118
55
  const reason = cannotCheckInPlace(sha, await git("rev-parse", "HEAD"), await git("status", "--porcelain"));
119
56
  if (reason)
120
57
  throw new Error(`cannot check ${sha.slice(0, 7)}: ${reason}`);
121
58
  }
122
- /** Takes the machine-wide check lock, waiting while another repo's check holds it. */
123
- async function lockCheck({ name, checkLock }, sha) {
124
- const holder = `${name} ${sha.slice(0, 7)}`;
125
- try {
126
- return releaseOnSignal(takeLock(checkLock, "check", holder));
127
- }
128
- catch (error) {
129
- if (!(error instanceof LockHeld))
130
- throw error;
131
- // A check nested in a check (one that pushes, say) runs under its ancestor's lock rather
132
- // than waiting forever on it.
133
- if (ancestors().has(error.pid))
134
- return () => { };
135
- }
136
- return releaseOnSignal(await waitForLock(checkLock, {
137
- what: "check",
138
- holder,
139
- onWait: ({ pid, holder }) => err(`local-ci: waiting for the check lock, held by pid ${pid} (${holder || "unknown"})`),
140
- }));
141
- }
142
59
  /**
143
60
  * Runs the check at `sha` in place, after the clean-tree rule, unless it already passed. One check
144
61
  * runs at a time across all repos, so overlapping checks can't starve each other's tests.
@@ -147,7 +64,7 @@ async function runCheck(current, sha) {
147
64
  if (passedCommand(current.passed, sha))
148
65
  return;
149
66
  await assertCheckable(sha);
150
- const unlock = await lockCheck(current, sha);
67
+ const unlock = await lockCheck(`${current.name} ${sha.slice(0, 7)}`, { file: current.checkLock });
151
68
  try {
152
69
  // Another run may have passed it, or the tree changed, while this one waited.
153
70
  if (passedCommand(current.passed, sha))
@@ -161,6 +78,7 @@ async function runCheck(current, sha) {
161
78
  }
162
79
  /** Runs the check command under the check lock, logging its duration and recording a pass. */
163
80
  async function runLocked({ root, passed, log: logFile }, sha) {
81
+ const held = heldCheckLock();
164
82
  const command = resolveCommand(root);
165
83
  err(`local-ci: running ${command} on ${sha.slice(0, 7)}`);
166
84
  const started = performance.now();
@@ -235,7 +153,13 @@ async function prePush(remoteUrl) {
235
153
  await runCheck(current, sha);
236
154
  // Post to the remote being pushed, not whichever one gh would pick.
237
155
  const ghRepo = remoteUrl ? githubRepo(remoteUrl) : undefined;
238
- const env = ghRepo ? { ...process.env, GH_REPO: ghRepo } : process.env;
156
+ const env = ghRepo ? { ...process.env, GH_REPO: ghRepo } : { ...process.env };
157
+ // git sends the push only once this hook exits, so the poster waits on git before GitHub.
158
+ const git = nearestGit(processTable(), process.pid);
159
+ if (git)
160
+ env[PUSH_PID] = String(git);
161
+ else
162
+ delete env[PUSH_PID];
239
163
  const fd = openSync(current.log, "a");
240
164
  for (const sha of shas) {
241
165
  spawn(process.execPath, [fileURLToPath(import.meta.url), "post", sha], {
@@ -254,11 +178,24 @@ async function post({ passed }, sha) {
254
178
  if (!command) {
255
179
  throw new Error(`${sha.slice(0, 7)} has no passing check; run local-ci status ${sha}`);
256
180
  }
181
+ await waitForPush(sha);
257
182
  await postCheck(sha, checkStatus(command, hostname()));
258
183
  const pr = await waitForOpenPr(exec, sha);
259
184
  if (pr)
260
185
  await prTitle(String(pr), sha);
261
186
  }
187
+ /** The env var pre-push sets on each poster to the pid of the git process doing the push. */
188
+ const PUSH_PID = "LOCAL_CI_PUSH_PID";
189
+ /** Waits for the pushing git to exit, when pre-push named one; a `post` run by hand has none. */
190
+ async function waitForPush(sha) {
191
+ const pid = Number(process.env[PUSH_PID]);
192
+ if (!Number.isInteger(pid) || pid <= 1)
193
+ return;
194
+ log(`${sha.slice(0, 7)}: waiting for git (pid ${pid}) to push`);
195
+ if (!(await waitForExit(pid))) {
196
+ log(`${sha.slice(0, 7)}: git (pid ${pid}) still running after 60 min; waiting for GitHub anyway`);
197
+ }
198
+ }
262
199
  /** Posts `pr-title` on `sha`, or on the PR's head commit when `sha` is omitted. */
263
200
  async function prTitle(pr, sha) {
264
201
  const { number, title, headRefOid } = await viewPr(exec, pr);
@@ -299,12 +236,21 @@ async function status(ref = "HEAD") {
299
236
  await post(current, sha);
300
237
  }
301
238
  async function runRelease() {
239
+ // First, before any child process runs.
240
+ const envToken = takeEnvToken();
302
241
  const { root, lock } = await repo();
303
242
  // Off by default, so a repo that publishes some other way can't release by accident.
304
243
  if (!releaseEnabled(root))
305
244
  return out("release not enabled");
306
245
  await assertGhAuth(exec);
307
- out(await release(exec, { root, lock, cli: fileURLToPath(import.meta.url), prTitle, log }));
246
+ out(await release(exec, {
247
+ root,
248
+ lock,
249
+ cli: fileURLToPath(import.meta.url),
250
+ prTitle,
251
+ log,
252
+ envToken,
253
+ }));
308
254
  }
309
255
  async function runRenovate([flag, ...extra]) {
310
256
  const cli = fileURLToPath(import.meta.url);
@@ -0,0 +1 @@
1
+ export { checkLockPath, lockCheck, type LockCheckOptions, withCheckLock } from "./check-lock.ts";
@@ -0,0 +1,2 @@
1
+ // The `@jonmilner/local-ci/lock` entry: only the public check-lock API, not the CLI's internals.
2
+ export { checkLockPath, lockCheck, withCheckLock } from "./check-lock.js";
package/dist/lock.d.ts CHANGED
@@ -1,3 +1,14 @@
1
+ import { type WaitOptions } from "./github.ts";
2
+ /** Whether a process with this pid is running. */
3
+ export declare function alive(pid: number): boolean;
4
+ export interface ExitWaitOptions extends WaitOptions {
5
+ alive?: (pid: number) => boolean;
6
+ }
7
+ /**
8
+ * Polls until `pid` exits, returning false if it is still running after `timeoutMs`. git sends a
9
+ * push only after its pre-push hook exits, so the poster waits on git before waiting on GitHub.
10
+ */
11
+ export declare function waitForExit(pid: number, options?: ExitWaitOptions): Promise<boolean>;
1
12
  /** A live process holds the lock. `holder` is what it wrote after its pid, such as its repo. */
2
13
  export declare class LockHeld extends Error {
3
14
  readonly pid: number;
package/dist/lock.js CHANGED
@@ -2,7 +2,7 @@ import { linkSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs
2
2
  import { uptime } from "node:os";
3
3
  import { defaultSleep } from "./github.js";
4
4
  /** Whether a process with this pid is running. */
5
- function alive(pid) {
5
+ export function alive(pid) {
6
6
  if (!Number.isInteger(pid) || pid <= 0)
7
7
  return false;
8
8
  try {
@@ -14,6 +14,20 @@ function alive(pid) {
14
14
  return error.code === "EPERM";
15
15
  }
16
16
  }
17
+ /**
18
+ * Polls until `pid` exits, returning false if it is still running after `timeoutMs`. git sends a
19
+ * push only after its pre-push hook exits, so the poster waits on git before waiting on GitHub.
20
+ */
21
+ export async function waitForExit(pid, options = {}) {
22
+ const { intervalMs = 1_000, timeoutMs = 3_600_000 } = options;
23
+ const { sleep = defaultSleep, alive: running = alive } = options;
24
+ for (let waited = 0; running(pid); waited += intervalMs) {
25
+ if (waited + intervalMs > timeoutMs)
26
+ return false;
27
+ await sleep(intervalMs);
28
+ }
29
+ return true;
30
+ }
17
31
  /**
18
32
  * Whether the lock file was written before the machine booted, so its pid, alive or not, belongs to
19
33
  * a process that never held it.
@@ -0,0 +1,3 @@
1
+ import type { ProcessRow } from "./push.ts";
2
+ /** Every process's parent and command, or an empty table when `ps` can't list them. */
3
+ export declare function processTable(): Map<number, ProcessRow>;
@@ -0,0 +1,17 @@
1
+ import { execFileSync } from "node:child_process";
2
+ /** Every process's parent and command, or an empty table when `ps` can't list them. */
3
+ export function processTable() {
4
+ const table = new Map();
5
+ try {
6
+ const listing = execFileSync("ps", ["-A", "-o", "pid=,ppid=,comm="], { encoding: "utf8" });
7
+ for (const line of listing.split("\n")) {
8
+ const match = /^\s*(\d+)\s+(\d+)\s?(.*)$/.exec(line);
9
+ if (match)
10
+ table.set(Number(match[1]), { ppid: Number(match[2]), command: match[3].trim() });
11
+ }
12
+ }
13
+ catch {
14
+ return new Map();
15
+ }
16
+ return table;
17
+ }
package/dist/push.d.ts CHANGED
@@ -15,3 +15,11 @@ export declare function shasToCheck(refs: PushedRef[]): string[];
15
15
  export declare function cannotCheckInPlace(sha: string, head: string, porcelain: string): string | null;
16
16
  /** `owner/repo` from a GitHub remote URL (SSH or HTTPS), or undefined for another host. */
17
17
  export declare function githubRepo(url: string): string | undefined;
18
+ export interface ProcessRow {
19
+ ppid: number;
20
+ command: string;
21
+ }
22
+ /** The pids above a process, nearest first, walking a pid → process table up from its parent `ppid`. */
23
+ export declare function ancestorsFrom(table: Map<number, ProcessRow>, ppid: number): number[];
24
+ /** The nearest `git` process above `start`: the git that runs a hook sends the push once it exits. */
25
+ export declare function nearestGit(table: Map<number, ProcessRow>, start: number): number | undefined;
package/dist/push.js CHANGED
@@ -37,3 +37,15 @@ export function cannotCheckInPlace(sha, head, porcelain) {
37
37
  export function githubRepo(url) {
38
38
  return /github\.com[:/]([^/]+\/[^/]+?)(?:\.git)?\/?$/.exec(url)?.[1];
39
39
  }
40
+ /** The pids above a process, nearest first, walking a pid → process table up from its parent `ppid`. */
41
+ export function ancestorsFrom(table, ppid) {
42
+ const found = [];
43
+ for (let pid = ppid; pid > 1 && !found.includes(pid); pid = table.get(pid)?.ppid ?? 0) {
44
+ found.push(pid);
45
+ }
46
+ return found;
47
+ }
48
+ /** The nearest `git` process above `start`: the git that runs a hook sends the push once it exits. */
49
+ export function nearestGit(table, start) {
50
+ return ancestorsFrom(table, table.get(start)?.ppid ?? 0).find((pid) => /(^|\/)git$/.test(table.get(pid)?.command ?? ""));
51
+ }
package/dist/release.d.ts CHANGED
@@ -1,7 +1,9 @@
1
1
  import { type Exec } from "./exec.ts";
2
2
  import { type WaitOptions } from "./github.ts";
3
- /** The macOS Keychain generic password that holds the npm token. */
4
- export declare const TOKEN_SERVICE = "local-ci-npm";
3
+ /** The npm token's name, both as an environment variable and in the `secret` store. */
4
+ export declare const TOKEN_NAME = "NPM_TOKEN_JONMILNER";
5
+ /** How many days ahead of the token's expiry a release starts warning. */
6
+ export declare const EXPIRY_WARN_DAYS = 14;
5
7
  /** npm and pnpm read a registry token from this variable; it is set only for `pnpm release`. */
6
8
  export declare const TOKEN_ENV = "NPM_CONFIG_//registry.npmjs.org/:_authToken";
7
9
  export interface Package {
@@ -16,8 +18,28 @@ export declare function versionCommand(dir: string): string[];
16
18
  export declare function publicPackages(exec: Exec, dir: string): Promise<Package[]>;
17
19
  /** Whether npm has this exact version. A 404 means no; any other error is thrown. */
18
20
  export declare function isPublished(exec: Exec, pkg: Package): Promise<boolean>;
19
- /** Reads the npm token from the Keychain, failing with the setup doc when it is missing. */
20
- export declare function readToken(exec: Exec): Promise<string>;
21
+ export interface Token {
22
+ value: string;
23
+ /** Where it came from, for the message when npm rejects it. */
24
+ source: string;
25
+ }
26
+ /**
27
+ * The warning for a token in the `secret` store that expires within EXPIRY_WARN_DAYS or has
28
+ * expired, from `secret list` (names and expiry dates, never values). Nothing when `secret list`
29
+ * fails or gives the token no expiry, since the warning is advice and local-ci needs no store.
30
+ */
31
+ export declare function expiryWarning(exec: Exec, now?: Date): Promise<string | undefined>;
32
+ /**
33
+ * The npm token: $NPM_TOKEN_JONMILNER if set, else `secret get NPM_TOKEN_JONMILNER`, warning
34
+ * through `log` when the stored one is near expiry. Fails with the setup doc when neither has it.
35
+ * Removes the variable from `env`, so only `pnpm release` gets the token, not every child.
36
+ */
37
+ /**
38
+ * Takes $NPM_TOKEN_JONMILNER out of `env`, so no child the release runs (git, npm, the pre-push
39
+ * hook) inherits it; only `pnpm release` gets the token, under npm's own variable.
40
+ */
41
+ export declare function takeEnvToken(env?: NodeJS.ProcessEnv): string | undefined;
42
+ export declare function readToken(exec: Exec, log: (message: string) => void, env?: NodeJS.ProcessEnv, now?: Date): Promise<Token>;
21
43
  /**
22
44
  * Polls npm until each package is visible. The registry can take minutes to list a version
23
45
  * after a publish, so a timeout here means the publish ran but npm hasn't caught up yet.
@@ -32,6 +54,8 @@ export interface ReleaseContext {
32
54
  /** Lints the PR title and posts `pr-title`, resolving false when the title fails. */
33
55
  prTitle: (pr: string) => Promise<boolean>;
34
56
  log: (message: string) => void;
57
+ /** $NPM_TOKEN_JONMILNER, taken out of process.env by takeEnvToken before the CLI ran anything. */
58
+ envToken?: string;
35
59
  }
36
60
  /**
37
61
  * Versions pending changesets through a merged PR, publishes what npm lacks, pushes the tags and
package/dist/release.js CHANGED
@@ -5,8 +5,10 @@ import { basename, dirname, join } from "node:path";
5
5
  import { run } from "./exec.js";
6
6
  import { defaultSleep, waitForStatuses } from "./github.js";
7
7
  import { takeLock } from "./lock.js";
8
- /** The macOS Keychain generic password that holds the npm token. */
9
- export const TOKEN_SERVICE = "local-ci-npm";
8
+ /** The npm token's name, both as an environment variable and in the `secret` store. */
9
+ export const TOKEN_NAME = "NPM_TOKEN_JONMILNER";
10
+ /** How many days ahead of the token's expiry a release starts warning. */
11
+ export const EXPIRY_WARN_DAYS = 14;
10
12
  /** npm and pnpm read a registry token from this variable; it is set only for `pnpm release`. */
11
13
  export const TOKEN_ENV = "NPM_CONFIG_//registry.npmjs.org/:_authToken";
12
14
  const SETUP_DOC = "https://github.com/jonmilner/config/blob/main/docs/new-machine.md#npm-publish-token";
@@ -56,19 +58,74 @@ async function unpublished(exec, packages) {
56
58
  const published = await Promise.all(packages.map((pkg) => isPublished(exec, pkg)));
57
59
  return packages.filter((_, index) => !published[index]);
58
60
  }
59
- /** Reads the npm token from the Keychain, failing with the setup doc when it is missing. */
60
- export async function readToken(exec) {
61
- let token = "";
61
+ const ROTATE = `secret set ${TOKEN_NAME} --expires <YYYY-MM-DD>`;
62
+ const DAY_MS = 86_400_000;
63
+ /**
64
+ * The warning for a token in the `secret` store that expires within EXPIRY_WARN_DAYS or has
65
+ * expired, from `secret list` (names and expiry dates, never values). Nothing when `secret list`
66
+ * fails or gives the token no expiry, since the warning is advice and local-ci needs no store.
67
+ */
68
+ export async function expiryWarning(exec, now = new Date()) {
69
+ let listing;
62
70
  try {
63
- token = (await exec("security", ["find-generic-password", "-s", TOKEN_SERVICE, "-w"])).trim();
71
+ listing = await exec("secret", ["list"]);
64
72
  }
65
73
  catch {
66
- // Not found, or the Keychain is locked: either way there is no token to use.
74
+ return undefined;
75
+ }
76
+ const line = listing.split("\n").find((entry) => entry.startsWith(`${TOKEN_NAME}\t`));
77
+ const date = /\texpire[sd] (\d{4}-\d{2}-\d{2})\b/.exec(line ?? "")?.[1];
78
+ if (!date)
79
+ return undefined;
80
+ const [year, month, day] = date.split("-").map(Number);
81
+ // Calendar days in the local date, as `secret list` counts them, so the time of day doesn't matter.
82
+ const today = Date.UTC(now.getFullYear(), now.getMonth(), now.getDate());
83
+ const days = Math.round((Date.UTC(year, month - 1, day) - today) / DAY_MS);
84
+ if (days < 0) {
85
+ return `warning: the npm token ${TOKEN_NAME} expired ${date}, so npm will likely reject it; rotate it with: ${ROTATE} (${SETUP_DOC})`;
86
+ }
87
+ if (days > EXPIRY_WARN_DAYS)
88
+ return undefined;
89
+ const left = days === 0 ? "today" : `in ${days} day${days === 1 ? "" : "s"}`;
90
+ return `warning: the npm token ${TOKEN_NAME} expires ${date} (${left}); rotate it with: ${ROTATE} (${SETUP_DOC})`;
91
+ }
92
+ /**
93
+ * The npm token: $NPM_TOKEN_JONMILNER if set, else `secret get NPM_TOKEN_JONMILNER`, warning
94
+ * through `log` when the stored one is near expiry. Fails with the setup doc when neither has it.
95
+ * Removes the variable from `env`, so only `pnpm release` gets the token, not every child.
96
+ */
97
+ /**
98
+ * Takes $NPM_TOKEN_JONMILNER out of `env`, so no child the release runs (git, npm, the pre-push
99
+ * hook) inherits it; only `pnpm release` gets the token, under npm's own variable.
100
+ */
101
+ export function takeEnvToken(env = process.env) {
102
+ const value = env[TOKEN_NAME];
103
+ delete env[TOKEN_NAME];
104
+ return value;
105
+ }
106
+ export async function readToken(exec, log, env = process.env, now = new Date()) {
107
+ const fromEnv = env[TOKEN_NAME]?.trim();
108
+ delete env[TOKEN_NAME];
109
+ if (fromEnv)
110
+ return { value: fromEnv, source: `$${TOKEN_NAME}` };
111
+ let value = "";
112
+ let reason = "";
113
+ try {
114
+ value = (await exec("secret", ["get", TOKEN_NAME])).trim();
115
+ }
116
+ catch (error) {
117
+ // Not installed, no such name, or the store won't decrypt: none of them gives a token.
118
+ const message = error instanceof Error ? error.message : String(error);
119
+ // Only the spawn's own ENOENT; one in secret's stderr is a file it lacks, shown as is.
120
+ reason = /\bspawn secret ENOENT\b/.test(message) ? "secret is not installed" : message;
67
121
  }
68
- if (!token) {
69
- throw new Error(`no npm token in the macOS Keychain (service ${TOKEN_SERVICE}). Create and store one: ${SETUP_DOC}`);
122
+ if (!value) {
123
+ throw new Error(`no npm token: $${TOKEN_NAME} is unset and \`secret get ${TOKEN_NAME}\` gave none${reason ? ` (${reason})` : ""}. Store one with \`${ROTATE}\`: ${SETUP_DOC}`);
70
124
  }
71
- return token;
125
+ const warning = await expiryWarning(exec, now);
126
+ if (warning)
127
+ log(warning);
128
+ return { value, source: `\`secret get ${TOKEN_NAME}\`` };
72
129
  }
73
130
  /** The hint for checking each package on npm by hand. */
74
131
  const npmView = (packages) => packages.length
@@ -85,7 +142,7 @@ function publish(dir, token, packages, signal) {
85
142
  // Its own session and group, so an interrupt stops the publish it started, not just pnpm.
86
143
  detached: true,
87
144
  // CI makes changesets fail on an OTP request instead of prompting on an ignored stdin.
88
- env: { ...process.env, CI: "true", [TOKEN_ENV]: token },
145
+ env: { ...process.env, CI: "true", [TOKEN_ENV]: token.value },
89
146
  stdio: ["ignore", "pipe", "pipe"],
90
147
  });
91
148
  // The group no longer gets the terminal's signals, so these are the only things that stop it.
@@ -141,7 +198,7 @@ function publish(dir, token, packages, signal) {
141
198
  reject(new Error(publishOver
142
199
  ? `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)}`
143
200
  : authError
144
- ? `npm rejected the token in the Keychain (service ${TOKEN_SERVICE}); it may have expired or lack "bypass 2FA". Rotate it: ${SETUP_DOC}`
201
+ ? `npm rejected the token from ${token.source}; it may have expired or lack "bypass 2FA". Rotate it with \`${ROTATE}\`: ${SETUP_DOC}`
145
202
  : `pnpm release failed (${code === null ? killed : `exit ${code}`})`));
146
203
  });
147
204
  });
@@ -307,7 +364,7 @@ export async function release(exec, context) {
307
364
  }
308
365
  // Read it before changing anything, so a missing token stops the run with nothing to undo.
309
366
  await live();
310
- const token = await readToken(exec);
367
+ const token = await readToken(exec, log, { [TOKEN_NAME]: context.envToken });
311
368
  if (pending.length) {
312
369
  branch = `release/${await stepGit(dir, "rev-parse", "--short", "HEAD")}`;
313
370
  await pruneStale(stepGit, root, dir, branch);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jonmilner/local-ci",
3
- "version": "0.8.0",
3
+ "version": "0.10.0",
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": {
@@ -17,6 +17,13 @@
17
17
  "README.md"
18
18
  ],
19
19
  "type": "module",
20
+ "exports": {
21
+ "./lock": {
22
+ "types": "./dist/lock-entry.d.ts",
23
+ "default": "./dist/lock-entry.js"
24
+ },
25
+ "./package.json": "./package.json"
26
+ },
20
27
  "publishConfig": {
21
28
  "access": "public"
22
29
  },