@jonmilner/local-ci 0.3.2 → 0.3.4
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 +2 -2
- package/dist/cli.js +12 -3
- package/dist/config.d.ts +9 -0
- package/dist/config.js +20 -0
- package/dist/release.js +21 -7
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -28,7 +28,7 @@ exec pnpm exec local-ci pre-push "$@"
|
|
|
28
28
|
2. At the merge commit (or the default branch, with no changesets pending), it runs `local-ci status`, then `pnpm release` with the npm token, then pushes the tags `changeset publish` created.
|
|
29
29
|
3. It checks each published `name@version` with `npm view`, waiting up to 5 minutes in all for the registry to list them. A timeout or `npm view` error here exits non-zero, but the publish already ran: check with the `npm view` command the error gives before rerunning.
|
|
30
30
|
|
|
31
|
-
A failure after the push closes the PR and deletes `release/<short-sha>`; Ctrl-C (SIGINT), SIGTERM, SIGHUP (closing the terminal) or Ctrl-\\ (SIGQUIT) stops the running step (killing `pnpm release` and the publish it started), does the same cleanup, then exits 128 + the signal number, e.g. 130 for Ctrl-C (a second signal exits at once, killing the publish outright). A run killed outright leaves its temp worktree and local branch, which the next run removes; its PR and remote branch stay open for you to close. Step 2 runs even with no changesets pending, so a rerun after a failed publish finishes the job. With nothing pending and every version already on npm it prints `nothing to release`. The repo needs changesets and a root `release` script (for example `pnpm build && changeset publish`). The npm token comes from the macOS Keychain (service `local-ci-npm`) and is passed only in `pnpm release`'s environment, never written to `.npmrc` or logged; see [npm publish token](../../docs/new-machine.md#npm-publish-token).
|
|
31
|
+
A failure after the push closes the PR and deletes `release/<short-sha>`; Ctrl-C (SIGINT), SIGTERM, SIGHUP (closing the terminal) or Ctrl-\\ (SIGQUIT) stops the running step (killing `pnpm release` and the publish it started), does the same cleanup, then exits 128 + the signal number, e.g. 130 for Ctrl-C (a second signal exits at once, killing the publish outright). A run killed outright leaves its temp worktree and local branch, which the next run removes; its PR and remote branch stay open for you to close. Step 2 runs even with no changesets pending, so a rerun after a failed publish finishes the job. If npm refuses the publish because it already has the version (a rerun while the registry still lags), it says so with the `npm view` command to check, instead of blaming the token; rerun once npm lists it. With nothing pending and every version already on npm it prints `nothing to release`. The repo needs changesets and a root `release` script (for example `pnpm build && changeset publish`). The npm token comes from the macOS Keychain (service `local-ci-npm`) and is passed only in `pnpm release`'s environment, never written to `.npmrc` or logged; see [npm publish token](../../docs/new-machine.md#npm-publish-token).
|
|
32
32
|
|
|
33
33
|
## Check command
|
|
34
34
|
|
|
@@ -42,6 +42,6 @@ A repo without a `package.json` sets `LOCAL_CI_COMMAND`, for example in the hook
|
|
|
42
42
|
|
|
43
43
|
## State
|
|
44
44
|
|
|
45
|
-
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.
|
|
45
|
+
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. 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 local-ci along with the check, so that run logs nothing.
|
|
46
46
|
|
|
47
47
|
Part of [jonmilner/config](https://github.com/jonmilner/config#readme).
|
package/dist/cli.js
CHANGED
|
@@ -4,7 +4,7 @@ import { closeSync, openSync, readFileSync } from "node:fs";
|
|
|
4
4
|
import { hostname } from "node:os";
|
|
5
5
|
import { basename, dirname, resolve } from "node:path";
|
|
6
6
|
import { fileURLToPath } from "node:url";
|
|
7
|
-
import { passedCommand, recordPass, releaseEnabled, resolveCommand, statePaths } from "./config.js";
|
|
7
|
+
import { appendLog, checkRunMessage, logLine, passedCommand, recordPass, releaseEnabled, resolveCommand, statePaths, } from "./config.js";
|
|
8
8
|
import { exec } from "./exec.js";
|
|
9
9
|
import { assertGhAuth, checkStatus, postStatus, prTitleStatus, viewPr, waitForCommit, waitForOpenPr, } from "./github.js";
|
|
10
10
|
import { cannotCheckInPlace, githubRepo, parsePrePush, shasToCheck } from "./push.js";
|
|
@@ -26,7 +26,7 @@ async function repo() {
|
|
|
26
26
|
}
|
|
27
27
|
const out = (s) => process.stdout.write(`${s}\n`);
|
|
28
28
|
const err = (s) => process.stderr.write(`${s}\n`);
|
|
29
|
-
const log = (message) => out(
|
|
29
|
+
const log = (message) => out(logLine(message));
|
|
30
30
|
/** The check command itself exited non-zero, as opposed to the check being unable to run. */
|
|
31
31
|
class CheckFailed extends Error {
|
|
32
32
|
command;
|
|
@@ -36,7 +36,7 @@ class CheckFailed extends Error {
|
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
38
|
/** Runs the check at `sha` in place, after the clean-tree rule, unless it already passed. */
|
|
39
|
-
async function runCheck({ root, passed }, sha) {
|
|
39
|
+
async function runCheck({ root, passed, log: logFile }, sha) {
|
|
40
40
|
if (passedCommand(passed, sha))
|
|
41
41
|
return;
|
|
42
42
|
const reason = cannotCheckInPlace(sha, await git("rev-parse", "HEAD"), await git("status", "--porcelain"));
|
|
@@ -44,10 +44,19 @@ async function runCheck({ root, passed }, sha) {
|
|
|
44
44
|
throw new Error(`cannot check ${sha.slice(0, 7)}: ${reason}`);
|
|
45
45
|
const command = resolveCommand(root);
|
|
46
46
|
err(`local-ci: running ${command} on ${sha.slice(0, 7)}`);
|
|
47
|
+
const started = performance.now();
|
|
47
48
|
const result = spawnSync("sh", ["-c", command], {
|
|
48
49
|
cwd: root,
|
|
49
50
|
stdio: ["ignore", "inherit", "inherit"],
|
|
50
51
|
});
|
|
52
|
+
// Every run's duration goes in the log, so a faster check can be measured against a slower one.
|
|
53
|
+
// A log that can't be written must not change the check's outcome.
|
|
54
|
+
try {
|
|
55
|
+
appendLog(logFile, checkRunMessage(sha, command, result.status, performance.now() - started));
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
err(`local-ci: could not log the run: ${error instanceof Error ? error.message : String(error)}`);
|
|
59
|
+
}
|
|
51
60
|
// A spawn error or a signal means the check never finished, so it is not a failure to post.
|
|
52
61
|
if (result.status === null) {
|
|
53
62
|
const cause = result.error?.message ?? result.signal;
|
package/dist/config.d.ts
CHANGED
|
@@ -10,6 +10,15 @@ export declare function statePaths(repo: string, env?: NodeJS.ProcessEnv): {
|
|
|
10
10
|
passed: string;
|
|
11
11
|
lock: string;
|
|
12
12
|
};
|
|
13
|
+
/** A line of the repo's log: the message after an ISO timestamp. */
|
|
14
|
+
export declare function logLine(message: string, now?: Date): string;
|
|
15
|
+
/** Appends `message` to the repo's log as one timestamped line. */
|
|
16
|
+
export declare function appendLog(file: string, message: string, now?: Date): void;
|
|
17
|
+
/**
|
|
18
|
+
* How a check run ended and how long it took, e.g. `abc1234: pnpm check passed in 71.2s`.
|
|
19
|
+
* A null `status` (a spawn error or a signal) means the check never finished.
|
|
20
|
+
*/
|
|
21
|
+
export declare function checkRunMessage(sha: string, command: string, status: number | null, ms: number): string;
|
|
13
22
|
/** Records that `command` passed at `sha`, one `<sha>\t<command>` line per commit. */
|
|
14
23
|
export declare function recordPass(file: string, sha: string, command: string): void;
|
|
15
24
|
/** The command that last passed at `sha`, or undefined if it was never checked. */
|
package/dist/config.js
CHANGED
|
@@ -34,6 +34,26 @@ export function statePaths(repo, env = process.env) {
|
|
|
34
34
|
lock: join(dir, `${repo}.release.lock`),
|
|
35
35
|
};
|
|
36
36
|
}
|
|
37
|
+
/** A line of the repo's log: the message after an ISO timestamp. */
|
|
38
|
+
export function logLine(message, now = new Date()) {
|
|
39
|
+
return `[${now.toISOString()}] ${message}`;
|
|
40
|
+
}
|
|
41
|
+
/** Appends `message` to the repo's log as one timestamped line. */
|
|
42
|
+
export function appendLog(file, message, now = new Date()) {
|
|
43
|
+
appendFileSync(file, `${logLine(message, now)}\n`);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* How a check run ended and how long it took, e.g. `abc1234: pnpm check passed in 71.2s`.
|
|
47
|
+
* A null `status` (a spawn error or a signal) means the check never finished.
|
|
48
|
+
*/
|
|
49
|
+
export function checkRunMessage(sha, command, status, ms) {
|
|
50
|
+
const seconds = `${(ms / 1000).toFixed(1)}s`;
|
|
51
|
+
const outcome = status === null
|
|
52
|
+
? `did not finish after ${seconds}`
|
|
53
|
+
: `${status === 0 ? "passed" : "failed"} in ${seconds}`;
|
|
54
|
+
// One line per run, even for a command written over several lines.
|
|
55
|
+
return `${sha.slice(0, 7)}: ${command.replace(/\s+/g, " ")} ${outcome}`;
|
|
56
|
+
}
|
|
37
57
|
/** Records that `command` passed at `sha`, one `<sha>\t<command>` line per commit. */
|
|
38
58
|
export function recordPass(file, sha, command) {
|
|
39
59
|
appendFileSync(file, `${sha}\t${command}\n`);
|
package/dist/release.js
CHANGED
|
@@ -16,6 +16,8 @@ const TEMP_PREFIX = "local-ci-release-";
|
|
|
16
16
|
const SIGNALS = ["SIGINT", "SIGTERM", "SIGHUP", "SIGQUIT"];
|
|
17
17
|
// What npm and pnpm print when the registry rejects the token, or wants an OTP it can't bypass.
|
|
18
18
|
const AUTH_ERROR = /\b(?:E401|E403|EOTP|ERR_PNPM_OTP_NON_INTERACTIVE|401 Unauthorized|403 Forbidden)\b/;
|
|
19
|
+
// What npm prints when the version is already on the registry. It comes with an E403, so it is checked before AUTH_ERROR.
|
|
20
|
+
const PUBLISH_OVER = /cannot publish over the previously published version/;
|
|
19
21
|
const id = ({ name, version }) => `${name}@${version}`;
|
|
20
22
|
function alive(pid) {
|
|
21
23
|
if (!Number.isInteger(pid) || pid <= 0)
|
|
@@ -113,8 +115,15 @@ function run(file, args, cwd) {
|
|
|
113
115
|
throw new Error(`${file} ${args.join(" ")} failed (${cause})`);
|
|
114
116
|
}
|
|
115
117
|
}
|
|
116
|
-
/**
|
|
117
|
-
|
|
118
|
+
/** The hint for checking each package on npm by hand. */
|
|
119
|
+
const npmView = (packages) => packages.length
|
|
120
|
+
? `check with: ${packages.map((pkg) => `npm view ${id(pkg)} version`).join("; ")}`
|
|
121
|
+
: "check with: npm view <name>@<version> version";
|
|
122
|
+
/**
|
|
123
|
+
* Runs `pnpm release` with the token in its env only, teeing its output to spot a rejected token
|
|
124
|
+
* or a version npm already has. `packages` are the ones it should publish, for the hints.
|
|
125
|
+
*/
|
|
126
|
+
function publish(dir, token, packages, signal) {
|
|
118
127
|
return new Promise((resolve, reject) => {
|
|
119
128
|
const child = spawn("pnpm", ["release"], {
|
|
120
129
|
cwd: dir,
|
|
@@ -147,15 +156,18 @@ function publish(dir, token, signal) {
|
|
|
147
156
|
process.off("exit", kill);
|
|
148
157
|
};
|
|
149
158
|
let authError = false;
|
|
159
|
+
let publishOver = false;
|
|
150
160
|
const tee = (stream, sink) => {
|
|
151
161
|
// Keep a tail so a match split across chunks is still seen.
|
|
152
162
|
let tail = "";
|
|
153
163
|
stream.on("data", (chunk) => {
|
|
154
164
|
sink.write(chunk);
|
|
155
165
|
const text = tail + chunk.toString();
|
|
166
|
+
if (PUBLISH_OVER.test(text))
|
|
167
|
+
publishOver = true;
|
|
156
168
|
if (AUTH_ERROR.test(text))
|
|
157
169
|
authError = true;
|
|
158
|
-
tail = text.slice(-
|
|
170
|
+
tail = text.slice(-64);
|
|
159
171
|
});
|
|
160
172
|
};
|
|
161
173
|
tee(child.stdout, process.stdout);
|
|
@@ -171,9 +183,11 @@ function publish(dir, token, signal) {
|
|
|
171
183
|
kill();
|
|
172
184
|
if (code === 0)
|
|
173
185
|
return resolve();
|
|
174
|
-
reject(new Error(
|
|
175
|
-
? `npm
|
|
176
|
-
:
|
|
186
|
+
reject(new Error(publishOver
|
|
187
|
+
? `npm already has a version this run tried to publish, so it refused to publish over it; the registry may still be catching up on an earlier publish. Rerun once npm lists it; ${npmView(packages)}`
|
|
188
|
+
: authError
|
|
189
|
+
? `npm rejected the token in the Keychain (service ${TOKEN_SERVICE}); it may have expired or lack "bypass 2FA". Rotate it: ${SETUP_DOC}`
|
|
190
|
+
: `pnpm release failed (${code === null ? killed : `exit ${code}`})`));
|
|
177
191
|
});
|
|
178
192
|
});
|
|
179
193
|
}
|
|
@@ -394,7 +408,7 @@ export async function release(exec, context) {
|
|
|
394
408
|
await stepRun(process.execPath, [context.cli, "status"], dir);
|
|
395
409
|
await live();
|
|
396
410
|
log(`publishing ${packages.map(id).join(", ") || "nothing new"}`);
|
|
397
|
-
await track(publish(dir, token, abort.signal));
|
|
411
|
+
await track(publish(dir, token, packages, abort.signal));
|
|
398
412
|
// Every current version, so a tag an earlier half-failed run left unpushed goes too.
|
|
399
413
|
await pushTags(stepGit, stepRun, dir, await publicPackages(step, dir));
|
|
400
414
|
if (packages.length)
|