@jonmilner/local-ci 0.3.0 → 0.3.2
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 +3 -3
- package/dist/release.d.ts +10 -1
- package/dist/release.js +203 -55
- 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
|
-
3. It checks each published `name@version` with `npm view
|
|
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
|
-
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. 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
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Exec } from "./exec.ts";
|
|
2
|
+
import { type WaitOptions } from "./github.ts";
|
|
2
3
|
/** The macOS Keychain generic password that holds the npm token. */
|
|
3
4
|
export declare const TOKEN_SERVICE = "local-ci-npm";
|
|
4
5
|
/** npm and pnpm read a registry token from this variable; it is set only for `pnpm release`. */
|
|
@@ -11,12 +12,19 @@ export interface Package {
|
|
|
11
12
|
export declare function takeLock(file: string): () => void;
|
|
12
13
|
/** The changeset files waiting in `dir`'s `.changeset` folder. */
|
|
13
14
|
export declare function pendingChangesets(dir: string): string[];
|
|
15
|
+
/** How `dir` versions its changesets: its root `version-packages` script, else `changeset version`. */
|
|
16
|
+
export declare function versionCommand(dir: string): string[];
|
|
14
17
|
/** The workspace's public packages at `dir`, with the versions in their package.json. */
|
|
15
18
|
export declare function publicPackages(exec: Exec, dir: string): Promise<Package[]>;
|
|
16
19
|
/** Whether npm has this exact version. A 404 means no; any other error is thrown. */
|
|
17
20
|
export declare function isPublished(exec: Exec, pkg: Package): Promise<boolean>;
|
|
18
21
|
/** Reads the npm token from the Keychain, failing with the setup doc when it is missing. */
|
|
19
22
|
export declare function readToken(exec: Exec): Promise<string>;
|
|
23
|
+
/**
|
|
24
|
+
* Polls npm until each package is visible. The registry can take minutes to list a version
|
|
25
|
+
* after a publish, so a timeout here means the publish ran but npm hasn't caught up yet.
|
|
26
|
+
*/
|
|
27
|
+
export declare function verify(exec: Exec, packages: Package[], options?: WaitOptions): Promise<void>;
|
|
20
28
|
export interface ReleaseContext {
|
|
21
29
|
/** The checkout the command runs from. */
|
|
22
30
|
root: string;
|
|
@@ -29,6 +37,7 @@ export interface ReleaseContext {
|
|
|
29
37
|
}
|
|
30
38
|
/**
|
|
31
39
|
* 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.
|
|
40
|
+
* verifies the publish. Resolves with a one-line summary. SIGINT, SIGTERM, SIGHUP or SIGQUIT runs
|
|
41
|
+
* the same cleanup as a failure, then exits 128 + the signal number.
|
|
33
42
|
*/
|
|
34
43
|
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,8 +9,13 @@ 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
|
+
// How long verify() waits for npm to list what was just published.
|
|
13
|
+
const NPM_WAIT_MS = 300_000;
|
|
14
|
+
const TEMP_PREFIX = "local-ci-release-";
|
|
15
|
+
// SIGHUP and SIGQUIT too, since the terminal sends them to node's group and `pnpm release` has its own.
|
|
16
|
+
const SIGNALS = ["SIGINT", "SIGTERM", "SIGHUP", "SIGQUIT"];
|
|
12
17
|
// What npm and pnpm print when the registry rejects the token, or wants an OTP it can't bypass.
|
|
13
|
-
const AUTH_ERROR = /\b(?:E401|E403|EOTP|401 Unauthorized|403 Forbidden)\b/;
|
|
18
|
+
const AUTH_ERROR = /\b(?:E401|E403|EOTP|ERR_PNPM_OTP_NON_INTERACTIVE|401 Unauthorized|403 Forbidden)\b/;
|
|
14
19
|
const id = ({ name, version }) => `${name}@${version}`;
|
|
15
20
|
function alive(pid) {
|
|
16
21
|
if (!Number.isInteger(pid) || pid <= 0)
|
|
@@ -58,6 +63,14 @@ export function pendingChangesets(dir) {
|
|
|
58
63
|
return [];
|
|
59
64
|
return readdirSync(folder).filter((file) => file.endsWith(".md") && file !== "README.md");
|
|
60
65
|
}
|
|
66
|
+
/** How `dir` versions its changesets: its root `version-packages` script, else `changeset version`. */
|
|
67
|
+
export function versionCommand(dir) {
|
|
68
|
+
const { scripts } = JSON.parse(readFileSync(join(dir, "package.json"), "utf8"));
|
|
69
|
+
const script = scripts?.["version-packages"];
|
|
70
|
+
return typeof script === "string" && script.trim()
|
|
71
|
+
? ["run", "version-packages"]
|
|
72
|
+
: ["exec", "changeset", "version"];
|
|
73
|
+
}
|
|
61
74
|
/** The workspace's public packages at `dir`, with the versions in their package.json. */
|
|
62
75
|
export async function publicPackages(exec, dir) {
|
|
63
76
|
const projects = JSON.parse(await exec("pnpm", ["-C", dir, "ls", "-r", "--depth", "-1", "--json"]));
|
|
@@ -101,14 +114,38 @@ function run(file, args, cwd) {
|
|
|
101
114
|
}
|
|
102
115
|
}
|
|
103
116
|
/** Runs `pnpm release` with the token in its env only, teeing its output to spot a rejected token. */
|
|
104
|
-
function publish(dir, token) {
|
|
117
|
+
function publish(dir, token, signal) {
|
|
105
118
|
return new Promise((resolve, reject) => {
|
|
106
119
|
const child = spawn("pnpm", ["release"], {
|
|
107
120
|
cwd: dir,
|
|
121
|
+
// Its own session and group, so an interrupt stops the publish it started, not just pnpm.
|
|
122
|
+
detached: true,
|
|
108
123
|
// CI makes changesets fail on an OTP request instead of prompting on an ignored stdin.
|
|
109
124
|
env: { ...process.env, CI: "true", [TOKEN_ENV]: token },
|
|
110
125
|
stdio: ["ignore", "pipe", "pipe"],
|
|
111
126
|
});
|
|
127
|
+
// The group no longer gets the terminal's signals, so these are the only things that stop it.
|
|
128
|
+
// An interrupt asks it to stop and the close below settles once it has; exiting first kills it.
|
|
129
|
+
const killGroup = (name) => {
|
|
130
|
+
try {
|
|
131
|
+
if (child.pid)
|
|
132
|
+
process.kill(-child.pid, name);
|
|
133
|
+
}
|
|
134
|
+
catch {
|
|
135
|
+
// Already gone.
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
const stop = () => killGroup("SIGTERM");
|
|
139
|
+
const kill = () => killGroup("SIGKILL");
|
|
140
|
+
if (signal.aborted)
|
|
141
|
+
stop();
|
|
142
|
+
else
|
|
143
|
+
signal.addEventListener("abort", stop, { once: true });
|
|
144
|
+
process.once("exit", kill);
|
|
145
|
+
const settled = () => {
|
|
146
|
+
signal.removeEventListener("abort", stop);
|
|
147
|
+
process.off("exit", kill);
|
|
148
|
+
};
|
|
112
149
|
let authError = false;
|
|
113
150
|
const tee = (stream, sink) => {
|
|
114
151
|
// Keep a tail so a match split across chunks is still seen.
|
|
@@ -123,87 +160,202 @@ function publish(dir, token) {
|
|
|
123
160
|
};
|
|
124
161
|
tee(child.stdout, process.stdout);
|
|
125
162
|
tee(child.stderr, process.stderr);
|
|
126
|
-
child.on("error",
|
|
127
|
-
|
|
163
|
+
child.on("error", (error) => {
|
|
164
|
+
settled();
|
|
165
|
+
reject(error);
|
|
166
|
+
});
|
|
167
|
+
child.on("close", (code, killed) => {
|
|
168
|
+
settled();
|
|
169
|
+
// Any of the group that ignored the interrupt and let go of the pipes must not outlive the run.
|
|
170
|
+
if (signal.aborted)
|
|
171
|
+
kill();
|
|
128
172
|
if (code === 0)
|
|
129
173
|
return resolve();
|
|
130
174
|
reject(new Error(authError
|
|
131
175
|
? `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 ?
|
|
176
|
+
: `pnpm release failed (${code === null ? killed : `exit ${code}`})`));
|
|
133
177
|
});
|
|
134
178
|
});
|
|
135
179
|
}
|
|
136
|
-
/**
|
|
137
|
-
|
|
138
|
-
|
|
180
|
+
/**
|
|
181
|
+
* Polls npm until each package is visible. The registry can take minutes to list a version
|
|
182
|
+
* after a publish, so a timeout here means the publish ran but npm hasn't caught up yet.
|
|
183
|
+
*/
|
|
184
|
+
export async function verify(exec, packages, options = {}) {
|
|
185
|
+
const { intervalMs = 5_000, timeoutMs = NPM_WAIT_MS, sleep = defaultSleep } = options;
|
|
186
|
+
const ranHint = (pkg) => `The publish ran and may still appear; check with: npm view ${id(pkg)} version`;
|
|
187
|
+
// One budget for every package, so several slow ones still give up after timeoutMs.
|
|
188
|
+
let waited = 0;
|
|
139
189
|
for (const pkg of packages) {
|
|
140
|
-
for (
|
|
141
|
-
|
|
142
|
-
|
|
190
|
+
for (;;) {
|
|
191
|
+
let published;
|
|
192
|
+
try {
|
|
193
|
+
published = await isPublished(exec, pkg);
|
|
194
|
+
}
|
|
195
|
+
catch (error) {
|
|
196
|
+
throw new Error(`could not check ${id(pkg)} on npm: ${error.message}. ${ranHint(pkg)}`, {
|
|
197
|
+
cause: error,
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
if (published)
|
|
201
|
+
break;
|
|
202
|
+
if (waited + intervalMs > timeoutMs) {
|
|
203
|
+
throw new Error(`${id(pkg)} is not on npm after waiting ${timeoutMs / 1000}s. ${ranHint(pkg)}`);
|
|
204
|
+
}
|
|
143
205
|
await sleep(intervalMs);
|
|
206
|
+
waited += intervalMs;
|
|
144
207
|
}
|
|
145
208
|
}
|
|
146
209
|
}
|
|
210
|
+
const gitWith = (exec) => async (cwd, ...args) => (await exec("git", ["-C", cwd, ...args])).trim();
|
|
147
211
|
/** Pushes the local `name@version` tags of `packages` that origin lacks, resolving with their names. */
|
|
148
|
-
async function pushTags(git, dir, packages) {
|
|
212
|
+
async function pushTags(git, run, dir, packages) {
|
|
149
213
|
const local = new Set((await git(dir, "tag", "--list")).split("\n"));
|
|
150
214
|
const remote = new Set((await git(dir, "ls-remote", "--tags", "origin"))
|
|
151
215
|
.split("\n")
|
|
152
216
|
.map((line) => line.split("\t")[1]?.replace(/^refs\/tags\//, "")));
|
|
153
217
|
const missing = packages.map(id).filter((tag) => local.has(tag) && !remote.has(tag));
|
|
154
218
|
if (missing.length)
|
|
155
|
-
run("git", ["push", "origin", ...missing.map((tag) => `refs/tags/${tag}`)], dir);
|
|
219
|
+
await run("git", ["push", "origin", ...missing.map((tag) => `refs/tags/${tag}`)], dir);
|
|
156
220
|
return missing;
|
|
157
221
|
}
|
|
222
|
+
/**
|
|
223
|
+
* Removes the temp worktrees and `release/*` branches that runs killed before their cleanup left,
|
|
224
|
+
* 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.
|
|
225
|
+
*/
|
|
226
|
+
async function pruneStale(git, root, own, branch) {
|
|
227
|
+
// Forgets worktrees whose directory is gone, so their branch can be deleted.
|
|
228
|
+
await git(root, "worktree", "prune");
|
|
229
|
+
const branches = new Set([branch]);
|
|
230
|
+
for (const entry of (await git(root, "worktree", "list", "--porcelain")).split("\n\n")) {
|
|
231
|
+
const path = /^worktree (.+)$/m.exec(entry)?.[1];
|
|
232
|
+
// Only a temp directory as `release` creates it, other than this run's. Its unique name tells
|
|
233
|
+
// them apart, since git may report a path with its symlinks resolved.
|
|
234
|
+
const temp = path && basename(path) === "worktree" ? basename(dirname(path)) : "";
|
|
235
|
+
if (!path || !temp.startsWith(TEMP_PREFIX) || temp === basename(dirname(own)))
|
|
236
|
+
continue;
|
|
237
|
+
// Twice, so a locked worktree goes too.
|
|
238
|
+
await git(root, "worktree", "remove", "--force", "--force", path);
|
|
239
|
+
rmSync(dirname(path), { recursive: true, force: true });
|
|
240
|
+
const held = /^branch refs\/heads\/(release\/.+)$/m.exec(entry)?.[1];
|
|
241
|
+
if (held)
|
|
242
|
+
branches.add(held);
|
|
243
|
+
}
|
|
244
|
+
for (const name of branches) {
|
|
245
|
+
if (await git(root, "branch", "--list", name))
|
|
246
|
+
await git(root, "branch", "-D", name);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
158
249
|
/**
|
|
159
250
|
* 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.
|
|
251
|
+
* verifies the publish. Resolves with a one-line summary. SIGINT, SIGTERM, SIGHUP or SIGQUIT runs
|
|
252
|
+
* the same cleanup as a failure, then exits 128 + the signal number.
|
|
161
253
|
*/
|
|
162
254
|
export async function release(exec, context) {
|
|
163
255
|
const { root, log } = context;
|
|
164
|
-
const git =
|
|
256
|
+
const git = gitWith(exec);
|
|
165
257
|
if (await git(root, "status", "--porcelain")) {
|
|
166
258
|
throw new Error("the working tree has uncommitted or untracked files; commit or stash them first");
|
|
167
259
|
}
|
|
168
260
|
await git(root, "fetch", "origin");
|
|
169
261
|
const unlock = takeLock(context.lock);
|
|
170
|
-
const temp = mkdtempSync(join(tmpdir(),
|
|
262
|
+
const temp = mkdtempSync(join(tmpdir(), TEMP_PREFIX));
|
|
171
263
|
const dir = join(temp, "worktree");
|
|
172
264
|
let branch;
|
|
173
265
|
// Set from the push until the merge, so a failure in between closes the PR and deletes the branch.
|
|
174
266
|
let remoteBranch;
|
|
175
267
|
let repo;
|
|
176
268
|
let pr;
|
|
269
|
+
let interrupted;
|
|
270
|
+
const abort = new AbortController();
|
|
271
|
+
// Once a signal arrives no step starts, so nothing races the cleanup or outlives it.
|
|
272
|
+
const live = async () => {
|
|
273
|
+
// A signal that arrived during a synchronous step is delivered only once the event loop turns.
|
|
274
|
+
await new Promise((resolve) => setImmediate(resolve));
|
|
275
|
+
if (interrupted)
|
|
276
|
+
throw new Error(`interrupted by ${interrupted}`);
|
|
277
|
+
};
|
|
278
|
+
// The step running now, which the cleanup waits for so it never pulls the worktree from under it.
|
|
279
|
+
let inFlight = Promise.resolve();
|
|
280
|
+
const track = (promise) => {
|
|
281
|
+
inFlight = promise.catch(() => { });
|
|
282
|
+
return promise;
|
|
283
|
+
};
|
|
284
|
+
const step = async (file, args) => {
|
|
285
|
+
await live();
|
|
286
|
+
return track(exec(file, args));
|
|
287
|
+
};
|
|
288
|
+
const stepGit = gitWith(step);
|
|
289
|
+
const stepRun = async (file, args, cwd) => {
|
|
290
|
+
await live();
|
|
291
|
+
run(file, args, cwd);
|
|
292
|
+
};
|
|
293
|
+
// Set once the run is cleaning up on its own, so a signal then doesn't turn a success into an exit.
|
|
294
|
+
let finishing = false;
|
|
295
|
+
let cleaning;
|
|
296
|
+
const cleanup = () => (cleaning ??= (async () => {
|
|
297
|
+
if (remoteBranch) {
|
|
298
|
+
log(`cleaning up ${pr ? `#${pr} and ` : ""}${remoteBranch} so the next run starts fresh`);
|
|
299
|
+
await (pr && repo
|
|
300
|
+
? exec("gh", ["pr", "close", pr, "--repo", repo, "--delete-branch"])
|
|
301
|
+
: git(root, "push", "origin", "--delete", remoteBranch)).catch((error) => log(`cleanup failed: ${error.message}`));
|
|
302
|
+
}
|
|
303
|
+
await git(root, "worktree", "remove", "--force", dir).catch(() => { });
|
|
304
|
+
if (branch)
|
|
305
|
+
await git(root, "branch", "-D", branch).catch(() => { });
|
|
306
|
+
rmSync(temp, { recursive: true, force: true });
|
|
307
|
+
unlock();
|
|
308
|
+
})());
|
|
309
|
+
let signals = 0;
|
|
310
|
+
const onSignal = (signal) => {
|
|
311
|
+
// A second signal exits at once, through process.exit so the publish's exit hook still kills it.
|
|
312
|
+
if (signals++)
|
|
313
|
+
process.exit(128 + constants.signals[signal]);
|
|
314
|
+
if (finishing)
|
|
315
|
+
return;
|
|
316
|
+
interrupted = signal;
|
|
317
|
+
abort.abort();
|
|
318
|
+
log(`${signal}: cleaning up before exiting; send it again to exit now`);
|
|
319
|
+
void inFlight
|
|
320
|
+
.then(() => cleanup())
|
|
321
|
+
.finally(() => process.exit(128 + constants.signals[signal]));
|
|
322
|
+
};
|
|
323
|
+
for (const signal of SIGNALS)
|
|
324
|
+
process.on(signal, onSignal);
|
|
177
325
|
try {
|
|
326
|
+
await live();
|
|
327
|
+
// Unguarded, like the token read below, so the fallback can't swallow an interrupt.
|
|
178
328
|
const remoteHead = await git(root, "symbolic-ref", "--short", "refs/remotes/origin/HEAD").catch(() => "origin/main");
|
|
179
329
|
const base = remoteHead.replace(/^origin\//, "");
|
|
180
|
-
await
|
|
330
|
+
await stepGit(root, "worktree", "add", "--detach", dir, `origin/${base}`);
|
|
181
331
|
const pending = pendingChangesets(dir);
|
|
182
|
-
const current = await publicPackages(
|
|
183
|
-
let packages = await unpublished(
|
|
332
|
+
const current = await publicPackages(step, dir);
|
|
333
|
+
let packages = await unpublished(step, current);
|
|
184
334
|
if (!pending.length && !packages.length) {
|
|
185
335
|
// A run that published but failed to push its tags leaves them here for the next one.
|
|
186
|
-
const pushed = await pushTags(
|
|
336
|
+
const pushed = await pushTags(stepGit, stepRun, dir, current);
|
|
187
337
|
return pushed.length ? `pushed tags ${pushed.join(", ")}` : "nothing to release";
|
|
188
338
|
}
|
|
189
339
|
// Read it before changing anything, so a missing token stops the run with nothing to undo.
|
|
340
|
+
await live();
|
|
190
341
|
const token = await readToken(exec);
|
|
191
342
|
if (pending.length) {
|
|
192
|
-
branch = `release/${await
|
|
343
|
+
branch = `release/${await stepGit(dir, "rev-parse", "--short", "HEAD")}`;
|
|
344
|
+
await pruneStale(stepGit, root, dir, branch);
|
|
193
345
|
log(`versioning ${pending.length} changeset(s) on ${branch}`);
|
|
194
|
-
await
|
|
195
|
-
|
|
196
|
-
|
|
346
|
+
await stepGit(dir, "switch", "-c", branch);
|
|
347
|
+
await stepRun("pnpm", ["install", "--frozen-lockfile"], dir);
|
|
348
|
+
await stepRun("pnpm", versionCommand(dir), dir);
|
|
197
349
|
// Bumped workspace specifiers change the lockfile, which the frozen install below needs.
|
|
198
|
-
|
|
199
|
-
await
|
|
200
|
-
await
|
|
201
|
-
const sha = await
|
|
350
|
+
await stepRun("pnpm", ["install", "--lockfile-only"], dir);
|
|
351
|
+
await stepGit(dir, "add", "-A");
|
|
352
|
+
await stepGit(dir, "commit", "-m", VERSION_TITLE);
|
|
353
|
+
const sha = await stepGit(dir, "rev-parse", "HEAD");
|
|
202
354
|
// The pre-push hook runs the check and posts `check`.
|
|
203
|
-
|
|
355
|
+
await stepRun("git", ["push", "-u", "origin", branch], dir);
|
|
204
356
|
remoteBranch = branch;
|
|
205
|
-
repo = (await
|
|
206
|
-
const url = await
|
|
357
|
+
repo = (await step("gh", ["repo", "view", "--json", "nameWithOwner", "-q", ".nameWithOwner"])).trim();
|
|
358
|
+
const url = await step("gh", [
|
|
207
359
|
"pr",
|
|
208
360
|
"create",
|
|
209
361
|
"--repo",
|
|
@@ -223,41 +375,37 @@ export async function release(exec, context) {
|
|
|
223
375
|
log(`opened #${pr}; waiting for check and pr-title`);
|
|
224
376
|
if (!(await context.prTitle(pr)))
|
|
225
377
|
throw new Error(`#${pr} has a failing pr-title`);
|
|
226
|
-
await waitForStatuses(
|
|
378
|
+
await waitForStatuses(step, sha, ["check", "pr-title"]);
|
|
227
379
|
// --repo keeps gh from switching branches in this worktree after the merge.
|
|
228
|
-
await
|
|
229
|
-
const merged = JSON.parse(await
|
|
380
|
+
await step("gh", ["pr", "merge", pr, "--repo", repo, "--squash", "--delete-branch"]);
|
|
381
|
+
const merged = JSON.parse(await step("gh", ["pr", "view", pr, "--repo", repo, "--json", "state,mergeCommit"]));
|
|
230
382
|
if (merged.state !== "MERGED" || !merged.mergeCommit) {
|
|
231
383
|
throw new Error(`#${pr} did not merge (state ${merged.state})`);
|
|
232
384
|
}
|
|
233
385
|
// Merging deleted the branch.
|
|
234
386
|
remoteBranch = undefined;
|
|
235
387
|
log(`merged #${pr} as ${merged.mergeCommit.oid.slice(0, 7)}`);
|
|
236
|
-
await
|
|
237
|
-
await
|
|
238
|
-
packages = await unpublished(
|
|
388
|
+
await stepGit(dir, "fetch", "origin");
|
|
389
|
+
await stepGit(dir, "switch", "--detach", merged.mergeCommit.oid);
|
|
390
|
+
packages = await unpublished(step, await publicPackages(step, dir));
|
|
239
391
|
}
|
|
240
|
-
|
|
392
|
+
await stepRun("pnpm", ["install", "--frozen-lockfile"], dir);
|
|
241
393
|
// The same check the push required, on the commit being published.
|
|
242
|
-
|
|
394
|
+
await stepRun(process.execPath, [context.cli, "status"], dir);
|
|
395
|
+
await live();
|
|
243
396
|
log(`publishing ${packages.map(id).join(", ") || "nothing new"}`);
|
|
244
|
-
await publish(dir, token);
|
|
397
|
+
await track(publish(dir, token, abort.signal));
|
|
245
398
|
// Every current version, so a tag an earlier half-failed run left unpushed goes too.
|
|
246
|
-
await pushTags(
|
|
247
|
-
|
|
399
|
+
await pushTags(stepGit, stepRun, dir, await publicPackages(step, dir));
|
|
400
|
+
if (packages.length)
|
|
401
|
+
log(`waiting up to ${NPM_WAIT_MS / 60_000} minutes for npm to list ${packages.map(id).join(", ")}`);
|
|
402
|
+
await verify(step, packages);
|
|
248
403
|
return packages.length ? `released ${packages.map(id).join(", ")}` : "nothing to release";
|
|
249
404
|
}
|
|
250
405
|
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();
|
|
406
|
+
finishing = true;
|
|
407
|
+
await cleanup();
|
|
408
|
+
for (const signal of SIGNALS)
|
|
409
|
+
process.off(signal, onSignal);
|
|
262
410
|
}
|
|
263
411
|
}
|