@jonmilner/local-ci 0.3.0 → 0.3.1
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/release.d.ts +4 -1
- package/dist/release.js +143 -48
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -24,11 +24,11 @@ exec pnpm exec local-ci pre-push "$@"
|
|
|
24
24
|
- `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.
|
|
25
25
|
- `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.
|
|
26
26
|
- `local-ci release`: publishes the repo's packages from this machine. Off unless `package.json` has `"local-ci": { "release": true }`; otherwise it prints `release not enabled` and exits 0. It needs a clean working tree, then fetches `origin` and takes a lock, so a second run exits with `release in progress`. In a temporary worktree at `origin`'s default branch:
|
|
27
|
-
1. With pending changesets, it creates `release/<short-sha>`, runs `pnpm changeset version
|
|
27
|
+
1. With pending changesets, it creates `release/<short-sha>`, runs the root `version-packages` script if `package.json` has one (for steps such as a size-history entry that belong in the version PR) or `pnpm changeset version` otherwise, commits `chore: version packages`, pushes (the hook runs the check and posts `check`), opens the PR, posts `pr-title`, waits for both statuses, and squash-merges it.
|
|
28
28
|
2. At the merge commit (or the default branch, with no changesets pending), it runs `local-ci status`, then `pnpm release` with the npm token, then pushes the tags `changeset publish` created.
|
|
29
29
|
3. It checks each published `name@version` with `npm view`.
|
|
30
30
|
|
|
31
|
-
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) or SIGTERM stops the running step (killing `pnpm release`), does the same cleanup, then exits 130 or 143 (a second signal exits at once). A run killed outright leaves its temp worktree and local branch, which the next run removes; its PR and remote branch stay open for you to close. Step 2 runs even with no changesets pending, so a rerun after a failed publish finishes the job. With nothing pending and every version already on npm it prints `nothing to release`. The repo needs changesets and a root `release` script (for example `pnpm build && changeset publish`). The npm token comes from the macOS Keychain (service `local-ci-npm`) and is passed only in `pnpm release`'s environment, never written to `.npmrc` or logged; see [npm publish token](../../docs/new-machine.md#npm-publish-token).
|
|
32
32
|
|
|
33
33
|
## Check command
|
|
34
34
|
|
package/dist/release.d.ts
CHANGED
|
@@ -11,6 +11,8 @@ export interface Package {
|
|
|
11
11
|
export declare function takeLock(file: string): () => void;
|
|
12
12
|
/** The changeset files waiting in `dir`'s `.changeset` folder. */
|
|
13
13
|
export declare function pendingChangesets(dir: string): string[];
|
|
14
|
+
/** How `dir` versions its changesets: its root `version-packages` script, else `changeset version`. */
|
|
15
|
+
export declare function versionCommand(dir: string): string[];
|
|
14
16
|
/** The workspace's public packages at `dir`, with the versions in their package.json. */
|
|
15
17
|
export declare function publicPackages(exec: Exec, dir: string): Promise<Package[]>;
|
|
16
18
|
/** Whether npm has this exact version. A 404 means no; any other error is thrown. */
|
|
@@ -29,6 +31,7 @@ export interface ReleaseContext {
|
|
|
29
31
|
}
|
|
30
32
|
/**
|
|
31
33
|
* Versions pending changesets through a merged PR, publishes what npm lacks, pushes the tags and
|
|
32
|
-
* verifies the publish. Resolves with a one-line summary.
|
|
34
|
+
* verifies the publish. Resolves with a one-line summary. SIGINT or SIGTERM runs the same cleanup
|
|
35
|
+
* as a failure, then exits 128 + the signal number.
|
|
33
36
|
*/
|
|
34
37
|
export declare function release(exec: Exec, context: ReleaseContext): Promise<string>;
|
package/dist/release.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { spawn, spawnSync } from "node:child_process";
|
|
2
2
|
import { existsSync, linkSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync, } from "node:fs";
|
|
3
|
-
import { tmpdir } from "node:os";
|
|
4
|
-
import { join } from "node:path";
|
|
3
|
+
import { constants, tmpdir } from "node:os";
|
|
4
|
+
import { basename, dirname, join } from "node:path";
|
|
5
5
|
import { defaultSleep, waitForStatuses } from "./github.js";
|
|
6
6
|
/** The macOS Keychain generic password that holds the npm token. */
|
|
7
7
|
export const TOKEN_SERVICE = "local-ci-npm";
|
|
@@ -9,6 +9,8 @@ export const TOKEN_SERVICE = "local-ci-npm";
|
|
|
9
9
|
export const TOKEN_ENV = "NPM_CONFIG_//registry.npmjs.org/:_authToken";
|
|
10
10
|
const SETUP_DOC = "https://github.com/jonmilner/config/blob/main/docs/new-machine.md#npm-publish-token";
|
|
11
11
|
const VERSION_TITLE = "chore: version packages";
|
|
12
|
+
const TEMP_PREFIX = "local-ci-release-";
|
|
13
|
+
const SIGNALS = ["SIGINT", "SIGTERM"];
|
|
12
14
|
// What npm and pnpm print when the registry rejects the token, or wants an OTP it can't bypass.
|
|
13
15
|
const AUTH_ERROR = /\b(?:E401|E403|EOTP|401 Unauthorized|403 Forbidden)\b/;
|
|
14
16
|
const id = ({ name, version }) => `${name}@${version}`;
|
|
@@ -58,6 +60,14 @@ export function pendingChangesets(dir) {
|
|
|
58
60
|
return [];
|
|
59
61
|
return readdirSync(folder).filter((file) => file.endsWith(".md") && file !== "README.md");
|
|
60
62
|
}
|
|
63
|
+
/** How `dir` versions its changesets: its root `version-packages` script, else `changeset version`. */
|
|
64
|
+
export function versionCommand(dir) {
|
|
65
|
+
const { scripts } = JSON.parse(readFileSync(join(dir, "package.json"), "utf8"));
|
|
66
|
+
const script = scripts?.["version-packages"];
|
|
67
|
+
return typeof script === "string" && script.trim()
|
|
68
|
+
? ["run", "version-packages"]
|
|
69
|
+
: ["exec", "changeset", "version"];
|
|
70
|
+
}
|
|
61
71
|
/** The workspace's public packages at `dir`, with the versions in their package.json. */
|
|
62
72
|
export async function publicPackages(exec, dir) {
|
|
63
73
|
const projects = JSON.parse(await exec("pnpm", ["-C", dir, "ls", "-r", "--depth", "-1", "--json"]));
|
|
@@ -101,10 +111,12 @@ function run(file, args, cwd) {
|
|
|
101
111
|
}
|
|
102
112
|
}
|
|
103
113
|
/** Runs `pnpm release` with the token in its env only, teeing its output to spot a rejected token. */
|
|
104
|
-
function publish(dir, token) {
|
|
114
|
+
function publish(dir, token, signal) {
|
|
105
115
|
return new Promise((resolve, reject) => {
|
|
106
116
|
const child = spawn("pnpm", ["release"], {
|
|
107
117
|
cwd: dir,
|
|
118
|
+
// An interrupt kills it, and the close below settles only once it has exited.
|
|
119
|
+
signal,
|
|
108
120
|
// CI makes changesets fail on an OTP request instead of prompting on an ignored stdin.
|
|
109
121
|
env: { ...process.env, CI: "true", [TOKEN_ENV]: token },
|
|
110
122
|
stdio: ["ignore", "pipe", "pipe"],
|
|
@@ -123,13 +135,16 @@ function publish(dir, token) {
|
|
|
123
135
|
};
|
|
124
136
|
tee(child.stdout, process.stdout);
|
|
125
137
|
tee(child.stderr, process.stderr);
|
|
126
|
-
child.on("error",
|
|
127
|
-
|
|
138
|
+
child.on("error", (error) => {
|
|
139
|
+
if (!signal.aborted)
|
|
140
|
+
reject(error);
|
|
141
|
+
});
|
|
142
|
+
child.on("close", (code, killed) => {
|
|
128
143
|
if (code === 0)
|
|
129
144
|
return resolve();
|
|
130
145
|
reject(new Error(authError
|
|
131
146
|
? `npm rejected the token in the Keychain (service ${TOKEN_SERVICE}); it may have expired or lack "bypass 2FA". Rotate it: ${SETUP_DOC}`
|
|
132
|
-
: `pnpm release failed (${code === null ?
|
|
147
|
+
: `pnpm release failed (${code === null ? killed : `exit ${code}`})`));
|
|
133
148
|
});
|
|
134
149
|
});
|
|
135
150
|
}
|
|
@@ -144,66 +159,152 @@ async function verify(exec, packages, options = {}) {
|
|
|
144
159
|
}
|
|
145
160
|
}
|
|
146
161
|
}
|
|
162
|
+
const gitWith = (exec) => async (cwd, ...args) => (await exec("git", ["-C", cwd, ...args])).trim();
|
|
147
163
|
/** Pushes the local `name@version` tags of `packages` that origin lacks, resolving with their names. */
|
|
148
|
-
async function pushTags(git, dir, packages) {
|
|
164
|
+
async function pushTags(git, run, dir, packages) {
|
|
149
165
|
const local = new Set((await git(dir, "tag", "--list")).split("\n"));
|
|
150
166
|
const remote = new Set((await git(dir, "ls-remote", "--tags", "origin"))
|
|
151
167
|
.split("\n")
|
|
152
168
|
.map((line) => line.split("\t")[1]?.replace(/^refs\/tags\//, "")));
|
|
153
169
|
const missing = packages.map(id).filter((tag) => local.has(tag) && !remote.has(tag));
|
|
154
170
|
if (missing.length)
|
|
155
|
-
run("git", ["push", "origin", ...missing.map((tag) => `refs/tags/${tag}`)], dir);
|
|
171
|
+
await run("git", ["push", "origin", ...missing.map((tag) => `refs/tags/${tag}`)], dir);
|
|
156
172
|
return missing;
|
|
157
173
|
}
|
|
174
|
+
/**
|
|
175
|
+
* Removes the temp worktrees and `release/*` branches that runs killed before their cleanup left,
|
|
176
|
+
* then `branch` if it remains. `own` is this run's worktree, which stays. The lock is held, so none of them belongs to a live run.
|
|
177
|
+
*/
|
|
178
|
+
async function pruneStale(git, root, own, branch) {
|
|
179
|
+
// Forgets worktrees whose directory is gone, so their branch can be deleted.
|
|
180
|
+
await git(root, "worktree", "prune");
|
|
181
|
+
const branches = new Set([branch]);
|
|
182
|
+
for (const entry of (await git(root, "worktree", "list", "--porcelain")).split("\n\n")) {
|
|
183
|
+
const path = /^worktree (.+)$/m.exec(entry)?.[1];
|
|
184
|
+
// Only a temp directory as `release` creates it, other than this run's. Its unique name tells
|
|
185
|
+
// them apart, since git may report a path with its symlinks resolved.
|
|
186
|
+
const temp = path && basename(path) === "worktree" ? basename(dirname(path)) : "";
|
|
187
|
+
if (!path || !temp.startsWith(TEMP_PREFIX) || temp === basename(dirname(own)))
|
|
188
|
+
continue;
|
|
189
|
+
// Twice, so a locked worktree goes too.
|
|
190
|
+
await git(root, "worktree", "remove", "--force", "--force", path);
|
|
191
|
+
rmSync(dirname(path), { recursive: true, force: true });
|
|
192
|
+
const held = /^branch refs\/heads\/(release\/.+)$/m.exec(entry)?.[1];
|
|
193
|
+
if (held)
|
|
194
|
+
branches.add(held);
|
|
195
|
+
}
|
|
196
|
+
for (const name of branches) {
|
|
197
|
+
if (await git(root, "branch", "--list", name))
|
|
198
|
+
await git(root, "branch", "-D", name);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
158
201
|
/**
|
|
159
202
|
* Versions pending changesets through a merged PR, publishes what npm lacks, pushes the tags and
|
|
160
|
-
* verifies the publish. Resolves with a one-line summary.
|
|
203
|
+
* verifies the publish. Resolves with a one-line summary. SIGINT or SIGTERM runs the same cleanup
|
|
204
|
+
* as a failure, then exits 128 + the signal number.
|
|
161
205
|
*/
|
|
162
206
|
export async function release(exec, context) {
|
|
163
207
|
const { root, log } = context;
|
|
164
|
-
const git =
|
|
208
|
+
const git = gitWith(exec);
|
|
165
209
|
if (await git(root, "status", "--porcelain")) {
|
|
166
210
|
throw new Error("the working tree has uncommitted or untracked files; commit or stash them first");
|
|
167
211
|
}
|
|
168
212
|
await git(root, "fetch", "origin");
|
|
169
213
|
const unlock = takeLock(context.lock);
|
|
170
|
-
const temp = mkdtempSync(join(tmpdir(),
|
|
214
|
+
const temp = mkdtempSync(join(tmpdir(), TEMP_PREFIX));
|
|
171
215
|
const dir = join(temp, "worktree");
|
|
172
216
|
let branch;
|
|
173
217
|
// Set from the push until the merge, so a failure in between closes the PR and deletes the branch.
|
|
174
218
|
let remoteBranch;
|
|
175
219
|
let repo;
|
|
176
220
|
let pr;
|
|
221
|
+
let interrupted;
|
|
222
|
+
const abort = new AbortController();
|
|
223
|
+
// Once a signal arrives no step starts, so nothing races the cleanup or outlives it.
|
|
224
|
+
const live = async () => {
|
|
225
|
+
// A signal that arrived during a synchronous step is delivered only once the event loop turns.
|
|
226
|
+
await new Promise((resolve) => setImmediate(resolve));
|
|
227
|
+
if (interrupted)
|
|
228
|
+
throw new Error(`interrupted by ${interrupted}`);
|
|
229
|
+
};
|
|
230
|
+
// The step running now, which the cleanup waits for so it never pulls the worktree from under it.
|
|
231
|
+
let inFlight = Promise.resolve();
|
|
232
|
+
const track = (promise) => {
|
|
233
|
+
inFlight = promise.catch(() => { });
|
|
234
|
+
return promise;
|
|
235
|
+
};
|
|
236
|
+
const step = async (file, args) => {
|
|
237
|
+
await live();
|
|
238
|
+
return track(exec(file, args));
|
|
239
|
+
};
|
|
240
|
+
const stepGit = gitWith(step);
|
|
241
|
+
const stepRun = async (file, args, cwd) => {
|
|
242
|
+
await live();
|
|
243
|
+
run(file, args, cwd);
|
|
244
|
+
};
|
|
245
|
+
// Set once the run is cleaning up on its own, so a signal then doesn't turn a success into an exit.
|
|
246
|
+
let finishing = false;
|
|
247
|
+
let cleaning;
|
|
248
|
+
const cleanup = () => (cleaning ??= (async () => {
|
|
249
|
+
if (remoteBranch) {
|
|
250
|
+
log(`cleaning up ${pr ? `#${pr} and ` : ""}${remoteBranch} so the next run starts fresh`);
|
|
251
|
+
await (pr && repo
|
|
252
|
+
? exec("gh", ["pr", "close", pr, "--repo", repo, "--delete-branch"])
|
|
253
|
+
: git(root, "push", "origin", "--delete", remoteBranch)).catch((error) => log(`cleanup failed: ${error.message}`));
|
|
254
|
+
}
|
|
255
|
+
await git(root, "worktree", "remove", "--force", dir).catch(() => { });
|
|
256
|
+
if (branch)
|
|
257
|
+
await git(root, "branch", "-D", branch).catch(() => { });
|
|
258
|
+
rmSync(temp, { recursive: true, force: true });
|
|
259
|
+
unlock();
|
|
260
|
+
})());
|
|
261
|
+
const onSignal = (signal) => {
|
|
262
|
+
// `once` hands a second signal back to Node's default, which exits at once.
|
|
263
|
+
if (finishing)
|
|
264
|
+
return;
|
|
265
|
+
interrupted = signal;
|
|
266
|
+
abort.abort();
|
|
267
|
+
log(`${signal}: cleaning up before exiting; send it again to exit now`);
|
|
268
|
+
void inFlight
|
|
269
|
+
.then(() => cleanup())
|
|
270
|
+
.finally(() => process.exit(128 + constants.signals[signal]));
|
|
271
|
+
};
|
|
272
|
+
for (const signal of SIGNALS)
|
|
273
|
+
process.once(signal, onSignal);
|
|
177
274
|
try {
|
|
275
|
+
await live();
|
|
276
|
+
// Unguarded, like the token read below, so the fallback can't swallow an interrupt.
|
|
178
277
|
const remoteHead = await git(root, "symbolic-ref", "--short", "refs/remotes/origin/HEAD").catch(() => "origin/main");
|
|
179
278
|
const base = remoteHead.replace(/^origin\//, "");
|
|
180
|
-
await
|
|
279
|
+
await stepGit(root, "worktree", "add", "--detach", dir, `origin/${base}`);
|
|
181
280
|
const pending = pendingChangesets(dir);
|
|
182
|
-
const current = await publicPackages(
|
|
183
|
-
let packages = await unpublished(
|
|
281
|
+
const current = await publicPackages(step, dir);
|
|
282
|
+
let packages = await unpublished(step, current);
|
|
184
283
|
if (!pending.length && !packages.length) {
|
|
185
284
|
// A run that published but failed to push its tags leaves them here for the next one.
|
|
186
|
-
const pushed = await pushTags(
|
|
285
|
+
const pushed = await pushTags(stepGit, stepRun, dir, current);
|
|
187
286
|
return pushed.length ? `pushed tags ${pushed.join(", ")}` : "nothing to release";
|
|
188
287
|
}
|
|
189
288
|
// Read it before changing anything, so a missing token stops the run with nothing to undo.
|
|
289
|
+
await live();
|
|
190
290
|
const token = await readToken(exec);
|
|
191
291
|
if (pending.length) {
|
|
192
|
-
branch = `release/${await
|
|
292
|
+
branch = `release/${await stepGit(dir, "rev-parse", "--short", "HEAD")}`;
|
|
293
|
+
await pruneStale(stepGit, root, dir, branch);
|
|
193
294
|
log(`versioning ${pending.length} changeset(s) on ${branch}`);
|
|
194
|
-
await
|
|
195
|
-
|
|
196
|
-
|
|
295
|
+
await stepGit(dir, "switch", "-c", branch);
|
|
296
|
+
await stepRun("pnpm", ["install", "--frozen-lockfile"], dir);
|
|
297
|
+
await stepRun("pnpm", versionCommand(dir), dir);
|
|
197
298
|
// Bumped workspace specifiers change the lockfile, which the frozen install below needs.
|
|
198
|
-
|
|
199
|
-
await
|
|
200
|
-
await
|
|
201
|
-
const sha = await
|
|
299
|
+
await stepRun("pnpm", ["install", "--lockfile-only"], dir);
|
|
300
|
+
await stepGit(dir, "add", "-A");
|
|
301
|
+
await stepGit(dir, "commit", "-m", VERSION_TITLE);
|
|
302
|
+
const sha = await stepGit(dir, "rev-parse", "HEAD");
|
|
202
303
|
// The pre-push hook runs the check and posts `check`.
|
|
203
|
-
|
|
304
|
+
await stepRun("git", ["push", "-u", "origin", branch], dir);
|
|
204
305
|
remoteBranch = branch;
|
|
205
|
-
repo = (await
|
|
206
|
-
const url = await
|
|
306
|
+
repo = (await step("gh", ["repo", "view", "--json", "nameWithOwner", "-q", ".nameWithOwner"])).trim();
|
|
307
|
+
const url = await step("gh", [
|
|
207
308
|
"pr",
|
|
208
309
|
"create",
|
|
209
310
|
"--repo",
|
|
@@ -223,41 +324,35 @@ export async function release(exec, context) {
|
|
|
223
324
|
log(`opened #${pr}; waiting for check and pr-title`);
|
|
224
325
|
if (!(await context.prTitle(pr)))
|
|
225
326
|
throw new Error(`#${pr} has a failing pr-title`);
|
|
226
|
-
await waitForStatuses(
|
|
327
|
+
await waitForStatuses(step, sha, ["check", "pr-title"]);
|
|
227
328
|
// --repo keeps gh from switching branches in this worktree after the merge.
|
|
228
|
-
await
|
|
229
|
-
const merged = JSON.parse(await
|
|
329
|
+
await step("gh", ["pr", "merge", pr, "--repo", repo, "--squash", "--delete-branch"]);
|
|
330
|
+
const merged = JSON.parse(await step("gh", ["pr", "view", pr, "--repo", repo, "--json", "state,mergeCommit"]));
|
|
230
331
|
if (merged.state !== "MERGED" || !merged.mergeCommit) {
|
|
231
332
|
throw new Error(`#${pr} did not merge (state ${merged.state})`);
|
|
232
333
|
}
|
|
233
334
|
// Merging deleted the branch.
|
|
234
335
|
remoteBranch = undefined;
|
|
235
336
|
log(`merged #${pr} as ${merged.mergeCommit.oid.slice(0, 7)}`);
|
|
236
|
-
await
|
|
237
|
-
await
|
|
238
|
-
packages = await unpublished(
|
|
337
|
+
await stepGit(dir, "fetch", "origin");
|
|
338
|
+
await stepGit(dir, "switch", "--detach", merged.mergeCommit.oid);
|
|
339
|
+
packages = await unpublished(step, await publicPackages(step, dir));
|
|
239
340
|
}
|
|
240
|
-
|
|
341
|
+
await stepRun("pnpm", ["install", "--frozen-lockfile"], dir);
|
|
241
342
|
// The same check the push required, on the commit being published.
|
|
242
|
-
|
|
343
|
+
await stepRun(process.execPath, [context.cli, "status"], dir);
|
|
344
|
+
await live();
|
|
243
345
|
log(`publishing ${packages.map(id).join(", ") || "nothing new"}`);
|
|
244
|
-
await publish(dir, token);
|
|
346
|
+
await track(publish(dir, token, abort.signal));
|
|
245
347
|
// Every current version, so a tag an earlier half-failed run left unpushed goes too.
|
|
246
|
-
await pushTags(
|
|
247
|
-
await verify(
|
|
348
|
+
await pushTags(stepGit, stepRun, dir, await publicPackages(step, dir));
|
|
349
|
+
await verify(step, packages);
|
|
248
350
|
return packages.length ? `released ${packages.map(id).join(", ")}` : "nothing to release";
|
|
249
351
|
}
|
|
250
352
|
finally {
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
: git(root, "push", "origin", "--delete", remoteBranch)).catch((error) => log(`cleanup failed: ${error.message}`));
|
|
256
|
-
}
|
|
257
|
-
await git(root, "worktree", "remove", "--force", dir).catch(() => { });
|
|
258
|
-
if (branch)
|
|
259
|
-
await git(root, "branch", "-D", branch).catch(() => { });
|
|
260
|
-
rmSync(temp, { recursive: true, force: true });
|
|
261
|
-
unlock();
|
|
353
|
+
finishing = true;
|
|
354
|
+
await cleanup();
|
|
355
|
+
for (const signal of SIGNALS)
|
|
356
|
+
process.off(signal, onSignal);
|
|
262
357
|
}
|
|
263
358
|
}
|