@jonmilner/local-ci 0.5.0 → 0.6.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 +3 -0
- package/commitlint.config.js +1 -1
- package/dist/cli.js +18 -4
- package/dist/launchd.d.ts +58 -0
- package/dist/launchd.js +171 -0
- package/package.json +2 -3
package/README.md
CHANGED
|
@@ -38,6 +38,9 @@ exec pnpm exec local-ci pre-push "$@"
|
|
|
38
38
|
|
|
39
39
|
One sweep runs at a time (`renovate.lock` in the state directory); a second exits with `renovate in progress`. A sweep killed before its cleanup leaves its worktree behind, and the next sweep removes it. It logs one timestamped line per PR and exits non-zero when any PR's check failed or could not run; a PR title that fails lint fails only `pr-title`.
|
|
40
40
|
|
|
41
|
+
- `local-ci renovate --install` (macOS): runs `local-ci renovate` on a schedule, so Renovate PRs get `check` and `pr-title` and automerge without you. It writes `~/Library/LaunchAgents/com.jonmilner.local-ci.renovate.plist` and loads it with `launchctl enable` (clearing any earlier `launchctl disable`) and `launchctl bootstrap`; the agent runs a sweep at once, at each login, and every 15 minutes after. It runs only while the Mac is awake: launchd skips the intervals the Mac slept through and runs one sweep on wake. launchd's own `PATH` lacks pnpm, gh and node, so the plist sets `PATH` to the installing shell's, less any `node_modules` directory and with the running node's directory last, and passes `LOCAL_CI_COMMAND`, `LOCAL_CI_PROJECTS` and `XDG_STATE_HOME` when set. No other shell variable reaches it, `GH_TOKEN` included, so `gh` must be logged in with its keyring (`gh auth login`). It runs `/usr/bin/env node` on that `PATH` and this copy of the CLI, through the package's `node_modules/<pkg>` link rather than pnpm's version-named store path, so upgrading node or local-ci keeps it working. Output goes to `renovate.log` in the state directory, moved to `renovate.log.1` at the start of a sweep once it passes 10MB. Re-running it replaces the agent; run it again after changing `PATH`, and install from a checkout that stays put (not a worktree you will delete), since the agent runs that copy. In this repo: `node packages/local-ci/src/cli.ts renovate --install`; elsewhere `pnpm exec local-ci renovate --install`.
|
|
42
|
+
- `local-ci renovate --uninstall`: unloads the agent (`launchctl bootout`) and deletes its plist; it prints `… is not installed` and exits 0 when there is none.
|
|
43
|
+
|
|
41
44
|
## Check command
|
|
42
45
|
|
|
43
46
|
`pnpm check` by default. Override it per repo in `package.json`:
|
package/commitlint.config.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
// lintTitle loads this file, so the shared config
|
|
1
|
+
// lintTitle loads this file, so the shared config resolves from this package.
|
|
2
2
|
export { default } from "@jonmilner/commitlint-config";
|
package/dist/cli.js
CHANGED
|
@@ -7,6 +7,7 @@ import { fileURLToPath } from "node:url";
|
|
|
7
7
|
import { appendLog, checkRunMessage, logLine, passedCommand, recordPass, releaseEnabled, resolveCommand, statePaths, } from "./config.js";
|
|
8
8
|
import { exec, run } from "./exec.js";
|
|
9
9
|
import { assertGhAuth, checkStatus, postStatus, prTitleStatus, viewPr, waitForCommit, waitForOpenPr, } from "./github.js";
|
|
10
|
+
import { installRenovateAgent, renovateLog, rotateLog, uninstallRenovateAgent } from "./launchd.js";
|
|
10
11
|
import { LockHeld, takeLock, waitForLock } from "./lock.js";
|
|
11
12
|
import { cannotCheckInPlace, githubRepo, parsePrePush, shasToCheck } from "./push.js";
|
|
12
13
|
import { release } from "./release.js";
|
|
@@ -18,7 +19,11 @@ const USAGE = `Usage:
|
|
|
18
19
|
local-ci pr-title [<pr>] lint the PR title and post the pr-title status
|
|
19
20
|
local-ci status [<sha>] post check for <sha> (default HEAD), running the check if needed
|
|
20
21
|
local-ci release version pending changesets through a PR, publish to npm and push tags
|
|
21
|
-
local-ci renovate check each open Renovate PR in your repos and post check and pr-title
|
|
22
|
+
local-ci renovate check each open Renovate PR in your repos and post check and pr-title
|
|
23
|
+
local-ci renovate --install
|
|
24
|
+
run renovate every 15 minutes with a launchd agent (macOS)
|
|
25
|
+
local-ci renovate --uninstall
|
|
26
|
+
remove that agent`;
|
|
22
27
|
const git = async (...args) => (await exec("git", args)).trim();
|
|
23
28
|
async function repo() {
|
|
24
29
|
const root = await git("rev-parse", "--show-toplevel");
|
|
@@ -269,9 +274,18 @@ async function runRelease() {
|
|
|
269
274
|
await assertGhAuth(exec);
|
|
270
275
|
out(await release(exec, { root, lock, cli: fileURLToPath(import.meta.url), prTitle, log }));
|
|
271
276
|
}
|
|
272
|
-
async function runRenovate() {
|
|
273
|
-
await assertGhAuth(exec);
|
|
277
|
+
async function runRenovate([flag, ...extra]) {
|
|
274
278
|
const cli = fileURLToPath(import.meta.url);
|
|
279
|
+
if (extra.length)
|
|
280
|
+
throw new Error(`renovate: unexpected ${extra.join(" ")}\n${USAGE}`);
|
|
281
|
+
if (flag === "--install")
|
|
282
|
+
return out(await installRenovateAgent({ exec, cli }));
|
|
283
|
+
if (flag === "--uninstall")
|
|
284
|
+
return out(await uninstallRenovateAgent({ exec }));
|
|
285
|
+
if (flag)
|
|
286
|
+
throw new Error(`renovate: unknown option ${flag}\n${USAGE}`);
|
|
287
|
+
rotateLog(renovateLog());
|
|
288
|
+
await assertGhAuth(exec);
|
|
275
289
|
const failed = await renovate({ exec, run, cli, projects: projectsDir() }, log);
|
|
276
290
|
if (failed)
|
|
277
291
|
process.exitCode = 1;
|
|
@@ -282,7 +296,7 @@ async function main([command, ...args]) {
|
|
|
282
296
|
if (command === "release")
|
|
283
297
|
return runRelease();
|
|
284
298
|
if (command === "renovate")
|
|
285
|
-
return runRenovate();
|
|
299
|
+
return runRenovate(args);
|
|
286
300
|
if (command === "post" || command === "pr-title" || command === "status") {
|
|
287
301
|
await assertGhAuth(exec);
|
|
288
302
|
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { Exec } from "./exec.ts";
|
|
2
|
+
/** The launchd label of the agent that runs `local-ci renovate`. */
|
|
3
|
+
export declare const RENOVATE_LABEL = "com.jonmilner.local-ci.renovate";
|
|
4
|
+
/** 15 minutes, in seconds. */
|
|
5
|
+
export declare const RENOVATE_INTERVAL = 900;
|
|
6
|
+
/** The agent's log is moved aside to `<log>.1` past this size, so it never grows without bound. */
|
|
7
|
+
export declare const LOG_MAX_BYTES: number;
|
|
8
|
+
/** The agent's log, which launchd appends the sweep's output to. */
|
|
9
|
+
export declare const renovateLog: (env?: NodeJS.ProcessEnv) => string;
|
|
10
|
+
/**
|
|
11
|
+
* Moves `file` to `<file>.1` once it passes `max` bytes. launchd opens the log for each run, so the
|
|
12
|
+
* run under way finishes in `.1` and the next starts a new file.
|
|
13
|
+
*/
|
|
14
|
+
export declare function rotateLog(file: string, max?: number): void;
|
|
15
|
+
/**
|
|
16
|
+
* The CLI path to run on a schedule. Under pnpm, `cli` is the real path in the version-named
|
|
17
|
+
* `node_modules/.pnpm/<pkg>@<version>…` directory, which the next upgrade deletes, so this prefers
|
|
18
|
+
* the package's stable link (`node_modules/<pkg>/…`) in `cwd` or the store's own project.
|
|
19
|
+
*/
|
|
20
|
+
export declare function stableCli(cli: string, cwd?: string): string;
|
|
21
|
+
/**
|
|
22
|
+
* The agent's PATH: the installing shell's, less any project's `node_modules/.bin` (which `pnpm
|
|
23
|
+
* exec` adds), with this node's directory last so node resolves even when the shell found it some
|
|
24
|
+
* other way. A directory a later upgrade removes is skipped, not fatal, since `env` searches on.
|
|
25
|
+
*/
|
|
26
|
+
export declare function agentPath(path: string, node: string): string;
|
|
27
|
+
export interface AgentOptions {
|
|
28
|
+
label: string;
|
|
29
|
+
/** The program and its arguments, absolute paths. */
|
|
30
|
+
args: string[];
|
|
31
|
+
env: Record<string, string>;
|
|
32
|
+
workingDirectory: string;
|
|
33
|
+
log: string;
|
|
34
|
+
interval: number;
|
|
35
|
+
}
|
|
36
|
+
/** A launchd agent plist that runs `args` at load and every `interval` seconds after. */
|
|
37
|
+
export declare function agentPlist({ label, args, env, workingDirectory, log, interval, }: AgentOptions): string;
|
|
38
|
+
export interface LaunchdContext {
|
|
39
|
+
exec: Exec;
|
|
40
|
+
/** This CLI, which the agent runs as `renovate`. */
|
|
41
|
+
cli: string;
|
|
42
|
+
/** The node running this CLI, whose directory the agent's PATH ends with. */
|
|
43
|
+
node?: string;
|
|
44
|
+
cwd?: string;
|
|
45
|
+
env?: NodeJS.ProcessEnv;
|
|
46
|
+
home?: string;
|
|
47
|
+
uid?: number;
|
|
48
|
+
platform?: NodeJS.Platform;
|
|
49
|
+
sleep?: (ms: number) => Promise<void>;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Writes the agent's plist and loads it, replacing an agent already loaded. It runs node through
|
|
53
|
+
* `env` on the installing shell's PATH (see `agentPath`), since launchd's own lacks pnpm, gh and
|
|
54
|
+
* node, and a node upgrade must not leave it pointing at a deleted binary.
|
|
55
|
+
*/
|
|
56
|
+
export declare function installRenovateAgent(context: LaunchdContext): Promise<string>;
|
|
57
|
+
/** Unloads the agent and deletes its plist; a no-op when it is not installed. */
|
|
58
|
+
export declare function uninstallRenovateAgent(context: Omit<LaunchdContext, "cli">): Promise<string>;
|
package/dist/launchd.js
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, realpathSync, renameSync, rmSync, statSync, writeFileSync, } from "node:fs";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { delimiter, dirname, join, sep } from "node:path";
|
|
4
|
+
import { stateDir } from "./config.js";
|
|
5
|
+
import { defaultSleep } from "./github.js";
|
|
6
|
+
/** The launchd label of the agent that runs `local-ci renovate`. */
|
|
7
|
+
export const RENOVATE_LABEL = "com.jonmilner.local-ci.renovate";
|
|
8
|
+
/** 15 minutes, in seconds. */
|
|
9
|
+
export const RENOVATE_INTERVAL = 900;
|
|
10
|
+
/** The variables the sweep reads that launchd would otherwise not pass on. */
|
|
11
|
+
const PASSED_ENV = ["LOCAL_CI_COMMAND", "LOCAL_CI_PROJECTS", "XDG_STATE_HOME"];
|
|
12
|
+
/** The agent's log is moved aside to `<log>.1` past this size, so it never grows without bound. */
|
|
13
|
+
export const LOG_MAX_BYTES = 10 * 1024 * 1024;
|
|
14
|
+
/** The agent's log, which launchd appends the sweep's output to. */
|
|
15
|
+
export const renovateLog = (env = process.env) => join(stateDir(env), "renovate.log");
|
|
16
|
+
/**
|
|
17
|
+
* Moves `file` to `<file>.1` once it passes `max` bytes. launchd opens the log for each run, so the
|
|
18
|
+
* run under way finishes in `.1` and the next starts a new file.
|
|
19
|
+
*/
|
|
20
|
+
export function rotateLog(file, max = LOG_MAX_BYTES) {
|
|
21
|
+
try {
|
|
22
|
+
if (statSync(file).size > max)
|
|
23
|
+
renameSync(file, `${file}.1`);
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
if (error.code !== "ENOENT")
|
|
27
|
+
throw error;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The CLI path to run on a schedule. Under pnpm, `cli` is the real path in the version-named
|
|
32
|
+
* `node_modules/.pnpm/<pkg>@<version>…` directory, which the next upgrade deletes, so this prefers
|
|
33
|
+
* the package's stable link (`node_modules/<pkg>/…`) in `cwd` or the store's own project.
|
|
34
|
+
*/
|
|
35
|
+
export function stableCli(cli, cwd = process.cwd()) {
|
|
36
|
+
const store = `${sep}node_modules${sep}.pnpm${sep}`;
|
|
37
|
+
const at = cli.indexOf(store);
|
|
38
|
+
if (at < 0)
|
|
39
|
+
return cli;
|
|
40
|
+
const inner = cli.indexOf(`${sep}node_modules${sep}`, at + store.length);
|
|
41
|
+
if (inner < 0)
|
|
42
|
+
return cli;
|
|
43
|
+
const rest = cli.slice(inner);
|
|
44
|
+
for (const root of [cwd, cli.slice(0, at)]) {
|
|
45
|
+
const link = join(root, rest);
|
|
46
|
+
try {
|
|
47
|
+
if (realpathSync(link) === realpathSync(cli))
|
|
48
|
+
return link;
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
// No such link here.
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return cli;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* The agent's PATH: the installing shell's, less any project's `node_modules/.bin` (which `pnpm
|
|
58
|
+
* exec` adds), with this node's directory last so node resolves even when the shell found it some
|
|
59
|
+
* other way. A directory a later upgrade removes is skipped, not fatal, since `env` searches on.
|
|
60
|
+
*/
|
|
61
|
+
export function agentPath(path, node) {
|
|
62
|
+
const dirs = path
|
|
63
|
+
.split(delimiter)
|
|
64
|
+
.filter((dir) => dir && !dir.split(sep).includes("node_modules"));
|
|
65
|
+
return [...new Set([...dirs, dirname(node)])].join(delimiter);
|
|
66
|
+
}
|
|
67
|
+
const escape = (s) => s.replace(/[&<>"']/g, (c) => `&${{ "&": "amp", "<": "lt", ">": "gt", '"': "quot", "'": "apos" }[c]};`);
|
|
68
|
+
const string = (s) => `<string>${escape(s)}</string>`;
|
|
69
|
+
/** A launchd agent plist that runs `args` at load and every `interval` seconds after. */
|
|
70
|
+
export function agentPlist({ label, args, env, workingDirectory, log, interval, }) {
|
|
71
|
+
const vars = Object.entries(env).map(([key, value]) => ` <key>${escape(key)}</key>\n ${string(value)}`);
|
|
72
|
+
return `<?xml version="1.0" encoding="UTF-8"?>
|
|
73
|
+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
74
|
+
<plist version="1.0">
|
|
75
|
+
<dict>
|
|
76
|
+
<key>Label</key>
|
|
77
|
+
${string(label)}
|
|
78
|
+
<key>ProgramArguments</key>
|
|
79
|
+
<array>
|
|
80
|
+
${args.map((arg) => ` ${string(arg)}`).join("\n")}
|
|
81
|
+
</array>
|
|
82
|
+
<key>EnvironmentVariables</key>
|
|
83
|
+
<dict>
|
|
84
|
+
${vars.join("\n")}
|
|
85
|
+
</dict>
|
|
86
|
+
<key>WorkingDirectory</key>
|
|
87
|
+
${string(workingDirectory)}
|
|
88
|
+
<key>RunAtLoad</key>
|
|
89
|
+
<true/>
|
|
90
|
+
<key>StartInterval</key>
|
|
91
|
+
<integer>${interval}</integer>
|
|
92
|
+
<key>StandardOutPath</key>
|
|
93
|
+
${string(log)}
|
|
94
|
+
<key>StandardErrorPath</key>
|
|
95
|
+
${string(log)}
|
|
96
|
+
</dict>
|
|
97
|
+
</plist>
|
|
98
|
+
`;
|
|
99
|
+
}
|
|
100
|
+
function resolve(context) {
|
|
101
|
+
const { env = process.env, home = homedir(), uid = process.getuid?.() ?? 0 } = context;
|
|
102
|
+
if ((context.platform ?? process.platform) !== "darwin") {
|
|
103
|
+
throw new Error("the renovate agent needs macOS (launchd)");
|
|
104
|
+
}
|
|
105
|
+
return {
|
|
106
|
+
env,
|
|
107
|
+
home,
|
|
108
|
+
domain: `gui/${uid}`,
|
|
109
|
+
service: `gui/${uid}/${RENOVATE_LABEL}`,
|
|
110
|
+
plist: join(home, "Library", "LaunchAgents", `${RENOVATE_LABEL}.plist`),
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
/** Whether launchd has the agent loaded. */
|
|
114
|
+
const loaded = (exec, service) => exec("launchctl", ["print", service]).then(() => true, () => false);
|
|
115
|
+
/**
|
|
116
|
+
* Unloads the agent if loaded, waiting until launchd has let it go so a bootstrap can follow, and
|
|
117
|
+
* resolves with whether it was loaded.
|
|
118
|
+
*/
|
|
119
|
+
async function bootout(context, service) {
|
|
120
|
+
const { exec, sleep = defaultSleep } = context;
|
|
121
|
+
if (!(await loaded(exec, service)))
|
|
122
|
+
return false;
|
|
123
|
+
// launchd gives a running sweep up to 20s to exit before killing it, so wait past that.
|
|
124
|
+
const failed = await exec("launchctl", ["bootout", service]).then(() => "", (error) => `: ${error.message}`);
|
|
125
|
+
for (let tries = 0; await loaded(exec, service); tries++) {
|
|
126
|
+
if (tries >= 250)
|
|
127
|
+
throw new Error(`launchctl could not unload ${service}${failed}`);
|
|
128
|
+
await sleep(100);
|
|
129
|
+
}
|
|
130
|
+
return true;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Writes the agent's plist and loads it, replacing an agent already loaded. It runs node through
|
|
134
|
+
* `env` on the installing shell's PATH (see `agentPath`), since launchd's own lacks pnpm, gh and
|
|
135
|
+
* node, and a node upgrade must not leave it pointing at a deleted binary.
|
|
136
|
+
*/
|
|
137
|
+
export async function installRenovateAgent(context) {
|
|
138
|
+
const { env, home, domain, service, plist } = resolve(context);
|
|
139
|
+
const log = renovateLog(env);
|
|
140
|
+
const vars = {
|
|
141
|
+
PATH: agentPath(env.PATH ?? "", context.node ?? process.execPath),
|
|
142
|
+
};
|
|
143
|
+
for (const key of PASSED_ENV)
|
|
144
|
+
if (env[key])
|
|
145
|
+
vars[key] = env[key];
|
|
146
|
+
// Written before the old agent goes, so a failed write leaves that one running.
|
|
147
|
+
mkdirSync(dirname(plist), { recursive: true });
|
|
148
|
+
mkdirSync(dirname(log), { recursive: true });
|
|
149
|
+
writeFileSync(plist, agentPlist({
|
|
150
|
+
label: RENOVATE_LABEL,
|
|
151
|
+
args: ["/usr/bin/env", "node", stableCli(context.cli, context.cwd), "renovate"],
|
|
152
|
+
env: vars,
|
|
153
|
+
workingDirectory: home,
|
|
154
|
+
log,
|
|
155
|
+
interval: RENOVATE_INTERVAL,
|
|
156
|
+
}));
|
|
157
|
+
await bootout(context, service);
|
|
158
|
+
// A `launchctl disable` outlives bootout and the plist, and would make bootstrap fail.
|
|
159
|
+
await context.exec("launchctl", ["enable", service]);
|
|
160
|
+
await context.exec("launchctl", ["bootstrap", domain, plist]);
|
|
161
|
+
return `installed ${RENOVATE_LABEL}: runs local-ci renovate now and every ${RENOVATE_INTERVAL / 60} minutes while the Mac is awake; log ${log}`;
|
|
162
|
+
}
|
|
163
|
+
/** Unloads the agent and deletes its plist; a no-op when it is not installed. */
|
|
164
|
+
export async function uninstallRenovateAgent(context) {
|
|
165
|
+
const { service, plist } = resolve(context);
|
|
166
|
+
const wasLoaded = await bootout(context, service);
|
|
167
|
+
if (!wasLoaded && !existsSync(plist))
|
|
168
|
+
return `${RENOVATE_LABEL} is not installed`;
|
|
169
|
+
rmSync(plist, { force: true });
|
|
170
|
+
return `uninstalled ${RENOVATE_LABEL}`;
|
|
171
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jonmilner/local-ci",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.1",
|
|
4
4
|
"description": "Run a repo's check in a pre-push hook and post the result as GitHub commit statuses",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -21,10 +21,9 @@
|
|
|
21
21
|
"access": "public"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@commitlint/config-conventional": "^20.0.0",
|
|
25
24
|
"@commitlint/lint": "^21.2.4",
|
|
26
25
|
"@commitlint/load": "^21.2.4",
|
|
27
|
-
"@jonmilner/commitlint-config": "^0.1.
|
|
26
|
+
"@jonmilner/commitlint-config": "^0.1.2"
|
|
28
27
|
},
|
|
29
28
|
"engines": {
|
|
30
29
|
"node": ">=22"
|