@codyswann/lisa 6.3.3 → 6.3.5
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/dist/controls/carrier-agent-script.d.ts +9 -0
- package/dist/controls/carrier-agent-script.d.ts.map +1 -1
- package/dist/controls/carrier-agent-script.js +38 -6
- package/dist/controls/carrier-agent-script.js.map +1 -1
- package/dist/controls/carrier-agents.d.ts.map +1 -1
- package/dist/controls/carrier-agents.js +2 -2
- package/dist/controls/carrier-agents.js.map +1 -1
- package/dist/core/nightly-e2e-guard-behavior-certificate.js +2 -2
- package/dist/core/upstream-evidence-manifest.d.ts.map +1 -1
- package/dist/core/upstream-evidence-manifest.js +19 -3
- package/dist/core/upstream-evidence-manifest.js.map +1 -1
- package/package.json +4 -4
- package/plugins/lisa/.claude-plugin/plugin.json +1 -1
- package/plugins/lisa/.codex-plugin/plugin.json +1 -1
- package/plugins/lisa/.codex-plugin/skills/lisa-parity-coderabbit/SKILL.md +6 -3
- package/plugins/lisa/skills/lisa-parity-coderabbit/SKILL.md +6 -3
- package/plugins/lisa-agy/plugin.json +1 -1
- package/plugins/lisa-agy/skills/lisa-parity-coderabbit/SKILL.md +6 -3
- package/plugins/lisa-cdk/.claude-plugin/plugin.json +1 -1
- package/plugins/lisa-cdk/.codex-plugin/plugin.json +1 -1
- package/plugins/lisa-cdk-agy/plugin.json +1 -1
- package/plugins/lisa-cdk-copilot/.claude-plugin/plugin.json +1 -1
- package/plugins/lisa-cdk-cursor/.claude-plugin/plugin.json +1 -1
- package/plugins/lisa-cdk-devin/.claude-plugin/plugin.json +1 -1
- package/plugins/lisa-cdk-devin/.devin-plugin/plugin.json +1 -1
- package/plugins/lisa-copilot/.claude-plugin/plugin.json +1 -1
- package/plugins/lisa-copilot/skills/lisa-parity-coderabbit/SKILL.md +6 -3
- package/plugins/lisa-cursor/.claude-plugin/plugin.json +1 -1
- package/plugins/lisa-cursor/skills/lisa-parity-coderabbit/SKILL.md +6 -3
- package/plugins/lisa-devin/.claude-plugin/plugin.json +1 -1
- package/plugins/lisa-devin/.devin-plugin/plugin.json +1 -1
- package/plugins/lisa-devin/skills/lisa-parity-coderabbit/SKILL.md +6 -3
- package/plugins/lisa-expo/.claude-plugin/plugin.json +1 -1
- package/plugins/lisa-expo/.codex-plugin/plugin.json +1 -1
- package/plugins/lisa-expo-agy/plugin.json +1 -1
- package/plugins/lisa-expo-copilot/.claude-plugin/plugin.json +1 -1
- package/plugins/lisa-expo-cursor/.claude-plugin/plugin.json +1 -1
- package/plugins/lisa-expo-devin/.claude-plugin/plugin.json +1 -1
- package/plugins/lisa-expo-devin/.devin-plugin/plugin.json +1 -1
- package/plugins/lisa-harper-fabric/.claude-plugin/plugin.json +1 -1
- package/plugins/lisa-harper-fabric/.codex-plugin/plugin.json +1 -1
- package/plugins/lisa-harper-fabric-agy/plugin.json +1 -1
- package/plugins/lisa-harper-fabric-copilot/.claude-plugin/plugin.json +1 -1
- package/plugins/lisa-harper-fabric-cursor/.claude-plugin/plugin.json +1 -1
- package/plugins/lisa-harper-fabric-devin/.claude-plugin/plugin.json +1 -1
- package/plugins/lisa-harper-fabric-devin/.devin-plugin/plugin.json +1 -1
- package/plugins/lisa-nestjs/.claude-plugin/plugin.json +1 -1
- package/plugins/lisa-nestjs/.codex-plugin/plugin.json +1 -1
- package/plugins/lisa-nestjs-agy/plugin.json +1 -1
- package/plugins/lisa-nestjs-copilot/.claude-plugin/plugin.json +1 -1
- package/plugins/lisa-nestjs-cursor/.claude-plugin/plugin.json +1 -1
- package/plugins/lisa-nestjs-devin/.claude-plugin/plugin.json +1 -1
- package/plugins/lisa-nestjs-devin/.devin-plugin/plugin.json +1 -1
- package/plugins/lisa-phaser/.claude-plugin/plugin.json +1 -1
- package/plugins/lisa-phaser/.codex-plugin/plugin.json +1 -1
- package/plugins/lisa-phaser-agy/plugin.json +1 -1
- package/plugins/lisa-phaser-copilot/.claude-plugin/plugin.json +1 -1
- package/plugins/lisa-phaser-cursor/.claude-plugin/plugin.json +1 -1
- package/plugins/lisa-phaser-devin/.claude-plugin/plugin.json +1 -1
- package/plugins/lisa-phaser-devin/.devin-plugin/plugin.json +1 -1
- package/plugins/lisa-rails/.claude-plugin/plugin.json +1 -1
- package/plugins/lisa-rails/.codex-plugin/plugin.json +1 -1
- package/plugins/lisa-rails-agy/plugin.json +1 -1
- package/plugins/lisa-rails-copilot/.claude-plugin/plugin.json +1 -1
- package/plugins/lisa-rails-cursor/.claude-plugin/plugin.json +1 -1
- package/plugins/lisa-rails-devin/.claude-plugin/plugin.json +1 -1
- package/plugins/lisa-rails-devin/.devin-plugin/plugin.json +1 -1
- package/plugins/lisa-typescript/.claude-plugin/plugin.json +1 -1
- package/plugins/lisa-typescript/.codex-plugin/plugin.json +1 -1
- package/plugins/lisa-typescript-agy/plugin.json +1 -1
- package/plugins/lisa-typescript-copilot/.claude-plugin/plugin.json +1 -1
- package/plugins/lisa-typescript-cursor/.claude-plugin/plugin.json +1 -1
- package/plugins/lisa-typescript-devin/.claude-plugin/plugin.json +1 -1
- package/plugins/lisa-typescript-devin/.devin-plugin/plugin.json +1 -1
- package/plugins/lisa-wiki/.claude-plugin/plugin.json +1 -1
- package/plugins/lisa-wiki/.codex-plugin/plugin.json +1 -1
- package/plugins/lisa-wiki-agy/plugin.json +1 -1
- package/plugins/lisa-wiki-copilot/.claude-plugin/plugin.json +1 -1
- package/plugins/lisa-wiki-cursor/.claude-plugin/plugin.json +1 -1
- package/plugins/lisa-wiki-devin/.claude-plugin/plugin.json +1 -1
- package/plugins/lisa-wiki-devin/.devin-plugin/plugin.json +1 -1
- package/plugins/src/base/skills/lisa-parity-coderabbit/SKILL.md +6 -3
- package/scripts/build-plugins.sh +76 -17
- package/scripts/check-plugins-sync.sh +4 -3
- package/scripts/lib/publish-plugin-tree.d.mts +33 -0
- package/scripts/lib/publish-plugin-tree.mjs +403 -0
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Publish a fully built plugin tree over the live one without the live tree
|
|
4
|
+
* ever being partly missing (CodySwannGT/lisa#4583).
|
|
5
|
+
*
|
|
6
|
+
* Running sessions read hooks straight out of the live plugin directories: the
|
|
7
|
+
* enforcement dispatcher resolves its guards from `plugins/lisa/hooks/`, and
|
|
8
|
+
* refuses every tool call when it finds none. The builder used to
|
|
9
|
+
* `rm -rf plugins/lisa` and copy the source back in, which left the guards
|
|
10
|
+
* absent for a couple of hundred milliseconds on every rebuild, and every
|
|
11
|
+
* session on that checkout was refused in the meantime.
|
|
12
|
+
*
|
|
13
|
+
* So the builder now writes the complete tree into a staging sibling on the
|
|
14
|
+
* same filesystem, and this helper moves it into place:
|
|
15
|
+
*
|
|
16
|
+
* - a live tree that does not exist yet is created by one directory rename;
|
|
17
|
+
* - otherwise every staged file is renamed over its live counterpart. A rename
|
|
18
|
+
* onto an existing file replaces it atomically, so a reader sees the old
|
|
19
|
+
* bytes or the new bytes and never an absent file. Files whose bytes and mode
|
|
20
|
+
* are already identical are left untouched;
|
|
21
|
+
* - only after every new file is in place are live entries the staged tree no
|
|
22
|
+
* longer contains removed, so retired files go last, not first.
|
|
23
|
+
*
|
|
24
|
+
* A whole-directory swap (rename live away, rename staged in) is not used: the
|
|
25
|
+
* path is absent between the two renames, which is the defect again in a
|
|
26
|
+
* smaller window, and the exchanging rename that would close it is not portable.
|
|
27
|
+
*
|
|
28
|
+
* Usage: node scripts/lib/publish-plugin-tree.mjs <staged-dir> <live-dir> [...]
|
|
29
|
+
*
|
|
30
|
+
* Any number of pairs, so one build publishes every tree from one process.
|
|
31
|
+
*/
|
|
32
|
+
import fs from "node:fs";
|
|
33
|
+
import path from "node:path";
|
|
34
|
+
|
|
35
|
+
import { invokedAsScript } from "./invoked-as-script.mjs";
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Whether two regular files already hold the same bytes and permission bits.
|
|
39
|
+
* @param {string} staged - path of the newly built file
|
|
40
|
+
* @param {fs.Stats} stagedStat - its stat
|
|
41
|
+
* @param {string} live - path of the file currently published
|
|
42
|
+
* @returns {boolean} true when replacing it would change nothing
|
|
43
|
+
*/
|
|
44
|
+
function sameFile(staged, stagedStat, live) {
|
|
45
|
+
const liveStat = fs.lstatSync(live, { throwIfNoEntry: false });
|
|
46
|
+
if (!liveStat?.isFile()) return false;
|
|
47
|
+
if (liveStat.size !== stagedStat.size) return false;
|
|
48
|
+
if ((liveStat.mode & 0o7777) !== (stagedStat.mode & 0o7777)) return false;
|
|
49
|
+
return fs.readFileSync(staged).equals(fs.readFileSync(live));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Move every staged entry into the live tree, newest content first.
|
|
54
|
+
* @param {string} stagedDir - directory holding the complete new tree
|
|
55
|
+
* @param {string} liveDir - directory sessions are reading
|
|
56
|
+
* @returns {void}
|
|
57
|
+
*/
|
|
58
|
+
function placeEntries(stagedDir, liveDir) {
|
|
59
|
+
const liveStat = fs.lstatSync(liveDir, { throwIfNoEntry: false });
|
|
60
|
+
if (liveStat && !liveStat.isDirectory()) {
|
|
61
|
+
fs.rmSync(liveDir, { force: true, recursive: true });
|
|
62
|
+
}
|
|
63
|
+
fs.mkdirSync(liveDir, { recursive: true });
|
|
64
|
+
for (const entry of fs.readdirSync(stagedDir, { withFileTypes: true })) {
|
|
65
|
+
const staged = path.join(stagedDir, entry.name);
|
|
66
|
+
const live = path.join(liveDir, entry.name);
|
|
67
|
+
if (entry.isDirectory()) {
|
|
68
|
+
placeEntries(staged, live);
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
const stagedStat = fs.lstatSync(staged);
|
|
72
|
+
if (sameFile(staged, stagedStat, live)) continue;
|
|
73
|
+
if (fs.lstatSync(live, { throwIfNoEntry: false })?.isDirectory()) {
|
|
74
|
+
fs.rmSync(live, { force: true, recursive: true });
|
|
75
|
+
}
|
|
76
|
+
fs.renameSync(staged, live);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Every path the staged tree holds, relative to its root, taken before any of
|
|
82
|
+
* it is moved: placing renames staged files away, so the staged tree cannot be
|
|
83
|
+
* consulted afterwards to decide what is retired.
|
|
84
|
+
* @param {string} dir - directory to list
|
|
85
|
+
* @param {string} [prefix] - relative path of `dir` within the staged root
|
|
86
|
+
* @returns {Map<string, boolean>} relative path to whether it is a directory
|
|
87
|
+
*/
|
|
88
|
+
function listTree(dir, prefix = "") {
|
|
89
|
+
const listed = new Map();
|
|
90
|
+
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
91
|
+
const relative = path.join(prefix, entry.name);
|
|
92
|
+
listed.set(relative, entry.isDirectory());
|
|
93
|
+
if (entry.isDirectory()) {
|
|
94
|
+
for (const [inner, isDir] of listTree(
|
|
95
|
+
path.join(dir, entry.name),
|
|
96
|
+
relative
|
|
97
|
+
)) {
|
|
98
|
+
listed.set(inner, isDir);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return listed;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Remove live entries the staged tree did not contain.
|
|
107
|
+
* @param {Map<string, boolean>} staged - the staged listing from `listTree`
|
|
108
|
+
* @param {string} liveDir - directory sessions are reading
|
|
109
|
+
* @param {string} [prefix] - relative path of `liveDir` within the live root
|
|
110
|
+
* @returns {void}
|
|
111
|
+
*/
|
|
112
|
+
function removeRetired(staged, liveDir, prefix = "") {
|
|
113
|
+
for (const entry of fs.readdirSync(liveDir, { withFileTypes: true })) {
|
|
114
|
+
const relative = path.join(prefix, entry.name);
|
|
115
|
+
const live = path.join(liveDir, entry.name);
|
|
116
|
+
if (!staged.has(relative)) {
|
|
117
|
+
fs.rmSync(live, { force: true, recursive: true });
|
|
118
|
+
} else if (entry.isDirectory() && staged.get(relative)) {
|
|
119
|
+
removeRetired(staged, live, relative);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Publish a staged plugin tree over the live one. The staged tree is consumed.
|
|
126
|
+
* @param {string} stagedDir - directory holding the complete new tree
|
|
127
|
+
* @param {string} liveDir - directory sessions are reading
|
|
128
|
+
* @returns {void}
|
|
129
|
+
*/
|
|
130
|
+
export function publishPluginTree(stagedDir, liveDir) {
|
|
131
|
+
if (!fs.lstatSync(stagedDir, { throwIfNoEntry: false })?.isDirectory()) {
|
|
132
|
+
throw new Error(`publish-plugin-tree: no staged tree at ${stagedDir}`);
|
|
133
|
+
}
|
|
134
|
+
if (!fs.lstatSync(liveDir, { throwIfNoEntry: false })) {
|
|
135
|
+
fs.mkdirSync(path.dirname(liveDir), { recursive: true });
|
|
136
|
+
fs.renameSync(stagedDir, liveDir);
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
const staged = listTree(stagedDir);
|
|
140
|
+
placeEntries(stagedDir, liveDir);
|
|
141
|
+
removeRetired(staged, liveDir);
|
|
142
|
+
fs.rmSync(stagedDir, { force: true, recursive: true });
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** Prefix of a build's staging sibling; the suffix is the owning build's pid. */
|
|
146
|
+
const STAGE_PREFIX = ".build-staging-";
|
|
147
|
+
/** The checkout-wide publish lock, a sibling of the live trees. */
|
|
148
|
+
const LOCK_NAME = ".publish.lock";
|
|
149
|
+
/** How long a publish waits for another build's publish before giving up. */
|
|
150
|
+
const DEFAULT_LOCK_WAIT_MS = 300_000;
|
|
151
|
+
/** How often a waiting publish looks at the lock again. */
|
|
152
|
+
const LOCK_POLL_MS = 50;
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Whether the process that owns a staging area or a lock is confirmed gone.
|
|
156
|
+
*
|
|
157
|
+
* Only `ESRCH` confirms that: `EPERM` means the process exists and belongs to
|
|
158
|
+
* another user, and anything else is not knowledge either way. Anything short
|
|
159
|
+
* of confirmation keeps the owner's files, because removing a running build's
|
|
160
|
+
* files breaks that build.
|
|
161
|
+
* @param {number} pid - owner process id
|
|
162
|
+
* @param {(pid: number, signal: number) => unknown} [probe] - `process.kill`
|
|
163
|
+
* @returns {boolean} true only when the owner does not exist
|
|
164
|
+
*/
|
|
165
|
+
export function ownerGone(pid, probe = process.kill) {
|
|
166
|
+
if (!Number.isSafeInteger(pid) || pid <= 0) return false;
|
|
167
|
+
try {
|
|
168
|
+
probe(pid, 0);
|
|
169
|
+
return false;
|
|
170
|
+
} catch (error) {
|
|
171
|
+
return /** @type {NodeJS.ErrnoException} */ (error).code === "ESRCH";
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Remove the staging areas of builds that were killed before their exit trap
|
|
177
|
+
* ran, and nothing whose owner might still be running.
|
|
178
|
+
* @param {string} pluginsDir - directory holding the live trees
|
|
179
|
+
* @param {(pid: number, signal: number) => unknown} [probe] - `process.kill`
|
|
180
|
+
* @returns {readonly string[]} names of the staging areas removed
|
|
181
|
+
*/
|
|
182
|
+
export function clearStaleStages(pluginsDir, probe = process.kill) {
|
|
183
|
+
const removed = [];
|
|
184
|
+
for (const entry of fs.readdirSync(pluginsDir, { withFileTypes: true })) {
|
|
185
|
+
if (!entry.isDirectory() || !entry.name.startsWith(STAGE_PREFIX)) continue;
|
|
186
|
+
const suffix = entry.name.slice(STAGE_PREFIX.length);
|
|
187
|
+
if (!/^\d+$/.test(suffix) || !ownerGone(Number(suffix), probe)) continue;
|
|
188
|
+
fs.rmSync(path.join(pluginsDir, entry.name), {
|
|
189
|
+
force: true,
|
|
190
|
+
recursive: true,
|
|
191
|
+
});
|
|
192
|
+
removed.push(entry.name);
|
|
193
|
+
}
|
|
194
|
+
return removed;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Block the calling thread without spinning.
|
|
199
|
+
* @param {number} ms - milliseconds to wait
|
|
200
|
+
* @returns {void}
|
|
201
|
+
*/
|
|
202
|
+
function sleepSync(ms) {
|
|
203
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* The pid recorded in a lock file, or null when it cannot be read.
|
|
208
|
+
* @param {string} file - lock file path
|
|
209
|
+
* @returns {number | null} the owner pid
|
|
210
|
+
*/
|
|
211
|
+
function lockOwner(file) {
|
|
212
|
+
try {
|
|
213
|
+
const pid = Number(fs.readFileSync(file, "utf8").trim());
|
|
214
|
+
return Number.isSafeInteger(pid) ? pid : null;
|
|
215
|
+
} catch {
|
|
216
|
+
return null;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Take the checkout-wide publish lock, clearing one left by a dead build.
|
|
222
|
+
*
|
|
223
|
+
* The lock file is complete before it exists under its real name: the pid is
|
|
224
|
+
* written to a private file and hard-linked into place, and the link fails if
|
|
225
|
+
* a lock is already there. So no reader ever sees an empty lock and mistakes
|
|
226
|
+
* it for a stale one.
|
|
227
|
+
* @param {string} lockFile - lock file path
|
|
228
|
+
* @param {number} waitMs - how long to wait for another holder
|
|
229
|
+
* @param {(pid: number, signal: number) => unknown} probe - `process.kill`
|
|
230
|
+
* @returns {void}
|
|
231
|
+
*/
|
|
232
|
+
function acquireLock(lockFile, waitMs, probe) {
|
|
233
|
+
const mine = `${lockFile}.${process.pid}`;
|
|
234
|
+
fs.writeFileSync(mine, `${process.pid}\n`);
|
|
235
|
+
const deadline = Date.now() + waitMs;
|
|
236
|
+
try {
|
|
237
|
+
for (;;) {
|
|
238
|
+
try {
|
|
239
|
+
fs.linkSync(mine, lockFile);
|
|
240
|
+
return;
|
|
241
|
+
} catch (error) {
|
|
242
|
+
if (/** @type {NodeJS.ErrnoException} */ (error).code !== "EEXIST") {
|
|
243
|
+
throw error;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
const owner = lockOwner(lockFile);
|
|
247
|
+
if (owner !== null && ownerGone(owner, probe)) {
|
|
248
|
+
clearDeadLock(lockFile, owner);
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
if (Date.now() >= deadline) {
|
|
252
|
+
throw new Error(
|
|
253
|
+
`publish-plugin-tree: another build (pid ${owner ?? "unknown"}) has held ${lockFile} for over ${waitMs} ms`
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
sleepSync(LOCK_POLL_MS);
|
|
257
|
+
}
|
|
258
|
+
} finally {
|
|
259
|
+
fs.rmSync(mine, { force: true });
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Remove a lock whose owner is gone, without removing one a live build has
|
|
265
|
+
* taken since. The lock is first moved to a private name, so two builds
|
|
266
|
+
* clearing the same dead lock cannot both act on it; if what was moved turns
|
|
267
|
+
* out to belong to someone else, it is linked back.
|
|
268
|
+
* @param {string} lockFile - lock file path
|
|
269
|
+
* @param {number} deadOwner - the pid confirmed gone
|
|
270
|
+
* @returns {void}
|
|
271
|
+
*/
|
|
272
|
+
function clearDeadLock(lockFile, deadOwner) {
|
|
273
|
+
const claimed = `${lockFile}.stale.${process.pid}`;
|
|
274
|
+
try {
|
|
275
|
+
fs.renameSync(lockFile, claimed);
|
|
276
|
+
} catch {
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
if (lockOwner(claimed) !== deadOwner) {
|
|
280
|
+
try {
|
|
281
|
+
fs.linkSync(claimed, lockFile);
|
|
282
|
+
} catch {
|
|
283
|
+
// A new holder already exists; the moved lock is not needed.
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
fs.rmSync(claimed, { force: true });
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Run `work` while holding the checkout-wide publish lock.
|
|
291
|
+
*
|
|
292
|
+
* Two builds in one checkout must not publish at once: one build's retirement
|
|
293
|
+
* pass, decided from its own listing, would delete a file the other had just
|
|
294
|
+
* placed, leaving a changed file whose new import is missing. The lock covers
|
|
295
|
+
* listing, placement and retirement together, and is released on every exit
|
|
296
|
+
* path, including a thrown error and SIGINT or SIGTERM.
|
|
297
|
+
* @template T
|
|
298
|
+
* @param {string} pluginsDir - directory holding the live trees
|
|
299
|
+
* @param {() => T} work - the publication to perform
|
|
300
|
+
* @param {{ waitMs?: number, probe?: (pid: number, signal: number) => unknown }} [options] - lock wait and liveness probe
|
|
301
|
+
* @returns {T} what `work` returned
|
|
302
|
+
*/
|
|
303
|
+
export function withPublishLock(pluginsDir, work, options = {}) {
|
|
304
|
+
const lockFile = path.join(pluginsDir, LOCK_NAME);
|
|
305
|
+
const waitMs = options.waitMs ?? DEFAULT_LOCK_WAIT_MS;
|
|
306
|
+
fs.mkdirSync(pluginsDir, { recursive: true });
|
|
307
|
+
acquireLock(lockFile, waitMs, options.probe ?? process.kill);
|
|
308
|
+
const release = () => {
|
|
309
|
+
if (lockOwner(lockFile) === process.pid)
|
|
310
|
+
fs.rmSync(lockFile, { force: true });
|
|
311
|
+
};
|
|
312
|
+
const onSignal = (/** @type {NodeJS.Signals} */ signal) => {
|
|
313
|
+
release();
|
|
314
|
+
process.exit(signal === "SIGINT" ? 130 : 143);
|
|
315
|
+
};
|
|
316
|
+
process.once("SIGINT", onSignal);
|
|
317
|
+
process.once("SIGTERM", onSignal);
|
|
318
|
+
try {
|
|
319
|
+
return work();
|
|
320
|
+
} finally {
|
|
321
|
+
process.removeListener("SIGINT", onSignal);
|
|
322
|
+
process.removeListener("SIGTERM", onSignal);
|
|
323
|
+
release();
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* Parse the command line: staged/live pairs, `--retire <live>` entries, and
|
|
329
|
+
* `--clear-stale <plugins-dir>` as a mode of its own.
|
|
330
|
+
* @param {readonly string[]} args - arguments after the script path
|
|
331
|
+
* @returns {{ clearStale?: string, pairs: Array<[string, string]>, retire: string[] } | null} the plan, or null when malformed
|
|
332
|
+
*/
|
|
333
|
+
function parseArgs(args) {
|
|
334
|
+
if (args[0] === "--clear-stale") {
|
|
335
|
+
return args.length === 2
|
|
336
|
+
? { clearStale: path.resolve(args[1]), pairs: [], retire: [] }
|
|
337
|
+
: null;
|
|
338
|
+
}
|
|
339
|
+
const pairs = [];
|
|
340
|
+
const retire = [];
|
|
341
|
+
const positional = [];
|
|
342
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
343
|
+
if (args[index] === "--retire") {
|
|
344
|
+
const target = args[index + 1];
|
|
345
|
+
if (!target) return null;
|
|
346
|
+
retire.push(path.resolve(target));
|
|
347
|
+
index += 1;
|
|
348
|
+
} else {
|
|
349
|
+
positional.push(path.resolve(args[index]));
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
if (positional.length % 2 !== 0) return null;
|
|
353
|
+
for (let index = 0; index < positional.length; index += 2) {
|
|
354
|
+
pairs.push([positional[index], positional[index + 1]]);
|
|
355
|
+
}
|
|
356
|
+
return pairs.length + retire.length > 0 ? { pairs, retire } : null;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* Publish and retire every named tree under one lock. All live trees must
|
|
361
|
+
* share one parent, which is the checkout's plugins directory.
|
|
362
|
+
* @param {Array<[string, string]>} pairs - staged and live directories
|
|
363
|
+
* @param {string[]} retire - live trees whose source is gone
|
|
364
|
+
* @returns {void}
|
|
365
|
+
*/
|
|
366
|
+
function publishAll(pairs, retire) {
|
|
367
|
+
const parents = new Set([
|
|
368
|
+
...pairs.map(([, live]) => path.dirname(live)),
|
|
369
|
+
...retire.map(live => path.dirname(live)),
|
|
370
|
+
]);
|
|
371
|
+
if (parents.size !== 1) {
|
|
372
|
+
throw new Error(
|
|
373
|
+
"publish-plugin-tree: every live tree must share one plugins directory"
|
|
374
|
+
);
|
|
375
|
+
}
|
|
376
|
+
const waitMs = Number(process.env.LISA_PLUGIN_PUBLISH_LOCK_WAIT_MS);
|
|
377
|
+
withPublishLock(
|
|
378
|
+
[...parents][0],
|
|
379
|
+
() => {
|
|
380
|
+
for (const [staged, live] of pairs) publishPluginTree(staged, live);
|
|
381
|
+
for (const live of retire) {
|
|
382
|
+
fs.rmSync(live, { force: true, recursive: true });
|
|
383
|
+
}
|
|
384
|
+
},
|
|
385
|
+
Number.isFinite(waitMs) && waitMs >= 0 ? { waitMs } : {}
|
|
386
|
+
);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
if (invokedAsScript(import.meta.url)) {
|
|
390
|
+
const plan = parseArgs(process.argv.slice(2));
|
|
391
|
+
if (plan === null) {
|
|
392
|
+
console.error(
|
|
393
|
+
"Usage: node scripts/lib/publish-plugin-tree.mjs <staged-dir> <live-dir> [...] [--retire <live-dir>]...\n" +
|
|
394
|
+
" node scripts/lib/publish-plugin-tree.mjs --clear-stale <plugins-dir>"
|
|
395
|
+
);
|
|
396
|
+
process.exit(2);
|
|
397
|
+
}
|
|
398
|
+
if (plan.clearStale) {
|
|
399
|
+
clearStaleStages(plan.clearStale);
|
|
400
|
+
} else {
|
|
401
|
+
publishAll(plan.pairs, plan.retire);
|
|
402
|
+
}
|
|
403
|
+
}
|