@jonmilner/local-ci 0.11.1 → 0.13.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 +5 -2
- package/dist/check-lock.d.ts +6 -4
- package/dist/check-lock.js +70 -27
- package/dist/cli.js +54 -18
- package/dist/lock.d.ts +4 -0
- package/dist/lock.js +29 -5
- package/dist/release.d.ts +7 -0
- package/dist/release.js +65 -9
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -26,6 +26,7 @@ That writes `pre-push` and `commit-msg` shims, each one line that calls local-ci
|
|
|
26
26
|
- `local-ci post <ref>`: resolves `<ref>` (a full or short sha, `HEAD`, a branch) to a local commit's full sha, failing with `<ref> is not a commit in this repo` when it names none, and with `<sha> has no passing check` when that commit never passed. 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
|
+
- `local-ci run -- <command>`: runs `<command>` under the machine-wide check lock (see [State](#state)), for a full check started by hand or from a script: `local-ci run -- pnpm check`. It waits its turn with every other check, and they wait for it; like any check, it runs beside a background job rather than waiting for one (see [State](#state)). The arguments after `--` go to the program as they are, with no shell (`local-ci run -- sh -c 'a && b'` for a shell line). Stdin is `/dev/null` and the command has no controlling terminal, so anything that prompts on `/dev/tty` fails; output goes straight through. It exits with the command's status, 128 plus the signal number if a signal killed the command, or 1 if the command can't start. It works outside a git repo, needs no `gh` login, posts no status and records no pass. Inside a check that already holds the lock it runs at once, so a repo's `check` script can wrap itself in `local-ci run` without deadlocking its own pre-push.
|
|
29
30
|
- `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:
|
|
30
31
|
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.
|
|
31
32
|
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.
|
|
@@ -33,6 +34,8 @@ That writes `pre-push` and `commit-msg` shims, each one line that calls local-ci
|
|
|
33
34
|
|
|
34
35
|
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
36
|
|
|
37
|
+
- `local-ci release --check`: says whether `local-ci release` has anything to do, and exits 0. It prints `release not enabled` (the same switch as above), `nothing to release`, or `pending: ` followed by what a run would act on at `origin`'s default branch: `changesets <files>`, `unpublished <name@version>` and `unpushed tags <name@version>`, joined by `; `. It fetches `origin`'s objects and extracts the branch's manifests and changesets to a temporary folder, but writes no ref, worktree or lock, needs no clean working tree or token, and never publishes, pushes or opens a PR, so it can run beside a release. Like `release`, it reads the switch from the checkout it runs in.
|
|
38
|
+
|
|
36
39
|
- `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
40
|
- skips a repo with no such clone, logging why;
|
|
38
41
|
- skips a head commit that already has a `check` status, posting `pr-title` if that is missing, so a failed check is not rerun on every sweep (rerun it with `local-ci status <sha>` in a checkout of the head);
|
|
@@ -58,7 +61,7 @@ A repo without a `package.json` sets `LOCAL_CI_COMMAND`, for example on the hook
|
|
|
58
61
|
|
|
59
62
|
Logs, the record of passed commits and the release lock live in `~/.local/state/local-ci/<repo>.log`, `<repo>.passed` and `<repo>.release.lock` (`$XDG_STATE_HOME` if set). A lock whose process has died is taken over.
|
|
60
63
|
|
|
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`,
|
|
64
|
+
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`, the `status` runs `renovate` starts, and `local-ci run` each take `check.lock` in the same directory around the command they run (not the status posts). Nothing else takes it: a bare `pnpm check` or `pnpm test` runs beside every other check, so start a full check by hand with `local-ci run -- pnpm check`. The lock only runs checks one at a time; it does not wait for memory pressure or swap to clear. A run that finds it held prints `local-ci: waiting for the check lock, held by pid <pid> (<repo> <short-sha>)` (`<repo> <command>` for `local-ci run`, with the command cut to 60 characters), 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, SIGHUP or SIGQUIT (Ctrl-\\) 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. A check that finds a background job's lock (see below) runs beside the job instead of waiting and leaves that lock alone, printing `local-ci: running beside background job pid <pid> (<holder>), without the check lock`; it holds `check.lock.beside` meanwhile, so another check waits for it rather than overlapping, and signals still stop it and release that lock as above. 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
65
|
|
|
63
66
|
### Taking the check lock from your own scripts
|
|
64
67
|
|
|
@@ -72,6 +75,6 @@ await withCheckLock("my-repo browser", async () => {
|
|
|
72
75
|
});
|
|
73
76
|
```
|
|
74
77
|
|
|
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. Pass `options.background: true` for a long job run after a push, such as a browser suite: it still waits for a running check, but `local-ci` checks (and other non-background callers) run
|
|
78
|
+
`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. Pass `options.background: true` for a long job run after a push, such as a browser suite: it still waits for a running check, but `local-ci` checks (and other non-background callers) run beside it instead of waiting, so a push never sits in its hook behind its run. Background jobs still queue behind each other. A check run beside a background job takes `check.lock.beside` instead, so checks still run one at a time: a second check started during the job waits for the first, and whoever takes `check.lock` after the background job ends (another background job or a check) waits for a check still running beside it. A background job waiting that way holds `check.lock` as a check would, without the `background` line, until the beside check ends, so checks arriving meanwhile wait in turn rather than run beside a job that has not started. A check nested in a beside check runs under its lock without waiting. A beside lock whose holder died is taken over, as `check.lock` is. Versions before the beside lock ignore it, so a check from such a version (another repo pinned to an older local-ci) can still run beside a newer beside check. The lock file marks such a lock with a third line, `background`; a file without it (as older versions wrote) is waited on as before. 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
79
|
|
|
77
80
|
Part of [jonmilner/config](https://github.com/jonmilner/config#readme).
|
package/dist/check-lock.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ChildProcess } from "node:child_process";
|
|
2
|
-
import {
|
|
2
|
+
import { LockHeld } from "./lock.ts";
|
|
3
3
|
/** The check lock this process holds through `lockCheck`, if any, for the CLI to attach its check to. */
|
|
4
4
|
export declare function heldCheckLock(): {
|
|
5
5
|
child?: ChildProcess;
|
|
@@ -22,9 +22,11 @@ export declare function checkLockPath(env?: NodeJS.ProcessEnv): string;
|
|
|
22
22
|
/**
|
|
23
23
|
* Takes the machine-wide check lock, waiting while another check or job holds it, and returns its
|
|
24
24
|
* release. `holder` names the caller in other waiters' messages. A caller nested under an ancestor
|
|
25
|
-
* that holds the lock
|
|
26
|
-
*
|
|
27
|
-
*
|
|
25
|
+
* that holds the lock runs without waiting, and its release leaves the lock alone. A check (not
|
|
26
|
+
* `background`) that finds a background job holding it runs beside the job under a second lock,
|
|
27
|
+
* `<file>.beside`, instead: a second such check waits for it, and whoever takes the check lock
|
|
28
|
+
* next waits for it to clear. SIGINT, SIGTERM, SIGHUP and SIGQUIT release the lock and end the
|
|
29
|
+
* process.
|
|
28
30
|
*/
|
|
29
31
|
export declare function lockCheck(holder: string, { file, onWait, background }?: LockCheckOptions): Promise<() => void>;
|
|
30
32
|
/** Runs `fn` under the machine-wide check lock, so it queues behind `local-ci` checks and vice versa. */
|
package/dist/check-lock.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { mkdirSync } from "node:fs";
|
|
2
2
|
import { join } from "node:path";
|
|
3
3
|
import { stateDir } from "./config.js";
|
|
4
|
-
import { waitForLock } from "./lock.js";
|
|
4
|
+
import { LockHeld, lockHolder, markLock, takeLock, waitForLock } from "./lock.js";
|
|
5
5
|
import { processTable } from "./processes.js";
|
|
6
6
|
import { ancestorsFrom } from "./push.js";
|
|
7
7
|
/** This process's ancestors' pids, or just its parent when `ps` can't list them. */
|
|
8
8
|
function ancestors() {
|
|
9
9
|
return new Set(ancestorsFrom(processTable(), process.ppid));
|
|
10
10
|
}
|
|
11
|
-
const SIGNALS = ["SIGINT", "SIGTERM", "SIGHUP"];
|
|
11
|
+
const SIGNALS = ["SIGINT", "SIGTERM", "SIGHUP", "SIGQUIT"];
|
|
12
12
|
/** The check lock this process holds, the check running under it, and a signal it deferred. */
|
|
13
13
|
let held;
|
|
14
14
|
/** The check lock this process holds through `lockCheck`, if any, for the CLI to attach its check to. */
|
|
@@ -43,7 +43,7 @@ function onSignal(signal) {
|
|
|
43
43
|
held = undefined;
|
|
44
44
|
reraise(signal);
|
|
45
45
|
}
|
|
46
|
-
/** Holds `unlock` until the returned release, releasing it on SIGINT, SIGTERM or
|
|
46
|
+
/** Holds `unlock` until the returned release, releasing it on SIGINT, SIGTERM, SIGHUP or SIGQUIT too. */
|
|
47
47
|
function releaseOnSignal(unlock) {
|
|
48
48
|
if (!process.listenerCount("SIGINT", onSignal)) {
|
|
49
49
|
for (const name of SIGNALS)
|
|
@@ -65,43 +65,86 @@ export function checkLockPath(env = process.env) {
|
|
|
65
65
|
mkdirSync(dir, { recursive: true });
|
|
66
66
|
return join(dir, "check.lock");
|
|
67
67
|
}
|
|
68
|
+
/** The lock a check takes to run beside a background job, next to the check lock it skips. */
|
|
69
|
+
function besideLockPath(file) {
|
|
70
|
+
return `${file}.beside`;
|
|
71
|
+
}
|
|
68
72
|
/**
|
|
69
73
|
* Takes the machine-wide check lock, waiting while another check or job holds it, and returns its
|
|
70
74
|
* release. `holder` names the caller in other waiters' messages. A caller nested under an ancestor
|
|
71
|
-
* that holds the lock
|
|
72
|
-
*
|
|
73
|
-
*
|
|
75
|
+
* that holds the lock runs without waiting, and its release leaves the lock alone. A check (not
|
|
76
|
+
* `background`) that finds a background job holding it runs beside the job under a second lock,
|
|
77
|
+
* `<file>.beside`, instead: a second such check waits for it, and whoever takes the check lock
|
|
78
|
+
* next waits for it to clear. SIGINT, SIGTERM, SIGHUP and SIGQUIT release the lock and end the
|
|
79
|
+
* process.
|
|
74
80
|
*/
|
|
75
81
|
export async function lockCheck(holder, { file = checkLockPath(), onWait, background = false } = {}) {
|
|
76
|
-
|
|
82
|
+
const sidecar = besideLockPath(file);
|
|
77
83
|
let mine;
|
|
78
84
|
// A check nested in a check (one that pushes, say) runs under its ancestor's lock rather than
|
|
79
|
-
// waiting forever on it.
|
|
85
|
+
// waiting forever on it, whether the ancestor holds the check lock or runs beside a background job.
|
|
80
86
|
const nested = (held) => (mine ??= ancestors()).has(held.pid);
|
|
87
|
+
const outer = lockHolder(sidecar, "check");
|
|
88
|
+
if (outer && nested(outer))
|
|
89
|
+
return () => { };
|
|
81
90
|
// A check never waits out a background job's long run; it runs beside it.
|
|
82
|
-
const
|
|
83
|
-
|
|
84
|
-
return true;
|
|
85
|
-
if (!held.background || background)
|
|
86
|
-
return false;
|
|
87
|
-
beside = held;
|
|
88
|
-
return true;
|
|
89
|
-
};
|
|
90
|
-
const unlock = await waitForLock(file, {
|
|
91
|
+
const besides = (held) => held.background && !background && !nested(held);
|
|
92
|
+
const options = {
|
|
91
93
|
what: "check",
|
|
92
94
|
holder,
|
|
93
|
-
background,
|
|
94
|
-
skip,
|
|
95
95
|
onWait: onWait ??
|
|
96
96
|
(({ pid, holder }) => process.stderr.write(`local-ci: waiting for the check lock, held by pid ${pid} (${holder || "unknown"})\n`)),
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
97
|
+
};
|
|
98
|
+
for (;;) {
|
|
99
|
+
let beside = false;
|
|
100
|
+
const skip = (held) => nested(held) || (beside = besides(held));
|
|
101
|
+
// Taken as a check's lock even for a background job, so checks wait for it rather than run
|
|
102
|
+
// beside it until it starts its run.
|
|
103
|
+
const unlock = await waitForLock(file, { ...options, skip });
|
|
104
|
+
if (unlock) {
|
|
105
|
+
const release = releaseOnSignal(unlock);
|
|
106
|
+
try {
|
|
107
|
+
// A check still running beside a background job that has since ended goes first.
|
|
108
|
+
(await waitForLock(sidecar, { ...options, skip: nested }))?.();
|
|
109
|
+
if (background)
|
|
110
|
+
markLock(file, holder, { background });
|
|
111
|
+
}
|
|
112
|
+
catch (error) {
|
|
113
|
+
release();
|
|
114
|
+
throw error;
|
|
115
|
+
}
|
|
116
|
+
return release;
|
|
117
|
+
}
|
|
118
|
+
if (!beside)
|
|
119
|
+
return () => { };
|
|
120
|
+
// One check at a time beside a background job.
|
|
121
|
+
const unside = await waitForLock(sidecar, { ...options, skip: nested });
|
|
122
|
+
if (!unside)
|
|
123
|
+
return () => { };
|
|
124
|
+
// The background job may have ended, or another taken its place, while this waited.
|
|
125
|
+
let now;
|
|
126
|
+
try {
|
|
127
|
+
const taken = takeLock(file, "check", holder);
|
|
128
|
+
// Holding the beside lock, nothing else runs beside: the check lock alone is enough.
|
|
129
|
+
unside();
|
|
130
|
+
return releaseOnSignal(taken);
|
|
131
|
+
}
|
|
132
|
+
catch (error) {
|
|
133
|
+
if (!(error instanceof LockHeld)) {
|
|
134
|
+
unside();
|
|
135
|
+
throw error;
|
|
136
|
+
}
|
|
137
|
+
now = error;
|
|
138
|
+
}
|
|
139
|
+
if (besides(now)) {
|
|
140
|
+
process.stderr.write(`local-ci: running beside background job pid ${now.pid} (${now.holder || "unknown"}), without the check lock\n`);
|
|
141
|
+
return releaseOnSignal(unside);
|
|
142
|
+
}
|
|
143
|
+
unside();
|
|
144
|
+
if (nested(now))
|
|
145
|
+
return () => { };
|
|
146
|
+
// A check holds the lock now: wait for it as usual.
|
|
147
|
+
}
|
|
105
148
|
}
|
|
106
149
|
/** Runs `fn` under the machine-wide check lock, so it queues behind `local-ci` checks and vice versa. */
|
|
107
150
|
export async function withCheckLock(holder, fn, options) {
|
package/dist/cli.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { spawn, spawnSync } from "node:child_process";
|
|
3
3
|
import { closeSync, openSync, readFileSync } from "node:fs";
|
|
4
|
-
import { hostname } from "node:os";
|
|
4
|
+
import { constants, 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
|
+
import { heldCheckLock, lockCheck, withCheckLock } from "./check-lock.js";
|
|
8
8
|
import { commitMsg } from "./commit-msg.js";
|
|
9
9
|
import { appendLog, checkRunMessage, logLine, passedCommand, recordPass, releaseEnabled, resolveCommand, shimCommand, statePaths, } from "./config.js";
|
|
10
10
|
import { exec, run } from "./exec.js";
|
|
@@ -14,7 +14,7 @@ import { installRenovateAgent, renovateLog, rotateLog, uninstallRenovateAgent }
|
|
|
14
14
|
import { waitForExit } from "./lock.js";
|
|
15
15
|
import { processTable } from "./processes.js";
|
|
16
16
|
import { cannotCheckInPlace, githubRepo, nearestGit, parsePrePush, shasToCheck } from "./push.js";
|
|
17
|
-
import { release, takeEnvToken } from "./release.js";
|
|
17
|
+
import { release, releaseCheck, takeEnvToken } from "./release.js";
|
|
18
18
|
import { projectsDir, renovate } from "./renovate.js";
|
|
19
19
|
import { lintTitle } from "./title.js";
|
|
20
20
|
const USAGE = `Usage:
|
|
@@ -25,7 +25,10 @@ const USAGE = `Usage:
|
|
|
25
25
|
lint a commit message file (git commit-msg hook) or stdin (-)
|
|
26
26
|
local-ci hooks install write the repo's pre-push and commit-msg hook shims
|
|
27
27
|
local-ci status [<sha>] post check for <sha> (default HEAD), running the check if needed
|
|
28
|
+
local-ci run -- <command>
|
|
29
|
+
run a command under the machine-wide check lock
|
|
28
30
|
local-ci release version pending changesets through a PR, publish to npm and push tags
|
|
31
|
+
local-ci release --check say whether release has anything to do, changing nothing
|
|
29
32
|
local-ci renovate check each open Renovate PR in your repos and post check and pr-title
|
|
30
33
|
local-ci renovate --install
|
|
31
34
|
run renovate every 15 minutes with a launchd agent (macOS)
|
|
@@ -90,29 +93,34 @@ async function runCheck(current, sha) {
|
|
|
90
93
|
unlock();
|
|
91
94
|
}
|
|
92
95
|
}
|
|
93
|
-
/** Runs
|
|
94
|
-
async function
|
|
96
|
+
/** Runs a program under the check lock, attached to it so a signal to local-ci reaches it. */
|
|
97
|
+
async function runHeld(file, args, cwd) {
|
|
95
98
|
const held = heldCheckLock();
|
|
96
|
-
const
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
cwd: root,
|
|
101
|
-
// Its own process group, so onSignal can stop everything the check started. A nested check
|
|
102
|
-
// has no handler and stays in its parent check's group, which that check's signal reaches.
|
|
99
|
+
const child = spawn(file, args, {
|
|
100
|
+
cwd,
|
|
101
|
+
// Its own process group, so onSignal can stop everything it started. A nested check has no
|
|
102
|
+
// handler and stays in its parent check's group, which that check's signal reaches.
|
|
103
103
|
detached: held !== undefined,
|
|
104
104
|
stdio: ["ignore", "inherit", "inherit"],
|
|
105
105
|
});
|
|
106
106
|
if (held)
|
|
107
107
|
held.child = child;
|
|
108
108
|
const result = await new Promise((resolve) => {
|
|
109
|
-
child.on("error", (error) => resolve({ status: null,
|
|
110
|
-
child.on("close", (status, signal) => resolve({ status,
|
|
109
|
+
child.on("error", (error) => resolve({ status: null, error: error.message }));
|
|
110
|
+
child.on("close", (status, signal) => resolve({ status, signal }));
|
|
111
111
|
});
|
|
112
112
|
if (held)
|
|
113
113
|
held.child = undefined;
|
|
114
|
+
return { ...result, interrupted: held?.signal };
|
|
115
|
+
}
|
|
116
|
+
/** Runs the check command under the check lock, logging its duration and recording a pass. */
|
|
117
|
+
async function runLocked({ root, passed, log: logFile }, sha) {
|
|
118
|
+
const command = resolveCommand(root);
|
|
119
|
+
err(`local-ci: running ${command} on ${sha.slice(0, 7)}`);
|
|
120
|
+
const started = performance.now();
|
|
121
|
+
const result = await runHeld("sh", ["-c", command], root);
|
|
114
122
|
// A check local-ci was told to stop never finished, whatever it exited with.
|
|
115
|
-
const interrupted =
|
|
123
|
+
const { interrupted } = result;
|
|
116
124
|
const status = interrupted ? null : result.status;
|
|
117
125
|
// Every run's duration goes in the log, so a faster check can be measured against a slower one.
|
|
118
126
|
// A log that can't be written must not change the check's outcome.
|
|
@@ -124,13 +132,32 @@ async function runLocked({ root, passed, log: logFile }, sha) {
|
|
|
124
132
|
}
|
|
125
133
|
// A spawn error or a signal means the check never finished, so it is not a failure to post.
|
|
126
134
|
if (status === null) {
|
|
127
|
-
const cause = interrupted ?? result.
|
|
135
|
+
const cause = interrupted ?? result.error ?? result.signal;
|
|
128
136
|
throw new Error(`${command} did not finish on ${sha.slice(0, 7)}: ${cause}`);
|
|
129
137
|
}
|
|
130
138
|
if (status !== 0)
|
|
131
139
|
throw new CheckFailed(command, sha);
|
|
132
140
|
recordPass(passed, sha, command);
|
|
133
141
|
}
|
|
142
|
+
/**
|
|
143
|
+
* Runs `-- <command>` under the check lock, so a full check started by hand waits its turn with
|
|
144
|
+
* the checks `pre-push` runs. Exits with the command's status, or 128 plus the signal that ended it.
|
|
145
|
+
*/
|
|
146
|
+
async function runUnderLock(args) {
|
|
147
|
+
const [separator, file, ...rest] = args;
|
|
148
|
+
if (separator !== "--" || !file)
|
|
149
|
+
throw new Error(`run: expected -- <command>\n${USAGE}`);
|
|
150
|
+
// Outside a git repo there is no repo name, and nothing else here needs one.
|
|
151
|
+
const name = (await repo().catch(() => undefined))?.name ?? basename(process.cwd());
|
|
152
|
+
// Other sessions print this while they wait, so a long script mustn't fill their output.
|
|
153
|
+
const command = [file, ...rest].join(" ");
|
|
154
|
+
const label = command.length > 60 ? `${command.slice(0, 57)}...` : command;
|
|
155
|
+
// After a signal the release dies of it, so a command told to stop never reports a status.
|
|
156
|
+
const result = await withCheckLock(`${name} ${label}`, () => runHeld(file, rest));
|
|
157
|
+
if (result.error)
|
|
158
|
+
throw new Error(`could not run ${file}: ${result.error}`);
|
|
159
|
+
process.exitCode = result.status ?? 128 + constants.signals[result.signal];
|
|
160
|
+
}
|
|
134
161
|
/** The repo root, its hooks directory, and the command its hook shims run local-ci with. */
|
|
135
162
|
async function hooksTarget(root) {
|
|
136
163
|
// `git config --get` exits 1 when the key is unset.
|
|
@@ -249,13 +276,20 @@ async function status(ref = "HEAD") {
|
|
|
249
276
|
}
|
|
250
277
|
await post(current, sha);
|
|
251
278
|
}
|
|
252
|
-
async function runRelease() {
|
|
279
|
+
async function runRelease(args) {
|
|
280
|
+
const check = args[0] === "--check";
|
|
281
|
+
const extra = check ? args.slice(1) : args;
|
|
282
|
+
// Any argument, even an empty one, other than a lone --check is refused, so a typo never publishes.
|
|
283
|
+
if (extra.length)
|
|
284
|
+
throw new Error(`release: unexpected ${JSON.stringify(extra.join(" "))}\n${USAGE}`);
|
|
253
285
|
// First, before any child process runs.
|
|
254
286
|
const envToken = takeEnvToken();
|
|
255
287
|
const { root, lock } = await repo();
|
|
256
288
|
// Off by default, so a repo that publishes some other way can't release by accident.
|
|
257
289
|
if (!releaseEnabled(root))
|
|
258
290
|
return out("release not enabled");
|
|
291
|
+
if (check)
|
|
292
|
+
return out(await releaseCheck(exec, root));
|
|
259
293
|
await assertGhAuth(exec);
|
|
260
294
|
out(await release(exec, {
|
|
261
295
|
root,
|
|
@@ -300,8 +334,10 @@ async function main([command, ...args]) {
|
|
|
300
334
|
}
|
|
301
335
|
if (command === "hooks")
|
|
302
336
|
return runHooks(args);
|
|
337
|
+
if (command === "run")
|
|
338
|
+
return runUnderLock(args);
|
|
303
339
|
if (command === "release")
|
|
304
|
-
return runRelease();
|
|
340
|
+
return runRelease(args);
|
|
305
341
|
if (command === "renovate")
|
|
306
342
|
return runRenovate(args);
|
|
307
343
|
if (command === "post" || command === "pr-title" || command === "status") {
|
package/dist/lock.d.ts
CHANGED
|
@@ -23,6 +23,10 @@ export interface TakeLockOptions {
|
|
|
23
23
|
/** Marks the lock as a background job's, on a third line, so checks need not wait behind it. */
|
|
24
24
|
background?: boolean;
|
|
25
25
|
}
|
|
26
|
+
/** The live process holding the lock, or `undefined` when it is free or its holder died. */
|
|
27
|
+
export declare function lockHolder(file: string, what?: string): LockHeld | undefined;
|
|
28
|
+
/** Rewrites a lock this process holds, as `takeLock` with these arguments would have written it. */
|
|
29
|
+
export declare function markLock(file: string, holder?: string, options?: TakeLockOptions): void;
|
|
26
30
|
/**
|
|
27
31
|
* Takes the lock and returns its release, or throws `LockHeld` while a live process holds it. The
|
|
28
32
|
* lock file holds the pid, then `holder` on a second line when given, then `background` on a third
|
package/dist/lock.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { linkSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
1
|
+
import { linkSync, readFileSync, renameSync, 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. */
|
|
@@ -65,6 +65,31 @@ function lockText(label, { background }) {
|
|
|
65
65
|
return `${process.pid}\n${holder ?? ""}\n${BACKGROUND}`;
|
|
66
66
|
return holder ? `${process.pid}\n${holder}` : String(process.pid);
|
|
67
67
|
}
|
|
68
|
+
/** The live holder `content`, the lock file's text, names, or `undefined` for a dead or stale one. */
|
|
69
|
+
function heldBy(file, what, content) {
|
|
70
|
+
const [pid = "", holder = "", flag] = content.split("\n");
|
|
71
|
+
if (!alive(Number(pid)) || beforeBoot(file))
|
|
72
|
+
return undefined;
|
|
73
|
+
return new LockHeld(what, file, Number(pid), holder, flag === BACKGROUND);
|
|
74
|
+
}
|
|
75
|
+
/** The live process holding the lock, or `undefined` when it is free or its holder died. */
|
|
76
|
+
export function lockHolder(file, what = "release") {
|
|
77
|
+
try {
|
|
78
|
+
return heldBy(file, what, readFileSync(file, "utf8"));
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
if (error.code === "ENOENT")
|
|
82
|
+
return undefined;
|
|
83
|
+
throw error;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/** Rewrites a lock this process holds, as `takeLock` with these arguments would have written it. */
|
|
87
|
+
export function markLock(file, holder, options = {}) {
|
|
88
|
+
// Renaming over the lock replaces it atomically, so nobody reads it empty or finds it free.
|
|
89
|
+
const mine = `${file}.${process.pid}`;
|
|
90
|
+
writeFileSync(mine, lockText(holder, options));
|
|
91
|
+
renameSync(mine, file);
|
|
92
|
+
}
|
|
68
93
|
/**
|
|
69
94
|
* Takes the lock and returns its release, or throws `LockHeld` while a live process holds it. The
|
|
70
95
|
* lock file holds the pid, then `holder` on a second line when given, then `background` on a third
|
|
@@ -94,10 +119,9 @@ export function takeLock(file, what = "release", holder, options = {}) {
|
|
|
94
119
|
continue;
|
|
95
120
|
throw readError;
|
|
96
121
|
}
|
|
97
|
-
const
|
|
98
|
-
if (
|
|
99
|
-
throw
|
|
100
|
-
}
|
|
122
|
+
const held = heldBy(file, what, content);
|
|
123
|
+
if (held)
|
|
124
|
+
throw held;
|
|
101
125
|
// The holder died without releasing it, or its pid is another process's since a reboot.
|
|
102
126
|
rmSync(file, { force: true });
|
|
103
127
|
}
|
package/dist/release.d.ts
CHANGED
|
@@ -45,6 +45,13 @@ export declare function readToken(exec: Exec, log: (message: string) => void, en
|
|
|
45
45
|
* after a publish, so a timeout here means the publish ran but npm hasn't caught up yet.
|
|
46
46
|
*/
|
|
47
47
|
export declare function verify(exec: Exec, packages: Package[], options?: WaitOptions): Promise<void>;
|
|
48
|
+
/**
|
|
49
|
+
* What `release` would act on at origin's default branch: `nothing to release`, or `pending: `
|
|
50
|
+
* with the changesets, unpublished versions and unpushed tags. Read-only: it fetches objects but
|
|
51
|
+
* writes no ref, worktree or lock, reads no token, and never publishes, pushes or opens a PR, so
|
|
52
|
+
* it cannot collide with a release running meanwhile.
|
|
53
|
+
*/
|
|
54
|
+
export declare function releaseCheck(exec: Exec, root: string): Promise<string>;
|
|
48
55
|
export interface ReleaseContext {
|
|
49
56
|
/** The checkout the command runs from. */
|
|
50
57
|
root: string;
|
package/dist/release.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { spawn } from "node:child_process";
|
|
2
|
-
import { existsSync, mkdtempSync, readdirSync, readFileSync, rmSync } from "node:fs";
|
|
2
|
+
import { existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync } from "node:fs";
|
|
3
3
|
import { constants, tmpdir } from "node:os";
|
|
4
4
|
import { basename, dirname, join } from "node:path";
|
|
5
5
|
import { run } from "./exec.js";
|
|
@@ -16,6 +16,9 @@ const VERSION_TITLE = "chore: version packages";
|
|
|
16
16
|
// How long verify() waits for npm to list what was just published.
|
|
17
17
|
const NPM_WAIT_MS = 300_000;
|
|
18
18
|
const TEMP_PREFIX = "local-ci-release-";
|
|
19
|
+
const CHECK_PREFIX = "local-ci-pending-";
|
|
20
|
+
// What `release --check` reads from the tree: the workspace's manifests and the changesets.
|
|
21
|
+
const CHECK_PATH = /(?:^|\/)package\.json$|^pnpm-workspace\.yaml$|^\.changeset\//;
|
|
19
22
|
// SIGHUP and SIGQUIT too, since the terminal sends them to node's group and `pnpm release` has its own.
|
|
20
23
|
const SIGNALS = ["SIGINT", "SIGTERM", "SIGHUP", "SIGQUIT"];
|
|
21
24
|
// What npm and pnpm print when the registry rejects the token, or wants an OTP it can't bypass.
|
|
@@ -234,13 +237,17 @@ export async function verify(exec, packages, options = {}) {
|
|
|
234
237
|
}
|
|
235
238
|
}
|
|
236
239
|
const gitWith = (exec) => async (cwd, ...args) => (await exec("git", ["-C", cwd, ...args])).trim();
|
|
237
|
-
/**
|
|
238
|
-
async function
|
|
240
|
+
/** The local `name@version` tags of `packages` that origin lacks. */
|
|
241
|
+
async function unpushedTags(git, dir, packages) {
|
|
239
242
|
const local = new Set((await git(dir, "tag", "--list")).split("\n"));
|
|
240
243
|
const remote = new Set((await git(dir, "ls-remote", "--tags", "origin"))
|
|
241
244
|
.split("\n")
|
|
242
245
|
.map((line) => line.split("\t")[1]?.replace(/^refs\/tags\//, "")));
|
|
243
|
-
|
|
246
|
+
return packages.map(id).filter((tag) => local.has(tag) && !remote.has(tag));
|
|
247
|
+
}
|
|
248
|
+
/** Pushes the local `name@version` tags of `packages` that origin lacks, resolving with their names. */
|
|
249
|
+
async function pushTags(git, run, dir, packages) {
|
|
250
|
+
const missing = await unpushedTags(git, dir, packages);
|
|
244
251
|
if (missing.length)
|
|
245
252
|
await run("git", ["push", "origin", ...missing.map((tag) => `refs/tags/${tag}`)], dir);
|
|
246
253
|
return missing;
|
|
@@ -272,6 +279,57 @@ async function pruneStale(git, root, own, branch) {
|
|
|
272
279
|
await git(root, "branch", "-D", name);
|
|
273
280
|
}
|
|
274
281
|
}
|
|
282
|
+
/** origin's default branch, without the `origin/` prefix; `main` when origin/HEAD is unset. */
|
|
283
|
+
async function defaultBranch(git, root) {
|
|
284
|
+
const remoteHead = await git(root, "symbolic-ref", "--short", "refs/remotes/origin/HEAD").catch(() => "origin/main");
|
|
285
|
+
return remoteHead.replace(/^origin\//, "");
|
|
286
|
+
}
|
|
287
|
+
/** What a release at `dir` acts on: the pending changesets, the public packages, and those npm lacks. */
|
|
288
|
+
async function plan(exec, dir) {
|
|
289
|
+
const changesets = pendingChangesets(dir);
|
|
290
|
+
const current = await publicPackages(exec, dir);
|
|
291
|
+
return { changesets, current, packages: await unpublished(exec, current) };
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* What `release` would act on at origin's default branch: `nothing to release`, or `pending: `
|
|
295
|
+
* with the changesets, unpublished versions and unpushed tags. Read-only: it fetches objects but
|
|
296
|
+
* writes no ref, worktree or lock, reads no token, and never publishes, pushes or opens a PR, so
|
|
297
|
+
* it cannot collide with a release running meanwhile.
|
|
298
|
+
*/
|
|
299
|
+
export async function releaseCheck(exec, root) {
|
|
300
|
+
const git = gitWith(exec);
|
|
301
|
+
const base = await defaultBranch(git, root);
|
|
302
|
+
// The tip release would fetch, taken before the fetch so the fetched objects include it.
|
|
303
|
+
const sha = (await git(root, "ls-remote", "origin", `refs/heads/${base}`)).split("\t")[0];
|
|
304
|
+
if (!sha)
|
|
305
|
+
throw new Error(`origin has no branch ${base}`);
|
|
306
|
+
// No refmap and no FETCH_HEAD, so no ref lock a concurrent release's fetch could hit.
|
|
307
|
+
await git(root, "fetch", "--quiet", "--no-write-fetch-head", "--refmap=", "origin", `refs/heads/${base}`);
|
|
308
|
+
// A plain directory, not a worktree: a check killed midway leaves only this temp folder.
|
|
309
|
+
const temp = mkdtempSync(join(tmpdir(), CHECK_PREFIX));
|
|
310
|
+
try {
|
|
311
|
+
const archive = join(temp, "tree.tar");
|
|
312
|
+
const dir = join(temp, "tree");
|
|
313
|
+
mkdirSync(dir);
|
|
314
|
+
const paths = (await git(root, "ls-tree", "-r", "-z", "--name-only", sha))
|
|
315
|
+
.split("\0")
|
|
316
|
+
.filter((path) => CHECK_PATH.test(path));
|
|
317
|
+
await git(root, "archive", "--format=tar", "-o", archive, sha, "--", ...paths);
|
|
318
|
+
await exec("tar", ["-xf", archive, "-C", dir]);
|
|
319
|
+
const { changesets, current, packages } = await plan(exec, dir);
|
|
320
|
+
// A real run pushes these whether or not anything else is pending.
|
|
321
|
+
const tags = await unpushedTags(git, root, current);
|
|
322
|
+
const parts = [
|
|
323
|
+
changesets.length && `changesets ${changesets.join(", ")}`,
|
|
324
|
+
packages.length && `unpublished ${packages.map(id).join(", ")}`,
|
|
325
|
+
tags.length && `unpushed tags ${tags.join(", ")}`,
|
|
326
|
+
].filter(Boolean);
|
|
327
|
+
return parts.length ? `pending: ${parts.join("; ")}` : "nothing to release";
|
|
328
|
+
}
|
|
329
|
+
finally {
|
|
330
|
+
rmSync(temp, { recursive: true, force: true });
|
|
331
|
+
}
|
|
332
|
+
}
|
|
275
333
|
/**
|
|
276
334
|
* Versions pending changesets through a merged PR, publishes what npm lacks, pushes the tags and
|
|
277
335
|
* verifies the publish. Resolves with a one-line summary. SIGINT, SIGTERM, SIGHUP or SIGQUIT runs
|
|
@@ -351,12 +409,10 @@ export async function release(exec, context) {
|
|
|
351
409
|
try {
|
|
352
410
|
await live();
|
|
353
411
|
// Unguarded, like the token read below, so the fallback can't swallow an interrupt.
|
|
354
|
-
const
|
|
355
|
-
const base = remoteHead.replace(/^origin\//, "");
|
|
412
|
+
const base = await defaultBranch(git, root);
|
|
356
413
|
await stepGit(root, "worktree", "add", "--detach", dir, `origin/${base}`);
|
|
357
|
-
const pending =
|
|
358
|
-
|
|
359
|
-
let packages = await unpublished(step, current);
|
|
414
|
+
const { changesets: pending, current, packages: unpublishedNow } = await plan(step, dir);
|
|
415
|
+
let packages = unpublishedNow;
|
|
360
416
|
if (!pending.length && !packages.length) {
|
|
361
417
|
// A run that published but failed to push its tags leaves them here for the next one.
|
|
362
418
|
const pushed = await pushTags(stepGit, stepRun, dir, current);
|