@jonmilner/local-ci 0.11.0 → 0.12.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 -3
- package/dist/check-lock.d.ts +5 -4
- package/dist/check-lock.js +67 -25
- package/dist/cli.js +30 -8
- 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
|
@@ -23,7 +23,7 @@ That writes `pre-push` and `commit-msg` shims, each one line that calls local-ci
|
|
|
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
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 <
|
|
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
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:
|
|
@@ -33,6 +33,8 @@ That writes `pre-push` and `commit-msg` shims, each one line that calls local-ci
|
|
|
33
33
|
|
|
34
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
|
+
- `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.
|
|
37
|
+
|
|
36
38
|
- `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
39
|
- skips a repo with no such clone, logging why;
|
|
38
40
|
- 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 +60,7 @@ A repo without a `package.json` sets `LOCAL_CI_COMMAND`, for example on the hook
|
|
|
58
60
|
|
|
59
61
|
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
62
|
|
|
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. A check that finds a background job's lock (see below)
|
|
63
|
+
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. 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
64
|
|
|
63
65
|
### Taking the check lock from your own scripts
|
|
64
66
|
|
|
@@ -72,6 +74,6 @@ await withCheckLock("my-repo browser", async () => {
|
|
|
72
74
|
});
|
|
73
75
|
```
|
|
74
76
|
|
|
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
|
|
77
|
+
`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
78
|
|
|
77
79
|
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,10 @@ 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 and SIGHUP release the lock and end the process.
|
|
28
29
|
*/
|
|
29
30
|
export declare function lockCheck(holder: string, { file, onWait, background }?: LockCheckOptions): Promise<() => void>;
|
|
30
31
|
/** 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,7 +1,7 @@
|
|
|
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. */
|
|
@@ -65,43 +65,85 @@ 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 and SIGHUP release the lock and end the process.
|
|
74
79
|
*/
|
|
75
80
|
export async function lockCheck(holder, { file = checkLockPath(), onWait, background = false } = {}) {
|
|
76
|
-
|
|
81
|
+
const sidecar = besideLockPath(file);
|
|
77
82
|
let mine;
|
|
78
83
|
// A check nested in a check (one that pushes, say) runs under its ancestor's lock rather than
|
|
79
|
-
// waiting forever on it.
|
|
84
|
+
// waiting forever on it, whether the ancestor holds the check lock or runs beside a background job.
|
|
80
85
|
const nested = (held) => (mine ??= ancestors()).has(held.pid);
|
|
86
|
+
const outer = lockHolder(sidecar, "check");
|
|
87
|
+
if (outer && nested(outer))
|
|
88
|
+
return () => { };
|
|
81
89
|
// 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, {
|
|
90
|
+
const besides = (held) => held.background && !background && !nested(held);
|
|
91
|
+
const options = {
|
|
91
92
|
what: "check",
|
|
92
93
|
holder,
|
|
93
|
-
background,
|
|
94
|
-
skip,
|
|
95
94
|
onWait: onWait ??
|
|
96
95
|
(({ 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
|
-
|
|
96
|
+
};
|
|
97
|
+
for (;;) {
|
|
98
|
+
let beside = false;
|
|
99
|
+
const skip = (held) => nested(held) || (beside = besides(held));
|
|
100
|
+
// Taken as a check's lock even for a background job, so checks wait for it rather than run
|
|
101
|
+
// beside it until it starts its run.
|
|
102
|
+
const unlock = await waitForLock(file, { ...options, skip });
|
|
103
|
+
if (unlock) {
|
|
104
|
+
const release = releaseOnSignal(unlock);
|
|
105
|
+
try {
|
|
106
|
+
// A check still running beside a background job that has since ended goes first.
|
|
107
|
+
(await waitForLock(sidecar, { ...options, skip: nested }))?.();
|
|
108
|
+
if (background)
|
|
109
|
+
markLock(file, holder, { background });
|
|
110
|
+
}
|
|
111
|
+
catch (error) {
|
|
112
|
+
release();
|
|
113
|
+
throw error;
|
|
114
|
+
}
|
|
115
|
+
return release;
|
|
116
|
+
}
|
|
117
|
+
if (!beside)
|
|
118
|
+
return () => { };
|
|
119
|
+
// One check at a time beside a background job.
|
|
120
|
+
const unside = await waitForLock(sidecar, { ...options, skip: nested });
|
|
121
|
+
if (!unside)
|
|
122
|
+
return () => { };
|
|
123
|
+
// The background job may have ended, or another taken its place, while this waited.
|
|
124
|
+
let now;
|
|
125
|
+
try {
|
|
126
|
+
const taken = takeLock(file, "check", holder);
|
|
127
|
+
// Holding the beside lock, nothing else runs beside: the check lock alone is enough.
|
|
128
|
+
unside();
|
|
129
|
+
return releaseOnSignal(taken);
|
|
130
|
+
}
|
|
131
|
+
catch (error) {
|
|
132
|
+
if (!(error instanceof LockHeld)) {
|
|
133
|
+
unside();
|
|
134
|
+
throw error;
|
|
135
|
+
}
|
|
136
|
+
now = error;
|
|
137
|
+
}
|
|
138
|
+
if (besides(now)) {
|
|
139
|
+
process.stderr.write(`local-ci: running beside background job pid ${now.pid} (${now.holder || "unknown"}), without the check lock\n`);
|
|
140
|
+
return releaseOnSignal(unside);
|
|
141
|
+
}
|
|
142
|
+
unside();
|
|
143
|
+
if (nested(now))
|
|
144
|
+
return () => { };
|
|
145
|
+
// A check holds the lock now: wait for it as usual.
|
|
146
|
+
}
|
|
105
147
|
}
|
|
106
148
|
/** Runs `fn` under the machine-wide check lock, so it queues behind `local-ci` checks and vice versa. */
|
|
107
149
|
export async function withCheckLock(holder, fn, options) {
|
package/dist/cli.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { spawn } from "node:child_process";
|
|
2
|
+
import { spawn, spawnSync } 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";
|
|
@@ -14,24 +14,39 @@ 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:
|
|
21
21
|
local-ci pre-push run the check for each pushed commit (git pre-push hook)
|
|
22
|
-
local-ci post <
|
|
22
|
+
local-ci post <ref> wait for <ref> on GitHub, then post its check and pr-title statuses
|
|
23
23
|
local-ci pr-title [<pr>] lint the PR title and post the pr-title status
|
|
24
24
|
local-ci commit-msg <file|->
|
|
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
28
|
local-ci release version pending changesets through a PR, publish to npm and push tags
|
|
29
|
+
local-ci release --check say whether release has anything to do, changing nothing
|
|
29
30
|
local-ci renovate check each open Renovate PR in your repos and post check and pr-title
|
|
30
31
|
local-ci renovate --install
|
|
31
32
|
run renovate every 15 minutes with a launchd agent (macOS)
|
|
32
33
|
local-ci renovate --uninstall
|
|
33
34
|
remove that agent`;
|
|
34
35
|
const git = async (...args) => (await exec("git", args)).trim();
|
|
36
|
+
/** The full sha `ref` names, so a short sha or `HEAD` matches the full-sha pass records. */
|
|
37
|
+
function resolveCommit(ref) {
|
|
38
|
+
const result = spawnSync("git", ["rev-parse", "--verify", "--quiet", `${ref}^{commit}`], {
|
|
39
|
+
encoding: "utf8",
|
|
40
|
+
});
|
|
41
|
+
if (result.status === 0)
|
|
42
|
+
return result.stdout.trim();
|
|
43
|
+
// `--quiet` keeps git silent only for a ref that names no commit; anything else (an ambiguous
|
|
44
|
+
// short sha, no repo, no git) keeps git's own message.
|
|
45
|
+
const stderr = result.stderr?.trim();
|
|
46
|
+
if (result.status === 1 && !stderr)
|
|
47
|
+
throw new Error(`${ref} is not a commit in this repo`);
|
|
48
|
+
throw new Error(`git rev-parse: ${stderr || result.error?.message || `exit ${result.status}`}`);
|
|
49
|
+
}
|
|
35
50
|
async function repo() {
|
|
36
51
|
const root = await git("rev-parse", "--show-toplevel");
|
|
37
52
|
// The common dir names the repo even from a linked worktree.
|
|
@@ -222,7 +237,7 @@ async function postFailure(sha, command) {
|
|
|
222
237
|
}
|
|
223
238
|
}
|
|
224
239
|
async function status(ref = "HEAD") {
|
|
225
|
-
const sha =
|
|
240
|
+
const sha = resolveCommit(ref);
|
|
226
241
|
const current = await repo();
|
|
227
242
|
try {
|
|
228
243
|
await runCheck(current, sha);
|
|
@@ -235,13 +250,20 @@ async function status(ref = "HEAD") {
|
|
|
235
250
|
}
|
|
236
251
|
await post(current, sha);
|
|
237
252
|
}
|
|
238
|
-
async function runRelease() {
|
|
253
|
+
async function runRelease(args) {
|
|
254
|
+
const check = args[0] === "--check";
|
|
255
|
+
const extra = check ? args.slice(1) : args;
|
|
256
|
+
// Any argument, even an empty one, other than a lone --check is refused, so a typo never publishes.
|
|
257
|
+
if (extra.length)
|
|
258
|
+
throw new Error(`release: unexpected ${JSON.stringify(extra.join(" "))}\n${USAGE}`);
|
|
239
259
|
// First, before any child process runs.
|
|
240
260
|
const envToken = takeEnvToken();
|
|
241
261
|
const { root, lock } = await repo();
|
|
242
262
|
// Off by default, so a repo that publishes some other way can't release by accident.
|
|
243
263
|
if (!releaseEnabled(root))
|
|
244
264
|
return out("release not enabled");
|
|
265
|
+
if (check)
|
|
266
|
+
return out(await releaseCheck(exec, root));
|
|
245
267
|
await assertGhAuth(exec);
|
|
246
268
|
out(await release(exec, {
|
|
247
269
|
root,
|
|
@@ -287,7 +309,7 @@ async function main([command, ...args]) {
|
|
|
287
309
|
if (command === "hooks")
|
|
288
310
|
return runHooks(args);
|
|
289
311
|
if (command === "release")
|
|
290
|
-
return runRelease();
|
|
312
|
+
return runRelease(args);
|
|
291
313
|
if (command === "renovate")
|
|
292
314
|
return runRenovate(args);
|
|
293
315
|
if (command === "post" || command === "pr-title" || command === "status") {
|
|
@@ -296,8 +318,8 @@ async function main([command, ...args]) {
|
|
|
296
318
|
switch (command) {
|
|
297
319
|
case "post":
|
|
298
320
|
if (!args[0])
|
|
299
|
-
throw new Error("post needs a commit SHA");
|
|
300
|
-
return post(await repo(), args[0]);
|
|
321
|
+
throw new Error("post needs a commit SHA or ref");
|
|
322
|
+
return post(await repo(), resolveCommit(args[0]));
|
|
301
323
|
case "pr-title":
|
|
302
324
|
if (!(await prTitle(args[0])))
|
|
303
325
|
process.exitCode = 1;
|
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);
|