@jonmilner/local-ci 0.4.0 → 0.5.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 +3 -1
- package/dist/cli.js +131 -10
- package/dist/config.d.ts +5 -1
- package/dist/config.js +5 -1
- package/dist/lock.d.ts +21 -0
- package/dist/lock.js +97 -0
- package/dist/release.d.ts +0 -2
- package/dist/release.js +2 -40
- package/dist/renovate.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -50,6 +50,8 @@ A repo without a `package.json` sets `LOCAL_CI_COMMAND`, for example in the hook
|
|
|
50
50
|
|
|
51
51
|
## State
|
|
52
52
|
|
|
53
|
-
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.
|
|
53
|
+
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.
|
|
54
|
+
|
|
55
|
+
One check command runs at a time on the machine, across every repo, so overlapping checks can't starve each other's tests: `pre-push`, `status`, and the `status` runs `renovate` starts each take `check.lock` in the same directory around the check command (not the status posts). A run that finds it held prints `local-ci: waiting for the check lock, held by pid <pid> (<repo> <short-sha>)`, again whenever another run takes it first, and polls every second until the holder exits or dies; Ctrl-C stops the wait. After the wait it skips a commit that passed meanwhile and re-applies the clean-tree rule. The lock goes when the check passes, fails or is killed by a signal. Ctrl-C, SIGTERM or SIGHUP to local-ci passes the signal to the check's whole process group (so a check script's children get it too), waits for the check to exit, then releases the lock and dies of the signal, so a check never outlives its lock. A lock whose holder died (SIGKILL, a crash) or that predates the last boot is taken over by the next run. A local-ci check nested inside a check (one that pushes, say) sees its ancestor holds the lock and runs without waiting. 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 the check, which logs `did not finish after`.
|
|
54
56
|
|
|
55
57
|
Part of [jonmilner/config](https://github.com/jonmilner/config#readme).
|
package/dist/cli.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import {
|
|
2
|
+
import { execFileSync, spawn } from "node:child_process";
|
|
3
3
|
import { closeSync, openSync, readFileSync } from "node:fs";
|
|
4
4
|
import { hostname } from "node:os";
|
|
5
5
|
import { basename, dirname, resolve } from "node:path";
|
|
@@ -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 { LockHeld, takeLock, waitForLock } from "./lock.js";
|
|
10
11
|
import { cannotCheckInPlace, githubRepo, parsePrePush, shasToCheck } from "./push.js";
|
|
11
12
|
import { release } from "./release.js";
|
|
12
13
|
import { projectsDir, renovate } from "./renovate.js";
|
|
@@ -24,7 +25,7 @@ async function repo() {
|
|
|
24
25
|
// The common dir names the repo even from a linked worktree.
|
|
25
26
|
const common = resolve(root, await git("rev-parse", "--git-common-dir"));
|
|
26
27
|
const name = basename(common) === ".git" ? basename(dirname(common)) : basename(common, ".git");
|
|
27
|
-
return { root, ...statePaths(name) };
|
|
28
|
+
return { root, name, ...statePaths(name) };
|
|
28
29
|
}
|
|
29
30
|
const out = (s) => process.stdout.write(`${s}\n`);
|
|
30
31
|
const err = (s) => process.stderr.write(`${s}\n`);
|
|
@@ -37,34 +38,154 @@ class CheckFailed extends Error {
|
|
|
37
38
|
this.command = command;
|
|
38
39
|
}
|
|
39
40
|
}
|
|
40
|
-
/**
|
|
41
|
-
|
|
42
|
-
|
|
41
|
+
/** This process's ancestors' pids, or none when `ps` can't list them. */
|
|
42
|
+
function ancestors() {
|
|
43
|
+
const parents = new Map();
|
|
44
|
+
try {
|
|
45
|
+
for (const line of execFileSync("ps", ["-A", "-o", "pid=,ppid="], { encoding: "utf8" }).split("\n")) {
|
|
46
|
+
const [pid, ppid] = line.trim().split(/\s+/).map(Number);
|
|
47
|
+
if (pid && ppid !== undefined)
|
|
48
|
+
parents.set(pid, ppid);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
return new Set();
|
|
53
|
+
}
|
|
54
|
+
const found = new Set();
|
|
55
|
+
for (let pid = process.ppid; pid > 1 && !found.has(pid); pid = parents.get(pid) ?? 0)
|
|
56
|
+
found.add(pid);
|
|
57
|
+
return found;
|
|
58
|
+
}
|
|
59
|
+
const SIGNALS = ["SIGINT", "SIGTERM", "SIGHUP"];
|
|
60
|
+
/** The check lock this process holds, the check running under it, and a signal it deferred. */
|
|
61
|
+
let held;
|
|
62
|
+
/** Dies of `signal` as if local-ci had no handler for it. */
|
|
63
|
+
function reraise(signal) {
|
|
64
|
+
for (const name of SIGNALS)
|
|
65
|
+
process.off(name, onSignal);
|
|
66
|
+
process.kill(process.pid, signal);
|
|
67
|
+
// The default action has ended the process by now; this only satisfies the type.
|
|
68
|
+
process.exit(1);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* While a check runs, passes the signal on and lets the check stop first, so it never outlives
|
|
72
|
+
* the lock; its release then dies of the signal. Otherwise releases the lock and dies of it now.
|
|
73
|
+
*/
|
|
74
|
+
function onSignal(signal) {
|
|
75
|
+
if (held?.child) {
|
|
76
|
+
held.signal ??= signal;
|
|
77
|
+
// The check leads its own process group, so the signal reaches a script's children too.
|
|
78
|
+
try {
|
|
79
|
+
process.kill(-held.child.pid, signal);
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
held.child.kill(signal);
|
|
83
|
+
}
|
|
43
84
|
return;
|
|
85
|
+
}
|
|
86
|
+
held?.unlock();
|
|
87
|
+
held = undefined;
|
|
88
|
+
reraise(signal);
|
|
89
|
+
}
|
|
90
|
+
/** Holds `unlock` until the returned release, releasing it on SIGINT, SIGTERM or SIGHUP too. */
|
|
91
|
+
function releaseOnSignal(unlock) {
|
|
92
|
+
if (!process.listenerCount("SIGINT", onSignal)) {
|
|
93
|
+
for (const name of SIGNALS)
|
|
94
|
+
process.on(name, onSignal);
|
|
95
|
+
}
|
|
96
|
+
const lock = { unlock };
|
|
97
|
+
held = lock;
|
|
98
|
+
return () => {
|
|
99
|
+
if (held === lock)
|
|
100
|
+
held = undefined;
|
|
101
|
+
unlock();
|
|
102
|
+
if (lock.signal)
|
|
103
|
+
reraise(lock.signal);
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
/** Throws unless `sha` can be checked in place: it is HEAD and the tree is clean. */
|
|
107
|
+
async function assertCheckable(sha) {
|
|
44
108
|
const reason = cannotCheckInPlace(sha, await git("rev-parse", "HEAD"), await git("status", "--porcelain"));
|
|
45
109
|
if (reason)
|
|
46
110
|
throw new Error(`cannot check ${sha.slice(0, 7)}: ${reason}`);
|
|
111
|
+
}
|
|
112
|
+
/** Takes the machine-wide check lock, waiting while another repo's check holds it. */
|
|
113
|
+
async function lockCheck({ name, checkLock }, sha) {
|
|
114
|
+
const holder = `${name} ${sha.slice(0, 7)}`;
|
|
115
|
+
try {
|
|
116
|
+
return releaseOnSignal(takeLock(checkLock, "check", holder));
|
|
117
|
+
}
|
|
118
|
+
catch (error) {
|
|
119
|
+
if (!(error instanceof LockHeld))
|
|
120
|
+
throw error;
|
|
121
|
+
// A check nested in a check (one that pushes, say) runs under its ancestor's lock rather
|
|
122
|
+
// than waiting forever on it.
|
|
123
|
+
if (ancestors().has(error.pid))
|
|
124
|
+
return () => { };
|
|
125
|
+
}
|
|
126
|
+
return releaseOnSignal(await waitForLock(checkLock, {
|
|
127
|
+
what: "check",
|
|
128
|
+
holder,
|
|
129
|
+
onWait: ({ pid, holder }) => err(`local-ci: waiting for the check lock, held by pid ${pid} (${holder || "unknown"})`),
|
|
130
|
+
}));
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Runs the check at `sha` in place, after the clean-tree rule, unless it already passed. One check
|
|
134
|
+
* runs at a time across all repos, so overlapping checks can't starve each other's tests.
|
|
135
|
+
*/
|
|
136
|
+
async function runCheck(current, sha) {
|
|
137
|
+
if (passedCommand(current.passed, sha))
|
|
138
|
+
return;
|
|
139
|
+
await assertCheckable(sha);
|
|
140
|
+
const unlock = await lockCheck(current, sha);
|
|
141
|
+
try {
|
|
142
|
+
// Another run may have passed it, or the tree changed, while this one waited.
|
|
143
|
+
if (passedCommand(current.passed, sha))
|
|
144
|
+
return;
|
|
145
|
+
await assertCheckable(sha);
|
|
146
|
+
await runLocked(current, sha);
|
|
147
|
+
}
|
|
148
|
+
finally {
|
|
149
|
+
unlock();
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
/** Runs the check command under the check lock, logging its duration and recording a pass. */
|
|
153
|
+
async function runLocked({ root, passed, log: logFile }, sha) {
|
|
47
154
|
const command = resolveCommand(root);
|
|
48
155
|
err(`local-ci: running ${command} on ${sha.slice(0, 7)}`);
|
|
49
156
|
const started = performance.now();
|
|
50
|
-
const
|
|
157
|
+
const child = spawn("sh", ["-c", command], {
|
|
51
158
|
cwd: root,
|
|
159
|
+
// Its own process group, so onSignal can stop everything the check started. A nested check
|
|
160
|
+
// has no handler and stays in its parent check's group, which that check's signal reaches.
|
|
161
|
+
detached: held !== undefined,
|
|
52
162
|
stdio: ["ignore", "inherit", "inherit"],
|
|
53
163
|
});
|
|
164
|
+
if (held)
|
|
165
|
+
held.child = child;
|
|
166
|
+
const result = await new Promise((resolve) => {
|
|
167
|
+
child.on("error", (error) => resolve({ status: null, cause: error.message }));
|
|
168
|
+
child.on("close", (status, signal) => resolve({ status, cause: signal ?? undefined }));
|
|
169
|
+
});
|
|
170
|
+
if (held)
|
|
171
|
+
held.child = undefined;
|
|
172
|
+
// A check local-ci was told to stop never finished, whatever it exited with.
|
|
173
|
+
const interrupted = held?.signal;
|
|
174
|
+
const status = interrupted ? null : result.status;
|
|
54
175
|
// Every run's duration goes in the log, so a faster check can be measured against a slower one.
|
|
55
176
|
// A log that can't be written must not change the check's outcome.
|
|
56
177
|
try {
|
|
57
|
-
appendLog(logFile, checkRunMessage(sha, command,
|
|
178
|
+
appendLog(logFile, checkRunMessage(sha, command, status, performance.now() - started));
|
|
58
179
|
}
|
|
59
180
|
catch (error) {
|
|
60
181
|
err(`local-ci: could not log the run: ${error instanceof Error ? error.message : String(error)}`);
|
|
61
182
|
}
|
|
62
183
|
// A spawn error or a signal means the check never finished, so it is not a failure to post.
|
|
63
|
-
if (
|
|
64
|
-
const cause =
|
|
184
|
+
if (status === null) {
|
|
185
|
+
const cause = interrupted ?? result.cause;
|
|
65
186
|
throw new Error(`${command} did not finish on ${sha.slice(0, 7)}: ${cause}`);
|
|
66
187
|
}
|
|
67
|
-
if (
|
|
188
|
+
if (status !== 0)
|
|
68
189
|
throw new CheckFailed(command, sha);
|
|
69
190
|
recordPass(passed, sha, command);
|
|
70
191
|
}
|
package/dist/config.d.ts
CHANGED
|
@@ -4,11 +4,15 @@ export declare function resolveCommand(root: string, env?: NodeJS.ProcessEnv): s
|
|
|
4
4
|
/** Whether `local-ci release` is switched on, with `"local-ci": { "release": true }` in package.json. */
|
|
5
5
|
export declare function releaseEnabled(root: string): boolean;
|
|
6
6
|
export declare function stateDir(env?: NodeJS.ProcessEnv): string;
|
|
7
|
-
/**
|
|
7
|
+
/**
|
|
8
|
+
* Where a repo's log, record of passed commits and release lock live, and the check lock all repos
|
|
9
|
+
* share, created on first use.
|
|
10
|
+
*/
|
|
8
11
|
export declare function statePaths(repo: string, env?: NodeJS.ProcessEnv): {
|
|
9
12
|
log: string;
|
|
10
13
|
passed: string;
|
|
11
14
|
lock: string;
|
|
15
|
+
checkLock: string;
|
|
12
16
|
};
|
|
13
17
|
/** A line of the repo's log: the message after an ISO timestamp. */
|
|
14
18
|
export declare function logLine(message: string, now?: Date): string;
|
package/dist/config.js
CHANGED
|
@@ -24,7 +24,10 @@ export function releaseEnabled(root) {
|
|
|
24
24
|
export function stateDir(env = process.env) {
|
|
25
25
|
return join(env.XDG_STATE_HOME || join(homedir(), ".local", "state"), "local-ci");
|
|
26
26
|
}
|
|
27
|
-
/**
|
|
27
|
+
/**
|
|
28
|
+
* Where a repo's log, record of passed commits and release lock live, and the check lock all repos
|
|
29
|
+
* share, created on first use.
|
|
30
|
+
*/
|
|
28
31
|
export function statePaths(repo, env = process.env) {
|
|
29
32
|
const dir = stateDir(env);
|
|
30
33
|
mkdirSync(dir, { recursive: true });
|
|
@@ -32,6 +35,7 @@ export function statePaths(repo, env = process.env) {
|
|
|
32
35
|
log: join(dir, `${repo}.log`),
|
|
33
36
|
passed: join(dir, `${repo}.passed`),
|
|
34
37
|
lock: join(dir, `${repo}.release.lock`),
|
|
38
|
+
checkLock: join(dir, "check.lock"),
|
|
35
39
|
};
|
|
36
40
|
}
|
|
37
41
|
/** A line of the repo's log: the message after an ISO timestamp. */
|
package/dist/lock.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** A live process holds the lock. `holder` is what it wrote after its pid, such as its repo. */
|
|
2
|
+
export declare class LockHeld extends Error {
|
|
3
|
+
readonly pid: number;
|
|
4
|
+
readonly holder: string;
|
|
5
|
+
constructor(what: string, file: string, pid: number, holder: string);
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Takes the lock and returns its release, or throws `LockHeld` while a live process holds it. The
|
|
9
|
+
* lock file holds the pid, then `holder` on a second line when given.
|
|
10
|
+
*/
|
|
11
|
+
export declare function takeLock(file: string, what?: string, holder?: string): () => void;
|
|
12
|
+
export interface WaitForLockOptions {
|
|
13
|
+
what: string;
|
|
14
|
+
holder?: string;
|
|
15
|
+
/** Called when the wait starts, and again whenever another process takes the lock first. */
|
|
16
|
+
onWait?: (held: LockHeld) => void;
|
|
17
|
+
intervalMs?: number;
|
|
18
|
+
sleep?: (ms: number) => Promise<void>;
|
|
19
|
+
}
|
|
20
|
+
/** Takes the lock, polling while a live process holds it, and returns its release. */
|
|
21
|
+
export declare function waitForLock(file: string, { what, holder, onWait, intervalMs, sleep }: WaitForLockOptions): Promise<() => void>;
|
package/dist/lock.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { linkSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { uptime } from "node:os";
|
|
3
|
+
import { defaultSleep } from "./github.js";
|
|
4
|
+
/** Whether a process with this pid is running. */
|
|
5
|
+
function alive(pid) {
|
|
6
|
+
if (!Number.isInteger(pid) || pid <= 0)
|
|
7
|
+
return false;
|
|
8
|
+
try {
|
|
9
|
+
process.kill(pid, 0);
|
|
10
|
+
return true;
|
|
11
|
+
}
|
|
12
|
+
catch (error) {
|
|
13
|
+
// EPERM: the process exists but belongs to another user.
|
|
14
|
+
return error.code === "EPERM";
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Whether the lock file was written before the machine booted, so its pid, alive or not, belongs to
|
|
19
|
+
* a process that never held it.
|
|
20
|
+
*/
|
|
21
|
+
function beforeBoot(file) {
|
|
22
|
+
try {
|
|
23
|
+
return statSync(file).mtimeMs < Date.now() - uptime() * 1_000;
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/** A live process holds the lock. `holder` is what it wrote after its pid, such as its repo. */
|
|
30
|
+
export class LockHeld extends Error {
|
|
31
|
+
pid;
|
|
32
|
+
holder;
|
|
33
|
+
constructor(what, file, pid, holder) {
|
|
34
|
+
super(`${what} in progress (pid ${pid}, lock ${file})`);
|
|
35
|
+
this.pid = pid;
|
|
36
|
+
this.holder = holder;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Takes the lock and returns its release, or throws `LockHeld` while a live process holds it. The
|
|
41
|
+
* lock file holds the pid, then `holder` on a second line when given.
|
|
42
|
+
*/
|
|
43
|
+
export function takeLock(file, what = "release", holder) {
|
|
44
|
+
// Linking a file that already holds the pid publishes the lock atomically, so nobody reads it empty.
|
|
45
|
+
const mine = `${file}.${process.pid}`;
|
|
46
|
+
writeFileSync(mine, holder ? `${process.pid}\n${holder}` : String(process.pid));
|
|
47
|
+
try {
|
|
48
|
+
// Each pass takes the lock, finds a live holder, or clears a released or dead one.
|
|
49
|
+
for (;;) {
|
|
50
|
+
try {
|
|
51
|
+
linkSync(mine, file);
|
|
52
|
+
return () => rmSync(file, { force: true });
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
if (error.code !== "EEXIST")
|
|
56
|
+
throw error;
|
|
57
|
+
let content;
|
|
58
|
+
try {
|
|
59
|
+
content = readFileSync(file, "utf8");
|
|
60
|
+
}
|
|
61
|
+
catch (readError) {
|
|
62
|
+
// Released between the link and the read: try again.
|
|
63
|
+
if (readError.code === "ENOENT")
|
|
64
|
+
continue;
|
|
65
|
+
throw readError;
|
|
66
|
+
}
|
|
67
|
+
const [pid = "", ...rest] = content.split("\n");
|
|
68
|
+
if (alive(Number(pid)) && !beforeBoot(file)) {
|
|
69
|
+
throw new LockHeld(what, file, Number(pid), rest.join("\n"));
|
|
70
|
+
}
|
|
71
|
+
// The holder died without releasing it, or its pid is another process's since a reboot.
|
|
72
|
+
rmSync(file, { force: true });
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
finally {
|
|
77
|
+
rmSync(mine, { force: true });
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
/** Takes the lock, polling while a live process holds it, and returns its release. */
|
|
81
|
+
export async function waitForLock(file, { what, holder, onWait, intervalMs = 1_000, sleep = defaultSleep }) {
|
|
82
|
+
let last;
|
|
83
|
+
for (;;) {
|
|
84
|
+
try {
|
|
85
|
+
return takeLock(file, what, holder);
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
if (!(error instanceof LockHeld))
|
|
89
|
+
throw error;
|
|
90
|
+
const seen = `${error.pid}\n${error.holder}`;
|
|
91
|
+
if (seen !== last)
|
|
92
|
+
onWait?.(error);
|
|
93
|
+
last = seen;
|
|
94
|
+
await sleep(intervalMs);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
package/dist/release.d.ts
CHANGED
|
@@ -8,8 +8,6 @@ export interface Package {
|
|
|
8
8
|
name: string;
|
|
9
9
|
version: string;
|
|
10
10
|
}
|
|
11
|
-
/** Takes the lock and returns its release, or throws `<what> in progress` while a live process holds it. */
|
|
12
|
-
export declare function takeLock(file: string, what?: string): () => void;
|
|
13
11
|
/** The changeset files waiting in `dir`'s `.changeset` folder. */
|
|
14
12
|
export declare function pendingChangesets(dir: string): string[];
|
|
15
13
|
/** How `dir` versions its changesets: its root `version-packages` script, else `changeset version`. */
|
package/dist/release.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { spawn } from "node:child_process";
|
|
2
|
-
import { existsSync,
|
|
2
|
+
import { existsSync, mkdtempSync, readdirSync, readFileSync, rmSync } from "node:fs";
|
|
3
3
|
import { constants, tmpdir } from "node:os";
|
|
4
4
|
import { basename, dirname, join } from "node:path";
|
|
5
5
|
import { run } from "./exec.js";
|
|
6
6
|
import { defaultSleep, waitForStatuses } from "./github.js";
|
|
7
|
+
import { takeLock } from "./lock.js";
|
|
7
8
|
/** The macOS Keychain generic password that holds the npm token. */
|
|
8
9
|
export const TOKEN_SERVICE = "local-ci-npm";
|
|
9
10
|
/** npm and pnpm read a registry token from this variable; it is set only for `pnpm release`. */
|
|
@@ -20,45 +21,6 @@ const AUTH_ERROR = /\b(?:E401|E403|EOTP|ERR_PNPM_OTP_NON_INTERACTIVE|401 Unautho
|
|
|
20
21
|
// What npm prints when the version is already on the registry. It comes with an E403, so it is checked before AUTH_ERROR.
|
|
21
22
|
const PUBLISH_OVER = /cannot publish over the previously published version/;
|
|
22
23
|
const id = ({ name, version }) => `${name}@${version}`;
|
|
23
|
-
function alive(pid) {
|
|
24
|
-
if (!Number.isInteger(pid) || pid <= 0)
|
|
25
|
-
return false;
|
|
26
|
-
try {
|
|
27
|
-
process.kill(pid, 0);
|
|
28
|
-
return true;
|
|
29
|
-
}
|
|
30
|
-
catch (error) {
|
|
31
|
-
// EPERM: the process exists but belongs to another user.
|
|
32
|
-
return error.code === "EPERM";
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
/** Takes the lock and returns its release, or throws `<what> in progress` while a live process holds it. */
|
|
36
|
-
export function takeLock(file, what = "release") {
|
|
37
|
-
// Linking a file that already holds the pid publishes the lock atomically, so nobody reads it empty.
|
|
38
|
-
const mine = `${file}.${process.pid}`;
|
|
39
|
-
writeFileSync(mine, String(process.pid));
|
|
40
|
-
try {
|
|
41
|
-
for (let attempt = 0; attempt < 2; attempt++) {
|
|
42
|
-
try {
|
|
43
|
-
linkSync(mine, file);
|
|
44
|
-
return () => rmSync(file, { force: true });
|
|
45
|
-
}
|
|
46
|
-
catch (error) {
|
|
47
|
-
if (error.code !== "EEXIST")
|
|
48
|
-
throw error;
|
|
49
|
-
const pid = Number(readFileSync(file, "utf8"));
|
|
50
|
-
if (alive(pid))
|
|
51
|
-
throw new Error(`${what} in progress (pid ${pid}, lock ${file})`);
|
|
52
|
-
// The holder died without releasing it.
|
|
53
|
-
rmSync(file, { force: true });
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
finally {
|
|
58
|
-
rmSync(mine, { force: true });
|
|
59
|
-
}
|
|
60
|
-
throw new Error(`could not take the ${what} lock ${file}`);
|
|
61
|
-
}
|
|
62
24
|
/** The changeset files waiting in `dir`'s `.changeset` folder. */
|
|
63
25
|
export function pendingChangesets(dir) {
|
|
64
26
|
const folder = join(dir, ".changeset");
|
package/dist/renovate.js
CHANGED
|
@@ -3,8 +3,8 @@ import { homedir, hostname, tmpdir } from "node:os";
|
|
|
3
3
|
import { basename, dirname, join } from "node:path";
|
|
4
4
|
import { passedCommand, stateDir, statePaths } from "./config.js";
|
|
5
5
|
import { checkStatus, commitStatuses, postStatus } from "./github.js";
|
|
6
|
+
import { takeLock } from "./lock.js";
|
|
6
7
|
import { githubRepo } from "./push.js";
|
|
7
|
-
import { takeLock } from "./release.js";
|
|
8
8
|
/** The login `gh` gives the Renovate GitHub app. */
|
|
9
9
|
export const RENOVATE_AUTHOR = "app/renovate";
|
|
10
10
|
export const RENOVATE_BRANCH = "renovate/";
|