@jonmilner/local-ci 0.8.0 → 0.9.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 +1 -1
- package/dist/cli.js +11 -2
- package/dist/release.d.ts +28 -4
- package/dist/release.js +70 -13
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -31,7 +31,7 @@ That writes `pre-push` and `commit-msg` shims, each one line that calls local-ci
|
|
|
31
31
|
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.
|
|
32
32
|
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.
|
|
33
33
|
|
|
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
|
|
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
36
|
- `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
37
|
- skips a repo with no such clone, logging why;
|
package/dist/cli.js
CHANGED
|
@@ -12,7 +12,7 @@ import { hooksDir, installHooks, missingHooks } from "./hooks.js";
|
|
|
12
12
|
import { installRenovateAgent, renovateLog, rotateLog, uninstallRenovateAgent } from "./launchd.js";
|
|
13
13
|
import { LockHeld, takeLock, waitForLock } from "./lock.js";
|
|
14
14
|
import { cannotCheckInPlace, githubRepo, parsePrePush, shasToCheck } from "./push.js";
|
|
15
|
-
import { release } from "./release.js";
|
|
15
|
+
import { release, takeEnvToken } from "./release.js";
|
|
16
16
|
import { projectsDir, renovate } from "./renovate.js";
|
|
17
17
|
import { lintTitle } from "./title.js";
|
|
18
18
|
const USAGE = `Usage:
|
|
@@ -299,12 +299,21 @@ async function status(ref = "HEAD") {
|
|
|
299
299
|
await post(current, sha);
|
|
300
300
|
}
|
|
301
301
|
async function runRelease() {
|
|
302
|
+
// First, before any child process runs.
|
|
303
|
+
const envToken = takeEnvToken();
|
|
302
304
|
const { root, lock } = await repo();
|
|
303
305
|
// Off by default, so a repo that publishes some other way can't release by accident.
|
|
304
306
|
if (!releaseEnabled(root))
|
|
305
307
|
return out("release not enabled");
|
|
306
308
|
await assertGhAuth(exec);
|
|
307
|
-
out(await release(exec, {
|
|
309
|
+
out(await release(exec, {
|
|
310
|
+
root,
|
|
311
|
+
lock,
|
|
312
|
+
cli: fileURLToPath(import.meta.url),
|
|
313
|
+
prTitle,
|
|
314
|
+
log,
|
|
315
|
+
envToken,
|
|
316
|
+
}));
|
|
308
317
|
}
|
|
309
318
|
async function runRenovate([flag, ...extra]) {
|
|
310
319
|
const cli = fileURLToPath(import.meta.url);
|
package/dist/release.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { type Exec } from "./exec.ts";
|
|
2
2
|
import { type WaitOptions } from "./github.ts";
|
|
3
|
-
/** The
|
|
4
|
-
export declare const
|
|
3
|
+
/** The npm token's name, both as an environment variable and in the `secret` store. */
|
|
4
|
+
export declare const TOKEN_NAME = "NPM_TOKEN_JONMILNER";
|
|
5
|
+
/** How many days ahead of the token's expiry a release starts warning. */
|
|
6
|
+
export declare const EXPIRY_WARN_DAYS = 14;
|
|
5
7
|
/** npm and pnpm read a registry token from this variable; it is set only for `pnpm release`. */
|
|
6
8
|
export declare const TOKEN_ENV = "NPM_CONFIG_//registry.npmjs.org/:_authToken";
|
|
7
9
|
export interface Package {
|
|
@@ -16,8 +18,28 @@ export declare function versionCommand(dir: string): string[];
|
|
|
16
18
|
export declare function publicPackages(exec: Exec, dir: string): Promise<Package[]>;
|
|
17
19
|
/** Whether npm has this exact version. A 404 means no; any other error is thrown. */
|
|
18
20
|
export declare function isPublished(exec: Exec, pkg: Package): Promise<boolean>;
|
|
19
|
-
|
|
20
|
-
|
|
21
|
+
export interface Token {
|
|
22
|
+
value: string;
|
|
23
|
+
/** Where it came from, for the message when npm rejects it. */
|
|
24
|
+
source: string;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The warning for a token in the `secret` store that expires within EXPIRY_WARN_DAYS or has
|
|
28
|
+
* expired, from `secret list` (names and expiry dates, never values). Nothing when `secret list`
|
|
29
|
+
* fails or gives the token no expiry, since the warning is advice and local-ci needs no store.
|
|
30
|
+
*/
|
|
31
|
+
export declare function expiryWarning(exec: Exec, now?: Date): Promise<string | undefined>;
|
|
32
|
+
/**
|
|
33
|
+
* The npm token: $NPM_TOKEN_JONMILNER if set, else `secret get NPM_TOKEN_JONMILNER`, warning
|
|
34
|
+
* through `log` when the stored one is near expiry. Fails with the setup doc when neither has it.
|
|
35
|
+
* Removes the variable from `env`, so only `pnpm release` gets the token, not every child.
|
|
36
|
+
*/
|
|
37
|
+
/**
|
|
38
|
+
* Takes $NPM_TOKEN_JONMILNER out of `env`, so no child the release runs (git, npm, the pre-push
|
|
39
|
+
* hook) inherits it; only `pnpm release` gets the token, under npm's own variable.
|
|
40
|
+
*/
|
|
41
|
+
export declare function takeEnvToken(env?: NodeJS.ProcessEnv): string | undefined;
|
|
42
|
+
export declare function readToken(exec: Exec, log: (message: string) => void, env?: NodeJS.ProcessEnv, now?: Date): Promise<Token>;
|
|
21
43
|
/**
|
|
22
44
|
* Polls npm until each package is visible. The registry can take minutes to list a version
|
|
23
45
|
* after a publish, so a timeout here means the publish ran but npm hasn't caught up yet.
|
|
@@ -32,6 +54,8 @@ export interface ReleaseContext {
|
|
|
32
54
|
/** Lints the PR title and posts `pr-title`, resolving false when the title fails. */
|
|
33
55
|
prTitle: (pr: string) => Promise<boolean>;
|
|
34
56
|
log: (message: string) => void;
|
|
57
|
+
/** $NPM_TOKEN_JONMILNER, taken out of process.env by takeEnvToken before the CLI ran anything. */
|
|
58
|
+
envToken?: string;
|
|
35
59
|
}
|
|
36
60
|
/**
|
|
37
61
|
* Versions pending changesets through a merged PR, publishes what npm lacks, pushes the tags and
|
package/dist/release.js
CHANGED
|
@@ -5,8 +5,10 @@ import { basename, dirname, join } from "node:path";
|
|
|
5
5
|
import { run } from "./exec.js";
|
|
6
6
|
import { defaultSleep, waitForStatuses } from "./github.js";
|
|
7
7
|
import { takeLock } from "./lock.js";
|
|
8
|
-
/** The
|
|
9
|
-
export const
|
|
8
|
+
/** The npm token's name, both as an environment variable and in the `secret` store. */
|
|
9
|
+
export const TOKEN_NAME = "NPM_TOKEN_JONMILNER";
|
|
10
|
+
/** How many days ahead of the token's expiry a release starts warning. */
|
|
11
|
+
export const EXPIRY_WARN_DAYS = 14;
|
|
10
12
|
/** npm and pnpm read a registry token from this variable; it is set only for `pnpm release`. */
|
|
11
13
|
export const TOKEN_ENV = "NPM_CONFIG_//registry.npmjs.org/:_authToken";
|
|
12
14
|
const SETUP_DOC = "https://github.com/jonmilner/config/blob/main/docs/new-machine.md#npm-publish-token";
|
|
@@ -56,19 +58,74 @@ async function unpublished(exec, packages) {
|
|
|
56
58
|
const published = await Promise.all(packages.map((pkg) => isPublished(exec, pkg)));
|
|
57
59
|
return packages.filter((_, index) => !published[index]);
|
|
58
60
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
61
|
+
const ROTATE = `secret set ${TOKEN_NAME} --expires <YYYY-MM-DD>`;
|
|
62
|
+
const DAY_MS = 86_400_000;
|
|
63
|
+
/**
|
|
64
|
+
* The warning for a token in the `secret` store that expires within EXPIRY_WARN_DAYS or has
|
|
65
|
+
* expired, from `secret list` (names and expiry dates, never values). Nothing when `secret list`
|
|
66
|
+
* fails or gives the token no expiry, since the warning is advice and local-ci needs no store.
|
|
67
|
+
*/
|
|
68
|
+
export async function expiryWarning(exec, now = new Date()) {
|
|
69
|
+
let listing;
|
|
62
70
|
try {
|
|
63
|
-
|
|
71
|
+
listing = await exec("secret", ["list"]);
|
|
64
72
|
}
|
|
65
73
|
catch {
|
|
66
|
-
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
const line = listing.split("\n").find((entry) => entry.startsWith(`${TOKEN_NAME}\t`));
|
|
77
|
+
const date = /\texpire[sd] (\d{4}-\d{2}-\d{2})\b/.exec(line ?? "")?.[1];
|
|
78
|
+
if (!date)
|
|
79
|
+
return undefined;
|
|
80
|
+
const [year, month, day] = date.split("-").map(Number);
|
|
81
|
+
// Calendar days in the local date, as `secret list` counts them, so the time of day doesn't matter.
|
|
82
|
+
const today = Date.UTC(now.getFullYear(), now.getMonth(), now.getDate());
|
|
83
|
+
const days = Math.round((Date.UTC(year, month - 1, day) - today) / DAY_MS);
|
|
84
|
+
if (days < 0) {
|
|
85
|
+
return `warning: the npm token ${TOKEN_NAME} expired ${date}, so npm will likely reject it; rotate it with: ${ROTATE} (${SETUP_DOC})`;
|
|
86
|
+
}
|
|
87
|
+
if (days > EXPIRY_WARN_DAYS)
|
|
88
|
+
return undefined;
|
|
89
|
+
const left = days === 0 ? "today" : `in ${days} day${days === 1 ? "" : "s"}`;
|
|
90
|
+
return `warning: the npm token ${TOKEN_NAME} expires ${date} (${left}); rotate it with: ${ROTATE} (${SETUP_DOC})`;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* The npm token: $NPM_TOKEN_JONMILNER if set, else `secret get NPM_TOKEN_JONMILNER`, warning
|
|
94
|
+
* through `log` when the stored one is near expiry. Fails with the setup doc when neither has it.
|
|
95
|
+
* Removes the variable from `env`, so only `pnpm release` gets the token, not every child.
|
|
96
|
+
*/
|
|
97
|
+
/**
|
|
98
|
+
* Takes $NPM_TOKEN_JONMILNER out of `env`, so no child the release runs (git, npm, the pre-push
|
|
99
|
+
* hook) inherits it; only `pnpm release` gets the token, under npm's own variable.
|
|
100
|
+
*/
|
|
101
|
+
export function takeEnvToken(env = process.env) {
|
|
102
|
+
const value = env[TOKEN_NAME];
|
|
103
|
+
delete env[TOKEN_NAME];
|
|
104
|
+
return value;
|
|
105
|
+
}
|
|
106
|
+
export async function readToken(exec, log, env = process.env, now = new Date()) {
|
|
107
|
+
const fromEnv = env[TOKEN_NAME]?.trim();
|
|
108
|
+
delete env[TOKEN_NAME];
|
|
109
|
+
if (fromEnv)
|
|
110
|
+
return { value: fromEnv, source: `$${TOKEN_NAME}` };
|
|
111
|
+
let value = "";
|
|
112
|
+
let reason = "";
|
|
113
|
+
try {
|
|
114
|
+
value = (await exec("secret", ["get", TOKEN_NAME])).trim();
|
|
115
|
+
}
|
|
116
|
+
catch (error) {
|
|
117
|
+
// Not installed, no such name, or the store won't decrypt: none of them gives a token.
|
|
118
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
119
|
+
// Only the spawn's own ENOENT; one in secret's stderr is a file it lacks, shown as is.
|
|
120
|
+
reason = /\bspawn secret ENOENT\b/.test(message) ? "secret is not installed" : message;
|
|
67
121
|
}
|
|
68
|
-
if (!
|
|
69
|
-
throw new Error(`no npm token
|
|
122
|
+
if (!value) {
|
|
123
|
+
throw new Error(`no npm token: $${TOKEN_NAME} is unset and \`secret get ${TOKEN_NAME}\` gave none${reason ? ` (${reason})` : ""}. Store one with \`${ROTATE}\`: ${SETUP_DOC}`);
|
|
70
124
|
}
|
|
71
|
-
|
|
125
|
+
const warning = await expiryWarning(exec, now);
|
|
126
|
+
if (warning)
|
|
127
|
+
log(warning);
|
|
128
|
+
return { value, source: `\`secret get ${TOKEN_NAME}\`` };
|
|
72
129
|
}
|
|
73
130
|
/** The hint for checking each package on npm by hand. */
|
|
74
131
|
const npmView = (packages) => packages.length
|
|
@@ -85,7 +142,7 @@ function publish(dir, token, packages, signal) {
|
|
|
85
142
|
// Its own session and group, so an interrupt stops the publish it started, not just pnpm.
|
|
86
143
|
detached: true,
|
|
87
144
|
// CI makes changesets fail on an OTP request instead of prompting on an ignored stdin.
|
|
88
|
-
env: { ...process.env, CI: "true", [TOKEN_ENV]: token },
|
|
145
|
+
env: { ...process.env, CI: "true", [TOKEN_ENV]: token.value },
|
|
89
146
|
stdio: ["ignore", "pipe", "pipe"],
|
|
90
147
|
});
|
|
91
148
|
// The group no longer gets the terminal's signals, so these are the only things that stop it.
|
|
@@ -141,7 +198,7 @@ function publish(dir, token, packages, signal) {
|
|
|
141
198
|
reject(new Error(publishOver
|
|
142
199
|
? `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)}`
|
|
143
200
|
: authError
|
|
144
|
-
? `npm rejected the token
|
|
201
|
+
? `npm rejected the token from ${token.source}; it may have expired or lack "bypass 2FA". Rotate it with \`${ROTATE}\`: ${SETUP_DOC}`
|
|
145
202
|
: `pnpm release failed (${code === null ? killed : `exit ${code}`})`));
|
|
146
203
|
});
|
|
147
204
|
});
|
|
@@ -307,7 +364,7 @@ export async function release(exec, context) {
|
|
|
307
364
|
}
|
|
308
365
|
// Read it before changing anything, so a missing token stops the run with nothing to undo.
|
|
309
366
|
await live();
|
|
310
|
-
const token = await readToken(exec);
|
|
367
|
+
const token = await readToken(exec, log, { [TOKEN_NAME]: context.envToken });
|
|
311
368
|
if (pending.length) {
|
|
312
369
|
branch = `release/${await stepGit(dir, "rev-parse", "--short", "HEAD")}`;
|
|
313
370
|
await pruneStale(stepGit, root, dir, branch);
|