@paleo/worktree-env 0.4.1 → 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 +12 -6
- package/dist/cli.d.ts +10 -2
- package/dist/cli.js +60 -29
- package/dist/dev-server.d.ts +9 -5
- package/dist/dev-server.js +113 -81
- package/dist/dev-servers-registry.d.ts +15 -11
- package/dist/dev-servers-registry.js +92 -74
- package/dist/errors.d.ts +0 -1
- package/dist/errors.js +0 -4
- package/dist/helpers.js +8 -8
- package/dist/log-polling.d.ts +0 -1
- package/dist/log-polling.js +13 -13
- package/dist/ports.d.ts +5 -5
- package/dist/process-control.d.ts +2 -2
- package/dist/process-control.js +22 -22
- package/dist/setup-worktree.d.ts +60 -48
- package/dist/setup-worktree.js +257 -102
- package/dist/slots.d.ts +20 -19
- package/dist/slots.js +93 -67
- package/dist/worktree.d.ts +5 -6
- package/dist/worktree.js +31 -31
- package/package.json +1 -1
package/dist/setup-worktree.js
CHANGED
|
@@ -1,27 +1,14 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { appendFileSync, closeSync, existsSync, mkdirSync, openSync, readdirSync, readFileSync, symlinkSync, writeFileSync, } from "node:fs";
|
|
2
3
|
import { dirname, join, relative, resolve } from "node:path";
|
|
3
|
-
import { isRemoveMode, isSetOwnerMode, isSetupMode, parseSetupArgs, printSetupHelp, validateSetupFlags, } from "./cli.js";
|
|
4
|
+
import { isFinalizeMode, isInfoMode, isRemoveMode, isSetOwnerMode, isSetupMode, isWaitMode, parseSetupArgs, printSetupHelp, validateSetupFlags, } from "./cli.js";
|
|
4
5
|
import { removeDevServerEntryByWorktree } from "./dev-servers-registry.js";
|
|
5
6
|
import { ConfigError } from "./errors.js";
|
|
6
7
|
import { copyAndPatchFile } from "./helpers.js";
|
|
7
|
-
import { defaultComputePorts, resolvePortScheme } from "./ports.js";
|
|
8
|
-
import { handleSetOwner, readSlots, resolveAndRegisterSlot, validateSlotAvailability, writeSlots, } from "./slots.js";
|
|
9
|
-
import { createBranch, detectWorktree, enforceWorktreeMode, getCurrentBranch, removeWorktree, useExistingBranch, verifyBranchAbsentFromRemote, } from "./worktree.js";
|
|
8
|
+
import { defaultComputePorts, isValidPort, resolvePortScheme } from "./ports.js";
|
|
10
9
|
import { cleanupPidFile, isProcessAlive, isProcessGroupAlive, killProcessGroup, readPid, } from "./process-control.js";
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
if (verbose)
|
|
14
|
-
console.log(msg);
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
function resolvePortsFn(config) {
|
|
18
|
-
if (config.ports)
|
|
19
|
-
return config.ports;
|
|
20
|
-
if (config.portNames && config.portNames.length > 0) {
|
|
21
|
-
return defaultComputePorts(config.portNames);
|
|
22
|
-
}
|
|
23
|
-
throw new ConfigError("Config error: provide either `ports` (function) or `portNames` (array).");
|
|
24
|
-
}
|
|
10
|
+
import { handleSetOwner, markSlotFailed, markSlotReady, readSlots, resolveAndRegisterSlot, resolveCurrentSlot, validateSlotAvailability, writeSlots, } from "./slots.js";
|
|
11
|
+
import { createBranch, detectWorktree, enforceWorktreeMode, getCurrentBranch, removeWorktree, useExistingBranch, verifyBranchAbsentFromRemote, } from "./worktree.js";
|
|
25
12
|
export async function runSetupWorktree(config) {
|
|
26
13
|
let args;
|
|
27
14
|
try {
|
|
@@ -36,6 +23,11 @@ export async function runSetupWorktree(config) {
|
|
|
36
23
|
printSetupHelp();
|
|
37
24
|
return;
|
|
38
25
|
}
|
|
26
|
+
if (!existsSync(config.scriptPath)) {
|
|
27
|
+
console.error(`Error: scriptPath does not exist: ${config.scriptPath}. ` +
|
|
28
|
+
"Pass `fileURLToPath(import.meta.url)` from your wrapper script.");
|
|
29
|
+
process.exit(1);
|
|
30
|
+
}
|
|
39
31
|
try {
|
|
40
32
|
validateSetupFlags(args);
|
|
41
33
|
}
|
|
@@ -46,6 +38,18 @@ export async function runSetupWorktree(config) {
|
|
|
46
38
|
}
|
|
47
39
|
throw err;
|
|
48
40
|
}
|
|
41
|
+
if (isFinalizeMode(args)) {
|
|
42
|
+
await runFinalize(args, config);
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
if (isWaitMode(args)) {
|
|
46
|
+
await runWait(args, config);
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
if (isInfoMode(args)) {
|
|
50
|
+
runInfo(config);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
49
53
|
if (!isSetupMode(args) && !isRemoveMode(args) && !isSetOwnerMode(args)) {
|
|
50
54
|
printSetupHelp();
|
|
51
55
|
return;
|
|
@@ -58,18 +62,18 @@ export async function runSetupWorktree(config) {
|
|
|
58
62
|
return;
|
|
59
63
|
}
|
|
60
64
|
if (isSetOwnerMode(args)) {
|
|
61
|
-
handleSetOwnerMode(args, ctx);
|
|
65
|
+
handleSetOwnerMode(args, ctx, config);
|
|
62
66
|
return;
|
|
63
67
|
}
|
|
64
68
|
await runSetup(args, ctx, run, config);
|
|
65
69
|
}
|
|
66
70
|
async function runSetup(args, ctx, run, config) {
|
|
67
|
-
const verboseLog = makeVerboseLog(run.verbose);
|
|
68
71
|
const scheme = resolvePortScheme(config);
|
|
69
72
|
const portsFn = resolvePortsFn(config);
|
|
70
73
|
validateSlotAvailability(args.slot, {
|
|
71
74
|
currentWorktree: ctx.currentWorktree,
|
|
72
75
|
mainWorktree: ctx.mainWorktree,
|
|
76
|
+
registryDir: config.registryDir,
|
|
73
77
|
scheme,
|
|
74
78
|
});
|
|
75
79
|
const setupCtx = ensureWorktree(args, ctx, run);
|
|
@@ -78,42 +82,235 @@ async function runSetup(args, ctx, run, config) {
|
|
|
78
82
|
slot: args.slot,
|
|
79
83
|
currentWorktree: setupCtx.currentWorktree,
|
|
80
84
|
mainWorktree: setupCtx.mainWorktree,
|
|
85
|
+
registryDir: config.registryDir,
|
|
81
86
|
scheme,
|
|
82
87
|
branch,
|
|
83
88
|
requestedOwner: args.owner,
|
|
84
89
|
});
|
|
85
90
|
const ports = portsFn(slot);
|
|
91
|
+
const runtimeDir = join(setupCtx.currentWorktree, config.runtimeDir);
|
|
92
|
+
mkdirSync(runtimeDir, { recursive: true });
|
|
93
|
+
const logPath = join(runtimeDir, "wt-setup.log");
|
|
94
|
+
// Opened "a" so the same fd can be inherited by the detached finalize child below.
|
|
95
|
+
const logFd = openSync(logPath, "a");
|
|
96
|
+
const teeLog = (message) => {
|
|
97
|
+
console.log(message);
|
|
98
|
+
appendFileSync(logFd, `${message}\n`);
|
|
99
|
+
};
|
|
100
|
+
const verboseLog = (msg) => {
|
|
101
|
+
if (run.verbose)
|
|
102
|
+
teeLog(msg);
|
|
103
|
+
else
|
|
104
|
+
appendFileSync(logFd, `${msg}\n`);
|
|
105
|
+
};
|
|
86
106
|
verboseLog(`Using slot ${slot} (${Object.entries(ports)
|
|
87
107
|
.map(([k, v]) => `${k}: ${v}`)
|
|
88
108
|
.join(", ")})`);
|
|
89
|
-
|
|
90
|
-
const sharedDirs = config.sharedDirs ?? [".local", ".plans"];
|
|
91
|
-
linkSharedDirectories(setupCtx, sharedDirs, verboseLog);
|
|
109
|
+
linkSharedDirectories(setupCtx, config.sharedDirs, verboseLog);
|
|
92
110
|
generateConfigFiles(setupCtx, config.configFiles, slot, ports, args.force ?? false, verboseLog);
|
|
93
|
-
|
|
94
|
-
const setupContext = {
|
|
95
|
-
currentWorktree: setupCtx.currentWorktree,
|
|
96
|
-
mainWorktree: setupCtx.mainWorktree,
|
|
111
|
+
teeLog(config.printSummary({
|
|
97
112
|
slot,
|
|
98
113
|
branch,
|
|
99
114
|
owner,
|
|
100
115
|
ports,
|
|
101
|
-
|
|
102
|
-
|
|
116
|
+
currentWorktree: setupCtx.currentWorktree,
|
|
117
|
+
mainWorktree: setupCtx.mainWorktree,
|
|
118
|
+
}));
|
|
119
|
+
teeLog(`WORKTREE_CREATED path=${setupCtx.currentWorktree} branch=${branch} slot=${slot}`);
|
|
120
|
+
teeLog(`Setup continuing in background. Tail: ${config.runtimeDir}/wt-setup.log`);
|
|
121
|
+
const child = spawn(process.execPath, [config.scriptPath, "--__finalize", String(slot)], {
|
|
122
|
+
detached: true,
|
|
123
|
+
stdio: ["ignore", logFd, logFd],
|
|
124
|
+
cwd: setupCtx.currentWorktree,
|
|
125
|
+
});
|
|
126
|
+
child.unref();
|
|
127
|
+
closeSync(logFd);
|
|
128
|
+
}
|
|
129
|
+
async function runFinalize(args, config) {
|
|
130
|
+
const slot = Number(args.__finalize);
|
|
131
|
+
const ctx = detectWorktree();
|
|
132
|
+
const logPath = join(ctx.currentWorktree, config.runtimeDir, "wt-setup.log");
|
|
133
|
+
const appendLog = (message) => {
|
|
134
|
+
appendFileSync(logPath, `${message}\n`);
|
|
135
|
+
};
|
|
136
|
+
const registry = readSlots(ctx.mainWorktree, config.registryDir);
|
|
137
|
+
const entry = registry.slots[String(slot)];
|
|
138
|
+
if (!entry || resolve(entry.worktree) !== resolve(ctx.currentWorktree)) {
|
|
139
|
+
appendLog(`FAILED: No matching slot ${slot} for worktree ${ctx.currentWorktree}.`);
|
|
140
|
+
process.exit(1);
|
|
141
|
+
}
|
|
142
|
+
if (entry.status === "ready" && !args.force) {
|
|
143
|
+
appendLog(`READY: branch ${entry.branch} (slot ${slot}) already finalized; skipping.`);
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
const portsFn = resolvePortsFn(config);
|
|
147
|
+
const ports = portsFn(slot);
|
|
148
|
+
appendLog(`--- finalizing slot ${slot} at ${new Date().toISOString()} ---`);
|
|
149
|
+
const setupContext = {
|
|
150
|
+
currentWorktree: ctx.currentWorktree,
|
|
151
|
+
mainWorktree: ctx.mainWorktree,
|
|
152
|
+
slot,
|
|
153
|
+
branch: entry.branch,
|
|
154
|
+
owner: entry.owner,
|
|
155
|
+
ports,
|
|
156
|
+
force: args.force ?? false,
|
|
157
|
+
verbose: false,
|
|
103
158
|
};
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
159
|
+
try {
|
|
160
|
+
await config.finalizeWorktree(setupContext);
|
|
161
|
+
markSlotReady(ctx.mainWorktree, config.registryDir, slot);
|
|
162
|
+
appendLog("============================================================");
|
|
163
|
+
appendLog(`READY: branch ${entry.branch} (slot ${slot})`);
|
|
164
|
+
appendLog("============================================================");
|
|
165
|
+
}
|
|
166
|
+
catch (err) {
|
|
167
|
+
const message = err.message;
|
|
168
|
+
const stack = err.stack ?? "";
|
|
169
|
+
markSlotFailed(ctx.mainWorktree, config.registryDir, slot, message);
|
|
170
|
+
appendLog(`FAILED: ${message}`);
|
|
171
|
+
if (stack)
|
|
172
|
+
appendLog(stack);
|
|
173
|
+
process.exit(1);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
function resolveWaitSlot(args, config) {
|
|
177
|
+
if (args.slot !== undefined) {
|
|
178
|
+
const slot = Number(args.slot);
|
|
179
|
+
const scheme = resolvePortScheme(config);
|
|
180
|
+
if (!isValidPort(slot, scheme)) {
|
|
181
|
+
console.error(`Error: --slot expects a port in [${scheme.minPort}, ${scheme.maxPort}] stepped by ${scheme.portStep}; got "${args.slot}".`);
|
|
182
|
+
process.exit(1);
|
|
183
|
+
}
|
|
184
|
+
return slot;
|
|
185
|
+
}
|
|
186
|
+
return resolveCurrentSlot(config.basePort, config.registryDir).slot;
|
|
187
|
+
}
|
|
188
|
+
function printWorktreeInfo(config, slot, worktreeForLog, fallback) {
|
|
189
|
+
const ctx = detectWorktree();
|
|
190
|
+
const registry = readSlots(ctx.mainWorktree, config.registryDir);
|
|
191
|
+
const entry = registry.slots[String(slot)];
|
|
192
|
+
const ports = resolvePortsFn(config)(slot);
|
|
193
|
+
const branch = entry?.branch ?? fallback.branch;
|
|
194
|
+
const owner = entry?.owner ?? fallback.owner;
|
|
195
|
+
// Main worktree has no slot entry by design — treat it as ready when the registry has no row.
|
|
196
|
+
const slotStatus = entry?.status ?? (ctx.isMainWorktree ? "ready" : "pending");
|
|
197
|
+
const logHint = ` (tail ${join(worktreeForLog, config.runtimeDir, "wt-setup.log")})`;
|
|
198
|
+
const display = slotStatus === "ready"
|
|
199
|
+
? "ready"
|
|
200
|
+
: slotStatus === "failed"
|
|
201
|
+
? `failed: ${entry?.failure?.message ?? "(no message)"}${logHint}`
|
|
202
|
+
: `pending${logHint}`;
|
|
203
|
+
console.log(`Status: ${display}`);
|
|
108
204
|
console.log(config.printSummary({
|
|
109
205
|
slot,
|
|
110
206
|
branch,
|
|
111
207
|
owner,
|
|
112
208
|
ports,
|
|
113
|
-
currentWorktree:
|
|
114
|
-
mainWorktree:
|
|
209
|
+
currentWorktree: entry?.worktree ?? ctx.currentWorktree,
|
|
210
|
+
mainWorktree: ctx.mainWorktree,
|
|
115
211
|
}));
|
|
116
212
|
}
|
|
213
|
+
function runInfo(config) {
|
|
214
|
+
const resolved = resolveCurrentSlot(config.basePort, config.registryDir);
|
|
215
|
+
printWorktreeInfo(config, resolved.slot, ".", { branch: resolved.branch, owner: resolved.owner });
|
|
216
|
+
}
|
|
217
|
+
async function runWait(args, config) {
|
|
218
|
+
const ctx = detectWorktree();
|
|
219
|
+
const slot = resolveWaitSlot(args, config);
|
|
220
|
+
const initial = readSlots(ctx.mainWorktree, config.registryDir).slots[String(slot)];
|
|
221
|
+
if (!initial) {
|
|
222
|
+
console.error(`Error: No slot ${slot} in registry.`);
|
|
223
|
+
process.exit(1);
|
|
224
|
+
}
|
|
225
|
+
const pollMs = 500;
|
|
226
|
+
// Poll slots.json — the finalize child writes `status` on success or failure. Tiny file, no
|
|
227
|
+
// log-tailing race.
|
|
228
|
+
for (;;) {
|
|
229
|
+
const entry = readSlots(ctx.mainWorktree, config.registryDir).slots[String(slot)];
|
|
230
|
+
if (!entry) {
|
|
231
|
+
console.error(`Error: Slot ${slot} disappeared from registry.`);
|
|
232
|
+
process.exit(1);
|
|
233
|
+
}
|
|
234
|
+
if (entry.status === "ready") {
|
|
235
|
+
printWorktreeInfo(config, slot, entry.worktree, { branch: entry.branch, owner: entry.owner });
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
if (entry.status === "failed") {
|
|
239
|
+
const logPath = join(entry.worktree, config.runtimeDir, "wt-setup.log");
|
|
240
|
+
console.error(`FAILED: ${entry.failure?.message ?? "(no message)"}`);
|
|
241
|
+
console.error(`Full log: ${logPath}`);
|
|
242
|
+
process.exit(1);
|
|
243
|
+
}
|
|
244
|
+
await new Promise((r) => setTimeout(r, pollMs));
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
async function handleRemove(args, ctx, run, config) {
|
|
248
|
+
const verboseLog = makeVerboseLog(run.verbose);
|
|
249
|
+
const removeHere = Boolean(args["remove-here"]);
|
|
250
|
+
const registry = readSlots(ctx.mainWorktree, config.registryDir);
|
|
251
|
+
const target = resolveRemoveTarget(args, ctx, registry, removeHere);
|
|
252
|
+
if (!args["no-remote-check"]) {
|
|
253
|
+
verifyBranchAbsentFromRemote(target.branch, run);
|
|
254
|
+
}
|
|
255
|
+
const ownerSuffix = target.owner ? `, owner ${target.owner}` : "";
|
|
256
|
+
if (!existsSync(target.worktreePath)) {
|
|
257
|
+
console.warn(`Warning: Worktree directory ${target.worktreePath} not found. Cleaning up registry only.`);
|
|
258
|
+
delete registry.slots[target.slotPort];
|
|
259
|
+
writeSlots(ctx.mainWorktree, config.registryDir, registry);
|
|
260
|
+
console.log(`Removed registry entry for branch "${target.branch}" (slot ${target.slotPort}${ownerSuffix}).`);
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
await stopAllDevServersInRuntimeDir(target.worktreePath, config.runtimeDir, verboseLog);
|
|
264
|
+
if (config.teardownInfrastructure) {
|
|
265
|
+
await config.teardownInfrastructure({
|
|
266
|
+
worktree: target.worktreePath,
|
|
267
|
+
mainWorktree: ctx.mainWorktree,
|
|
268
|
+
verbose: run.verbose,
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
delete registry.slots[target.slotPort];
|
|
272
|
+
writeSlots(ctx.mainWorktree, config.registryDir, registry);
|
|
273
|
+
removeDevServerEntryByWorktree(ctx.mainWorktree, config.registryDir, target.worktreePath);
|
|
274
|
+
if (removeHere) {
|
|
275
|
+
process.chdir(ctx.mainWorktree);
|
|
276
|
+
}
|
|
277
|
+
removeWorktree(target.worktreePath, run);
|
|
278
|
+
console.log(`Removed worktree for branch "${target.branch}" (slot ${target.slotPort}${ownerSuffix}).`);
|
|
279
|
+
if (removeHere) {
|
|
280
|
+
console.log(`Now run: cd ${ctx.mainWorktree}`);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
function handleSetOwnerMode(args, ctx, config) {
|
|
284
|
+
const newOwner = args["set-owner"];
|
|
285
|
+
const { slotPort } = handleSetOwner({
|
|
286
|
+
newOwner,
|
|
287
|
+
currentWorktree: ctx.currentWorktree,
|
|
288
|
+
mainWorktree: ctx.mainWorktree,
|
|
289
|
+
registryDir: config.registryDir,
|
|
290
|
+
isMainWorktree: ctx.isMainWorktree,
|
|
291
|
+
});
|
|
292
|
+
// Propagate to dev-servers.json entries for this worktree.
|
|
293
|
+
const devServersPath = join(ctx.mainWorktree, config.registryDir, "dev-servers.json");
|
|
294
|
+
if (existsSync(devServersPath)) {
|
|
295
|
+
const data = JSON.parse(readFileSync(devServersPath, "utf-8"));
|
|
296
|
+
let changed = false;
|
|
297
|
+
const resolvedCurrent = resolve(ctx.currentWorktree);
|
|
298
|
+
for (const server of data.servers) {
|
|
299
|
+
if (resolve(server.worktree) === resolvedCurrent) {
|
|
300
|
+
if (newOwner !== undefined)
|
|
301
|
+
server.owner = newOwner;
|
|
302
|
+
else
|
|
303
|
+
delete server.owner;
|
|
304
|
+
changed = true;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
if (changed) {
|
|
308
|
+
mkdirSync(dirname(devServersPath), { recursive: true });
|
|
309
|
+
writeFileSync(devServersPath, `${JSON.stringify(data, undefined, 2)}\n`);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
console.log(`Owner for slot ${slotPort}: ${newOwner ?? "(none)"}`);
|
|
313
|
+
}
|
|
117
314
|
function ensureWorktree(args, ctx, run) {
|
|
118
315
|
if (args.use)
|
|
119
316
|
return useExistingBranch(args.use, ctx, run);
|
|
@@ -180,9 +377,19 @@ function resolveRemoveTarget(args, ctx, registry, removeHere) {
|
|
|
180
377
|
}
|
|
181
378
|
return { slotPort: entry[0], branch, worktreePath, owner: entry[1].owner };
|
|
182
379
|
}
|
|
183
|
-
async function
|
|
184
|
-
|
|
185
|
-
|
|
380
|
+
async function stopAllDevServersInRuntimeDir(worktreePath, runtimeDir, log) {
|
|
381
|
+
const dir = join(worktreePath, runtimeDir);
|
|
382
|
+
let entries;
|
|
383
|
+
try {
|
|
384
|
+
entries = readdirSync(dir);
|
|
385
|
+
}
|
|
386
|
+
catch {
|
|
387
|
+
return;
|
|
388
|
+
}
|
|
389
|
+
for (const name of entries) {
|
|
390
|
+
if (!name.endsWith(".pid"))
|
|
391
|
+
continue;
|
|
392
|
+
const pidFile = join(dir, name);
|
|
186
393
|
const pid = readPid(pidFile);
|
|
187
394
|
if (pid === undefined)
|
|
188
395
|
continue;
|
|
@@ -207,69 +414,17 @@ async function stopDevServerByPidFiles(worktreePath, pidFiles, log) {
|
|
|
207
414
|
cleanupPidFile(pidFile);
|
|
208
415
|
}
|
|
209
416
|
}
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
if (!args["no-remote-check"]) {
|
|
216
|
-
verifyBranchAbsentFromRemote(target.branch, run);
|
|
217
|
-
}
|
|
218
|
-
const ownerSuffix = target.owner ? `, owner ${target.owner}` : "";
|
|
219
|
-
if (!existsSync(target.worktreePath)) {
|
|
220
|
-
console.warn(`Warning: Worktree directory ${target.worktreePath} not found. Cleaning up registry only.`);
|
|
221
|
-
delete registry.slots[target.slotPort];
|
|
222
|
-
writeSlots(ctx.mainWorktree, registry);
|
|
223
|
-
console.log(`Removed registry entry for branch "${target.branch}" (slot ${target.slotPort}${ownerSuffix}).`);
|
|
224
|
-
return;
|
|
225
|
-
}
|
|
226
|
-
await stopDevServerByPidFiles(target.worktreePath, config.devServerPidFiles, verboseLog);
|
|
227
|
-
if (config.teardownInfrastructure) {
|
|
228
|
-
await config.teardownInfrastructure({
|
|
229
|
-
worktree: target.worktreePath,
|
|
230
|
-
mainWorktree: ctx.mainWorktree,
|
|
231
|
-
verbose: run.verbose,
|
|
232
|
-
});
|
|
233
|
-
}
|
|
234
|
-
delete registry.slots[target.slotPort];
|
|
235
|
-
writeSlots(ctx.mainWorktree, registry);
|
|
236
|
-
removeDevServerEntryByWorktree(ctx.mainWorktree, target.worktreePath);
|
|
237
|
-
if (removeHere) {
|
|
238
|
-
process.chdir(ctx.mainWorktree);
|
|
239
|
-
}
|
|
240
|
-
removeWorktree(target.worktreePath, run);
|
|
241
|
-
console.log(`Removed worktree for branch "${target.branch}" (slot ${target.slotPort}${ownerSuffix}).`);
|
|
242
|
-
if (removeHere) {
|
|
243
|
-
console.log(`Now run: cd ${ctx.mainWorktree}`);
|
|
417
|
+
function resolvePortsFn(config) {
|
|
418
|
+
if (config.ports)
|
|
419
|
+
return config.ports;
|
|
420
|
+
if (config.portNames && config.portNames.length > 0) {
|
|
421
|
+
return defaultComputePorts(config.portNames);
|
|
244
422
|
}
|
|
423
|
+
throw new ConfigError("Config error: provide either `ports` (function) or `portNames` (array).");
|
|
245
424
|
}
|
|
246
|
-
function
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
mainWorktree: ctx.mainWorktree,
|
|
252
|
-
isMainWorktree: ctx.isMainWorktree,
|
|
253
|
-
});
|
|
254
|
-
// Propagate to dev-servers.json entries for this worktree.
|
|
255
|
-
const devServersPath = join(ctx.mainWorktree, ".local/worktrees/dev-servers.json");
|
|
256
|
-
if (existsSync(devServersPath)) {
|
|
257
|
-
const data = JSON.parse(readFileSync(devServersPath, "utf-8"));
|
|
258
|
-
let changed = false;
|
|
259
|
-
const resolvedCurrent = resolve(ctx.currentWorktree);
|
|
260
|
-
for (const server of data.servers) {
|
|
261
|
-
if (resolve(server.worktree) === resolvedCurrent) {
|
|
262
|
-
if (newOwner !== undefined)
|
|
263
|
-
server.owner = newOwner;
|
|
264
|
-
else
|
|
265
|
-
delete server.owner;
|
|
266
|
-
changed = true;
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
if (changed) {
|
|
270
|
-
mkdirSync(dirname(devServersPath), { recursive: true });
|
|
271
|
-
writeFileSync(devServersPath, `${JSON.stringify(data, undefined, 2)}\n`);
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
console.log(`Owner for slot ${slotPort}: ${newOwner ?? "(none)"}`);
|
|
425
|
+
function makeVerboseLog(verbose) {
|
|
426
|
+
return (msg) => {
|
|
427
|
+
if (verbose)
|
|
428
|
+
console.log(msg);
|
|
429
|
+
};
|
|
275
430
|
}
|
package/dist/slots.d.ts
CHANGED
|
@@ -1,33 +1,32 @@
|
|
|
1
1
|
import { type PortScheme } from "./ports.js";
|
|
2
|
-
export
|
|
3
|
-
|
|
4
|
-
export interface SlotEntry {
|
|
2
|
+
export interface ResolvedSlot {
|
|
3
|
+
slot: number;
|
|
5
4
|
worktree: string;
|
|
6
5
|
branch: string;
|
|
7
6
|
owner?: string;
|
|
8
7
|
}
|
|
9
|
-
export
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
-
export interface ResolvedSlot {
|
|
13
|
-
slot: number;
|
|
8
|
+
export type SlotStatus = "pending" | "ready" | "failed";
|
|
9
|
+
export interface SlotEntry {
|
|
14
10
|
worktree: string;
|
|
15
11
|
branch: string;
|
|
16
12
|
owner?: string;
|
|
13
|
+
createdAt: string;
|
|
14
|
+
status: SlotStatus;
|
|
15
|
+
failure?: {
|
|
16
|
+
at: string;
|
|
17
|
+
message: string;
|
|
18
|
+
};
|
|
17
19
|
}
|
|
18
|
-
export
|
|
19
|
-
|
|
20
|
-
export interface PickSlotArgs {
|
|
21
|
-
slot?: string;
|
|
22
|
-
currentWorktree: string;
|
|
23
|
-
mainWorktree: string;
|
|
24
|
-
scheme: PortScheme;
|
|
20
|
+
export interface SlotsRegistry {
|
|
21
|
+
slots: Record<string, SlotEntry>;
|
|
25
22
|
}
|
|
26
|
-
export declare function
|
|
23
|
+
export declare function readSlots(mainWorktree: string, registryDir: string): SlotsRegistry;
|
|
24
|
+
export declare function writeSlots(mainWorktree: string, registryDir: string, registry: SlotsRegistry): void;
|
|
27
25
|
export interface RegisterSlotInput {
|
|
28
26
|
slot?: string;
|
|
29
27
|
currentWorktree: string;
|
|
30
28
|
mainWorktree: string;
|
|
29
|
+
registryDir: string;
|
|
31
30
|
scheme: PortScheme;
|
|
32
31
|
branch: string;
|
|
33
32
|
requestedOwner?: string;
|
|
@@ -36,18 +35,20 @@ export declare function resolveAndRegisterSlot(input: RegisterSlotInput): {
|
|
|
36
35
|
port: number;
|
|
37
36
|
owner: string | undefined;
|
|
38
37
|
};
|
|
38
|
+
export declare function markSlotReady(mainWorktree: string, registryDir: string, slotPort: number): void;
|
|
39
|
+
export declare function markSlotFailed(mainWorktree: string, registryDir: string, slotPort: number, message: string): void;
|
|
39
40
|
export declare function validateSlotAvailability(slotArg: string | undefined, ctx: {
|
|
40
41
|
currentWorktree: string;
|
|
41
42
|
mainWorktree: string;
|
|
43
|
+
registryDir: string;
|
|
42
44
|
scheme: PortScheme;
|
|
43
45
|
}): void;
|
|
44
|
-
export declare function
|
|
45
|
-
export declare function synthesizeMainSlot(basePort: number): ResolvedSlot | undefined;
|
|
46
|
-
export declare function resolveCurrentSlot(basePort: number): ResolvedSlot;
|
|
46
|
+
export declare function resolveCurrentSlot(basePort: number, registryDir: string): ResolvedSlot;
|
|
47
47
|
export interface SetOwnerInput {
|
|
48
48
|
newOwner: string | undefined;
|
|
49
49
|
currentWorktree: string;
|
|
50
50
|
mainWorktree: string;
|
|
51
|
+
registryDir: string;
|
|
51
52
|
isMainWorktree: boolean;
|
|
52
53
|
}
|
|
53
54
|
export declare function handleSetOwner(input: SetOwnerInput): {
|