worktree-compose 1.1.1 → 2.0.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/LICENSE +2 -2
- package/README.md +66 -720
- package/SKILL.md +73 -76
- package/dist/cli.js +85 -10
- package/dist/commands/down.d.ts +2 -0
- package/dist/commands/down.js +7 -0
- package/dist/commands/list.d.ts +2 -0
- package/dist/commands/list.js +43 -6
- package/dist/commands/passthrough.d.ts +13 -0
- package/dist/commands/passthrough.js +27 -0
- package/dist/commands/stop.d.ts +3 -1
- package/dist/commands/stop.js +2 -2
- package/dist/compose/detect.d.ts +19 -0
- package/dist/compose/detect.js +56 -0
- package/dist/compose/isolation.d.ts +20 -7
- package/dist/compose/isolation.js +88 -36
- package/dist/compose/override.d.ts +54 -4
- package/dist/compose/override.js +179 -52
- package/dist/compose/parse.d.ts +11 -0
- package/dist/compose/parse.js +260 -13
- package/dist/compose/status.d.ts +14 -0
- package/dist/compose/status.js +58 -1
- package/dist/compose/types.d.ts +56 -0
- package/dist/config.d.ts +33 -0
- package/dist/context.d.ts +26 -0
- package/dist/context.js +140 -29
- package/dist/core/clean.d.ts +3 -1
- package/dist/core/clean.js +8 -3
- package/dist/core/compose.d.ts +87 -13
- package/dist/core/compose.js +282 -57
- package/dist/core/doctor.d.ts +2 -8
- package/dist/core/doctor.js +116 -79
- package/dist/core/down.d.ts +37 -0
- package/dist/core/down.js +54 -0
- package/dist/core/health.d.ts +90 -0
- package/dist/core/health.js +198 -0
- package/dist/core/list.d.ts +53 -6
- package/dist/core/list.js +144 -58
- package/dist/core/passthrough.d.ts +69 -0
- package/dist/core/passthrough.js +216 -0
- package/dist/core/promote.d.ts +4 -2
- package/dist/core/promote.js +5 -3
- package/dist/core/restart.d.ts +3 -3
- package/dist/core/restart.js +6 -5
- package/dist/core/seed.d.ts +50 -0
- package/dist/core/seed.js +245 -0
- package/dist/core/shared.d.ts +63 -0
- package/dist/core/shared.js +194 -0
- package/dist/core/start.d.ts +32 -5
- package/dist/core/start.js +297 -49
- package/dist/core/stop.d.ts +3 -1
- package/dist/core/stop.js +6 -3
- package/dist/core/teardown.d.ts +5 -2
- package/dist/core/teardown.js +33 -8
- package/dist/core/types.d.ts +2 -0
- package/dist/git/promote.d.ts +11 -3
- package/dist/git/promote.js +19 -5
- package/dist/mcp/server.js +154 -28
- package/dist/ports/allocate.d.ts +35 -23
- package/dist/ports/allocate.js +108 -58
- package/dist/ports/probe.d.ts +11 -0
- package/dist/ports/probe.js +40 -0
- package/dist/ports/types.d.ts +5 -0
- package/dist/selectors.d.ts +17 -0
- package/dist/selectors.js +19 -0
- package/dist/state/registry.d.ts +23 -3
- package/dist/state/registry.js +57 -17
- package/dist/sync/env.d.ts +42 -2
- package/dist/sync/env.js +79 -8
- package/dist/sync/envsync.d.ts +91 -0
- package/dist/sync/envsync.js +338 -0
- package/dist/sync/files.d.ts +87 -5
- package/dist/sync/files.js +343 -18
- package/dist/utils/paths.d.ts +2 -0
- package/dist/utils/paths.js +9 -0
- package/package.json +9 -2
- package/wtc.schema.json +155 -13
package/dist/core/doctor.js
CHANGED
|
@@ -1,47 +1,21 @@
|
|
|
1
|
-
import net from "node:net";
|
|
2
1
|
import path from "node:path";
|
|
3
|
-
import { buildContext, resolveConfig } from "../context.js";
|
|
2
|
+
import { buildContext, hostPorts, resolveConfig, sharedServices } from "../context.js";
|
|
3
|
+
import { neededUntrackedFiles } from "../sync/files.js";
|
|
4
4
|
import { loadConfigWithSource } from "../config.js";
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
5
|
+
import { resolveComposeRoots } from "../compose/detect.js";
|
|
6
|
+
import { parseComposeFiles } from "../compose/parse.js";
|
|
7
7
|
import { daemonCheckArgs, resolveEngine } from "../compose/engine.js";
|
|
8
8
|
import { createStackProbe, stackStatus } from "../compose/status.js";
|
|
9
|
-
import { composeIsolationWarnings,
|
|
9
|
+
import { composeIsolationWarnings, hostPortsInEnvironment, rawPortWarnings, remapNotes, sharedHostPaths, } from "../compose/isolation.js";
|
|
10
10
|
import { decideRemap } from "../compose/override.js";
|
|
11
11
|
import { extractPortMappings } from "../ports/extract.js";
|
|
12
12
|
import { portLabel } from "../ports/allocate.js";
|
|
13
|
+
import { isPortBusy } from "../ports/probe.js";
|
|
13
14
|
import { stackAllocations, stackRemap } from "./compose.js";
|
|
14
15
|
import { DEFAULT_ENV_FILE, findEnvSource } from "../sync/env.js";
|
|
15
16
|
import { getRepoInfo } from "../git/repo.js";
|
|
16
17
|
import { ENGINE_PROBE_TIMEOUT_MS, runSafe } from "../utils/exec.js";
|
|
17
|
-
|
|
18
|
-
function bindFails(port, host) {
|
|
19
|
-
return new Promise((resolve) => {
|
|
20
|
-
const server = net.createServer();
|
|
21
|
-
server.unref();
|
|
22
|
-
server.once("error", (err) => {
|
|
23
|
-
resolve(err.code === "EADDRINUSE");
|
|
24
|
-
});
|
|
25
|
-
server.once("listening", () => {
|
|
26
|
-
server.close(() => resolve(false));
|
|
27
|
-
});
|
|
28
|
-
server.listen({ port, host, exclusive: true });
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* True when something already listens on `port`. Docker publishes on all
|
|
33
|
-
* interfaces, but local dev servers and Homebrew databases usually bind
|
|
34
|
-
* 127.0.0.1 or ::1 only — and on macOS (BSD sockets, libuv's SO_REUSEADDR)
|
|
35
|
-
* a wildcard bind succeeds next to them, so each address is probed. Only
|
|
36
|
-
* EADDRINUSE counts as busy (no IPv6 → not busy).
|
|
37
|
-
*/
|
|
38
|
-
export async function isPortBusy(port) {
|
|
39
|
-
for (const host of ["0.0.0.0", "127.0.0.1", "::1"]) {
|
|
40
|
-
if (await bindFails(port, host))
|
|
41
|
-
return true;
|
|
42
|
-
}
|
|
43
|
-
return false;
|
|
44
|
-
}
|
|
18
|
+
export { isPortBusy };
|
|
45
19
|
function message(err) {
|
|
46
20
|
return err instanceof Error ? err.message : String(err);
|
|
47
21
|
}
|
|
@@ -91,47 +65,67 @@ export async function doctor(io) {
|
|
|
91
65
|
});
|
|
92
66
|
for (const w of repo.warnings)
|
|
93
67
|
add({ status: "warn", title: w });
|
|
68
|
+
// config
|
|
69
|
+
let config = null;
|
|
70
|
+
try {
|
|
71
|
+
const loaded = loadConfigWithSource(repo.mainRoot);
|
|
72
|
+
config = resolveConfig(loaded.config, {});
|
|
73
|
+
add({
|
|
74
|
+
status: "ok",
|
|
75
|
+
title: loaded.source ? `Config: ${path.relative(repo.mainRoot, loaded.source) || loaded.source}` : "No config (zero-config defaults)",
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
catch (err) {
|
|
79
|
+
add({ status: "fail", title: "Invalid wtc config", detail: message(err), hint: "Fix .wtcrc.json (or package.json#wtc)." });
|
|
80
|
+
}
|
|
94
81
|
// compose file
|
|
95
82
|
let composeFile = null;
|
|
96
|
-
|
|
83
|
+
let roots = [];
|
|
84
|
+
/** Compose files compose would not find by itself, as the config names them (see the context). */
|
|
85
|
+
let customRoots = [];
|
|
86
|
+
try {
|
|
87
|
+
const resolved = resolveComposeRoots(repo.mainRoot, config?.composeFile);
|
|
88
|
+
roots = resolved.files;
|
|
89
|
+
if (resolved.custom)
|
|
90
|
+
customRoots = roots.map((f) => path.relative(repo.mainRoot, f));
|
|
91
|
+
if (resolved.note)
|
|
92
|
+
add({ status: "ok", title: resolved.note });
|
|
93
|
+
}
|
|
94
|
+
catch (err) {
|
|
95
|
+
add({ status: "fail", title: "Could not locate the compose file", detail: message(err) });
|
|
96
|
+
}
|
|
97
|
+
const composePath = roots[0] ?? null;
|
|
97
98
|
if (!composePath) {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
99
|
+
if (!checks.some((c) => c.title === "Could not locate the compose file")) {
|
|
100
|
+
add({
|
|
101
|
+
status: "fail",
|
|
102
|
+
title: "No compose file found",
|
|
103
|
+
detail: `Looked in ${repo.mainRoot}`,
|
|
104
|
+
hint: 'Add compose.yaml (or docker-compose.yml) to the main worktree, or set "composeFile" in .wtcrc.json to where yours is.',
|
|
105
|
+
});
|
|
106
|
+
}
|
|
104
107
|
}
|
|
105
108
|
else {
|
|
109
|
+
const name = roots.map((f) => path.relative(repo.mainRoot, f)).join(" + ");
|
|
106
110
|
try {
|
|
107
|
-
composeFile =
|
|
108
|
-
const includes = composeFile.sourceFiles.length -
|
|
111
|
+
composeFile = parseComposeFiles(roots);
|
|
112
|
+
const includes = composeFile.sourceFiles.length - roots.length;
|
|
109
113
|
add({
|
|
110
114
|
status: "ok",
|
|
111
|
-
title: `${
|
|
115
|
+
title: `${name}: ${composeFile.services.length} service(s)` +
|
|
112
116
|
(includes > 0 ? `, ${includes} included file(s)` : ""),
|
|
113
117
|
detail: includes > 0
|
|
114
|
-
? composeFile.sourceFiles
|
|
118
|
+
? composeFile.sourceFiles
|
|
119
|
+
.filter((f) => !composeFile.rootFiles?.includes(f) && f !== composeFile.sourceFiles[0])
|
|
120
|
+
.map((f) => path.relative(repo.mainRoot, f))
|
|
121
|
+
.join(", ")
|
|
115
122
|
: undefined,
|
|
116
123
|
});
|
|
117
124
|
}
|
|
118
125
|
catch (err) {
|
|
119
|
-
add({ status: "fail", title: `Could not parse ${
|
|
126
|
+
add({ status: "fail", title: `Could not parse ${name}`, detail: message(err) });
|
|
120
127
|
}
|
|
121
128
|
}
|
|
122
|
-
// config
|
|
123
|
-
let config = null;
|
|
124
|
-
try {
|
|
125
|
-
const loaded = loadConfigWithSource(repo.mainRoot);
|
|
126
|
-
config = resolveConfig(loaded.config, {});
|
|
127
|
-
add({
|
|
128
|
-
status: "ok",
|
|
129
|
-
title: loaded.source ? `Config: ${path.relative(repo.mainRoot, loaded.source) || loaded.source}` : "No config (zero-config defaults)",
|
|
130
|
-
});
|
|
131
|
-
}
|
|
132
|
-
catch (err) {
|
|
133
|
-
add({ status: "fail", title: "Invalid wtc config", detail: message(err), hint: "Fix .wtcrc.json (or package.json#wtc)." });
|
|
134
|
-
}
|
|
135
129
|
// compose engine + daemon
|
|
136
130
|
let engine = null;
|
|
137
131
|
let composeWorks = false;
|
|
@@ -176,12 +170,17 @@ export async function doctor(io) {
|
|
|
176
170
|
if (!composeFile)
|
|
177
171
|
return finish();
|
|
178
172
|
// ports and isolation
|
|
179
|
-
const
|
|
173
|
+
const shared = sharedServices(config ?? {}, composeFile);
|
|
174
|
+
const mappings = [
|
|
175
|
+
...extractPortMappings(composeFile.services).filter((m) => !shared.has(m.serviceName)),
|
|
176
|
+
...hostPorts(config ?? {}),
|
|
177
|
+
];
|
|
180
178
|
const remap = decideRemap({
|
|
181
179
|
composeFile,
|
|
182
180
|
mappings,
|
|
183
181
|
mainRoot: repo.mainRoot,
|
|
184
182
|
commonDir: repo.commonDir,
|
|
183
|
+
composeRoots: customRoots,
|
|
185
184
|
envFiles: [
|
|
186
185
|
findEnvSource(repo.mainRoot, { dir: "", label: "root", envFileName: config?.envFile ?? DEFAULT_ENV_FILE }),
|
|
187
186
|
path.join(repo.mainRoot, DEFAULT_ENV_FILE),
|
|
@@ -212,7 +211,7 @@ export async function doctor(io) {
|
|
|
212
211
|
add({
|
|
213
212
|
status: "ok",
|
|
214
213
|
title: "Hardcoded settings remapped at runtime (compose file unchanged)",
|
|
215
|
-
detail: remapNotes(remap.targets, mappings).join(" "),
|
|
214
|
+
detail: remapNotes(remap.targets, mappings, true).join(" "),
|
|
216
215
|
});
|
|
217
216
|
}
|
|
218
217
|
if (remap.problem) {
|
|
@@ -229,7 +228,10 @@ export async function doctor(io) {
|
|
|
229
228
|
for (const w of rawPortWarnings(mappings, remapped)) {
|
|
230
229
|
add({ status: "warn", title: "Raw port mapping (not isolated)", detail: w });
|
|
231
230
|
}
|
|
232
|
-
const isolation =
|
|
231
|
+
const isolation = [
|
|
232
|
+
...composeIsolationWarnings(composeFile, new Set(remap.targets?.containerNames.map((c) => c.service)), new Set(remap.targets?.networks)),
|
|
233
|
+
...sharedHostPaths(composeFile, repo.mainRoot),
|
|
234
|
+
];
|
|
233
235
|
if (isolation.length === 0) {
|
|
234
236
|
add({
|
|
235
237
|
status: "ok",
|
|
@@ -241,6 +243,49 @@ export async function doctor(io) {
|
|
|
241
243
|
for (const w of isolation) {
|
|
242
244
|
add({ status: "warn", title: "Isolation", detail: w });
|
|
243
245
|
}
|
|
246
|
+
for (const w of hostPortsInEnvironment(composeFile, mappings)) {
|
|
247
|
+
add({
|
|
248
|
+
status: "warn",
|
|
249
|
+
title: "Host port written into a service's environment",
|
|
250
|
+
detail: w,
|
|
251
|
+
hint: "A worktree stack publishes another port. Write the port as a variable in the compose file (\"${VAR:-port}:port\" and ${VAR} in the value) so each stack gets its own.",
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
if (shared.size > 0) {
|
|
255
|
+
add({
|
|
256
|
+
status: "ok",
|
|
257
|
+
title: `${shared.size} shared service(s): worktree stacks use the main checkout's`,
|
|
258
|
+
detail: [...shared].join(", "),
|
|
259
|
+
hint: "They must be running in the main checkout (docker compose up -d …).",
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
// Untracked files the stack reads: git cannot bring them into a worktree, wtc copies them.
|
|
263
|
+
const needed = neededUntrackedFiles(repo.mainRoot, composeFile);
|
|
264
|
+
const copied = needed.filter((n) => !n.tooLarge && !n.data);
|
|
265
|
+
if (copied.length > 0) {
|
|
266
|
+
add({
|
|
267
|
+
status: "ok",
|
|
268
|
+
title: `${copied.length} untracked path(s) copied into each worktree`,
|
|
269
|
+
detail: copied.map((n) => n.rel).join(", "),
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
const data = needed.filter((n) => n.data);
|
|
273
|
+
if (data.length > 0) {
|
|
274
|
+
add({
|
|
275
|
+
status: "ok",
|
|
276
|
+
title: `${data.length} bind-mounted path(s) hold a service's data: each worktree starts with its own`,
|
|
277
|
+
detail: data.map((n) => n.rel).join(", "),
|
|
278
|
+
hint: 'To copy the main checkout\'s into each worktree anyway, list the path in "sync".',
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
for (const n of needed.filter((x) => x.tooLarge)) {
|
|
282
|
+
add({
|
|
283
|
+
status: "warn",
|
|
284
|
+
title: `${n.rel}/ is bind-mounted but untracked, and too large to copy unasked`,
|
|
285
|
+
detail: "A worktree stack starts with an empty directory there.",
|
|
286
|
+
hint: `Add "${n.rel}" to "sync" in .wtcrc.json to copy it into each worktree.`,
|
|
287
|
+
});
|
|
288
|
+
}
|
|
244
289
|
if (!config)
|
|
245
290
|
return finish();
|
|
246
291
|
// worktrees, registry, collisions, busy ports. Read-only: a diagnosis
|
|
@@ -248,24 +293,13 @@ export async function doctor(io) {
|
|
|
248
293
|
// worktree ids, or move a corrupt registry aside — it only reports.
|
|
249
294
|
let ctx;
|
|
250
295
|
try {
|
|
251
|
-
|
|
296
|
+
// resolveRemap: a worktree whose compose file differs from the main checkout's is read as start reads it.
|
|
297
|
+
ctx = buildContext({}, { cwd, readOnly: true, resolveRemap: composeWorks });
|
|
252
298
|
}
|
|
253
299
|
catch (err) {
|
|
254
300
|
add({ status: "fail", title: "Could not load worktrees", detail: message(err) });
|
|
255
301
|
return finish();
|
|
256
302
|
}
|
|
257
|
-
const indices = [
|
|
258
|
-
...ctx.worktrees.map((w) => w.index),
|
|
259
|
-
...ctx.orphans.map((o) => o.index),
|
|
260
|
-
...ctx.unavailable.map((u) => u.index),
|
|
261
|
-
];
|
|
262
|
-
const collisions = portCollisionWarnings(ctx.portMappings, ctx.portStride, indices, remapped);
|
|
263
|
-
if (collisions.length === 0) {
|
|
264
|
-
add({ status: "ok", title: "No cross-worktree port collisions" });
|
|
265
|
-
}
|
|
266
|
-
for (const w of collisions) {
|
|
267
|
-
add({ status: "warn", title: "Port collisions", detail: w });
|
|
268
|
-
}
|
|
269
303
|
// As start sees each worktree: its own compose override counts, and a
|
|
270
304
|
// port may have nowhere to move at that worktree's index.
|
|
271
305
|
const withRemap = { ...ctx, remap: remap.targets };
|
|
@@ -274,7 +308,10 @@ export async function doctor(io) {
|
|
|
274
308
|
let notes;
|
|
275
309
|
try {
|
|
276
310
|
const own = stackRemap(withRemap, wt.path, wt.project);
|
|
277
|
-
notes = [
|
|
311
|
+
notes = [
|
|
312
|
+
...own.warnings,
|
|
313
|
+
...stackAllocations(withRemap, { index: wt.index, dir: wt.path, project: wt.project }, own.targets).warnings,
|
|
314
|
+
];
|
|
278
315
|
}
|
|
279
316
|
catch {
|
|
280
317
|
continue; // reported by the busy-port check below
|
|
@@ -315,7 +352,7 @@ export async function doctor(io) {
|
|
|
315
352
|
let allocations;
|
|
316
353
|
try {
|
|
317
354
|
// As start allocates them: this worktree's own compose override counts.
|
|
318
|
-
allocations = stackAllocations(withRemap, wt.index, stackRemap(withRemap, wt.path, wt.project).targets).allocations;
|
|
355
|
+
allocations = stackAllocations(withRemap, { index: wt.index, dir: wt.path, project: wt.project }, stackRemap(withRemap, wt.path, wt.project).targets).allocations;
|
|
319
356
|
}
|
|
320
357
|
catch (err) {
|
|
321
358
|
add({ status: "fail", title: `Worktree ${wt.index}: cannot allocate ports`, detail: message(err) });
|
|
@@ -329,10 +366,10 @@ export async function doctor(io) {
|
|
|
329
366
|
}
|
|
330
367
|
if (busy.length > 0) {
|
|
331
368
|
add({
|
|
332
|
-
status: "
|
|
333
|
-
title: `${busy.length}
|
|
369
|
+
status: "ok",
|
|
370
|
+
title: `${busy.length} port(s) of stopped stacks are taken by something else`,
|
|
334
371
|
detail: busy.join(", "),
|
|
335
|
-
hint: "
|
|
372
|
+
hint: "The next 'wtc start' of that worktree moves them to free ports (and says so).",
|
|
336
373
|
});
|
|
337
374
|
}
|
|
338
375
|
else if (probed > 0) {
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { WorktreeSelector } from "../selectors.js";
|
|
2
|
+
import type { Reporter } from "../utils/log.js";
|
|
3
|
+
import type { CoreIo } from "./types.js";
|
|
4
|
+
export interface DownOptions {
|
|
5
|
+
/** Worktrees whose stack to remove; none: the current one. */
|
|
6
|
+
worktrees?: WorktreeSelector[];
|
|
7
|
+
/** Every worktree's stack. */
|
|
8
|
+
all?: boolean;
|
|
9
|
+
/** Keep the stack's named volumes (its data); only containers and networks go. */
|
|
10
|
+
keepVolumes?: boolean;
|
|
11
|
+
}
|
|
12
|
+
export interface DownResult {
|
|
13
|
+
removed: {
|
|
14
|
+
index: number;
|
|
15
|
+
branch: string;
|
|
16
|
+
project: string;
|
|
17
|
+
}[];
|
|
18
|
+
failed: {
|
|
19
|
+
index: number;
|
|
20
|
+
branch: string;
|
|
21
|
+
project: string;
|
|
22
|
+
error: string;
|
|
23
|
+
}[];
|
|
24
|
+
/** Stacks that could not be removed (non-zero → exit 1 / MCP isError). */
|
|
25
|
+
failures: number;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Remove the stacks of the selected worktrees: containers, networks and —
|
|
29
|
+
* unless `keepVolumes` — volumes (`compose down -v`). The worktrees, their
|
|
30
|
+
* indices and their ports stay, so the next `start` brings up a fresh
|
|
31
|
+
* stack in the same place. This is how one worktree's stack is taken down
|
|
32
|
+
* without touching anyone else's: `clean` ends every worktree's.
|
|
33
|
+
*
|
|
34
|
+
* From the main checkout nothing is assumed: the worktrees must be named,
|
|
35
|
+
* or `all` given.
|
|
36
|
+
*/
|
|
37
|
+
export declare function downWorktrees(options: DownOptions, reporter: Reporter, io: CoreIo): Promise<DownResult>;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { buildContext } from "../context.js";
|
|
2
|
+
import { implicitScopeNote, selectScope } from "../selectors.js";
|
|
3
|
+
import { reportContextWarnings, throwIfAborted } from "./compose.js";
|
|
4
|
+
import { teardownStack } from "./teardown.js";
|
|
5
|
+
/**
|
|
6
|
+
* Remove the stacks of the selected worktrees: containers, networks and —
|
|
7
|
+
* unless `keepVolumes` — volumes (`compose down -v`). The worktrees, their
|
|
8
|
+
* indices and their ports stay, so the next `start` brings up a fresh
|
|
9
|
+
* stack in the same place. This is how one worktree's stack is taken down
|
|
10
|
+
* without touching anyone else's: `clean` ends every worktree's.
|
|
11
|
+
*
|
|
12
|
+
* From the main checkout nothing is assumed: the worktrees must be named,
|
|
13
|
+
* or `all` given.
|
|
14
|
+
*/
|
|
15
|
+
export async function downWorktrees(options, reporter, io) {
|
|
16
|
+
// A stack is torn down by its project even when its compose file is gone.
|
|
17
|
+
const ctx = buildContext({}, { cwd: io.cwd, probeStacks: true, allowMissingCompose: true });
|
|
18
|
+
reportContextWarnings(ctx, reporter, { isolation: false });
|
|
19
|
+
const result = { removed: [], failed: [], failures: 0 };
|
|
20
|
+
if (ctx.worktrees.length === 0) {
|
|
21
|
+
reporter.warn("No worktrees to take down.");
|
|
22
|
+
return result;
|
|
23
|
+
}
|
|
24
|
+
const scope = selectScope(ctx.worktrees, options.worktrees, ctx.cwd, options.all);
|
|
25
|
+
if (!scope.implicit && !options.all && !(options.worktrees && options.worktrees.length > 0)) {
|
|
26
|
+
throw new Error(`Name the worktrees whose stack to remove (index, branch, directory, path), or pass --all for every worktree. ` +
|
|
27
|
+
(options.keepVolumes ? `Its volumes are kept.` : `Removing a stack deletes its volumes (its data).`));
|
|
28
|
+
}
|
|
29
|
+
if (scope.implicit)
|
|
30
|
+
reporter.info(implicitScopeNote(scope.implicit));
|
|
31
|
+
const engine = ctx.getEngine();
|
|
32
|
+
for (const wt of scope.targets) {
|
|
33
|
+
throwIfAborted(io);
|
|
34
|
+
const entry = { index: wt.index, branch: wt.branch, project: wt.project };
|
|
35
|
+
reporter.info(`Removing ${wt.project}${options.keepVolumes ? " (keeping its volumes)" : " and its volumes"}...`);
|
|
36
|
+
const outcome = await teardownStack(ctx, engine, { project: wt.project, index: wt.index, path: wt.path }, io, {
|
|
37
|
+
keepVolumes: options.keepVolumes,
|
|
38
|
+
});
|
|
39
|
+
if (outcome.ok) {
|
|
40
|
+
reporter.success(`Removed the stack of worktree ${wt.index} (${wt.branch})`);
|
|
41
|
+
if (outcome.leftNetworks?.length) {
|
|
42
|
+
reporter.warn(`${outcome.leftNetworks.join(", ")} ${outcome.leftNetworks.length === 1 ? "is" : "are"} still there: a container that is not this stack's is attached, and wtc did not connect it. ` +
|
|
43
|
+
`Disconnect it, then run 'wtc down' again.`);
|
|
44
|
+
}
|
|
45
|
+
result.removed.push(entry);
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
reporter.warn(`Could not remove ${wt.project}: ${outcome.error}`);
|
|
49
|
+
result.failed.push({ ...entry, error: outcome.error ?? "unknown error" });
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
result.failures = result.failed.length;
|
|
53
|
+
return result;
|
|
54
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import type { ComposeEngine } from "../compose/engine.js";
|
|
2
|
+
import type { PsContainer } from "../compose/status.js";
|
|
3
|
+
import type { ComposeService } from "../compose/types.js";
|
|
4
|
+
import type { ComposeCallEnv } from "./compose.js";
|
|
5
|
+
/** One service of a stack, as the engine reports its containers. */
|
|
6
|
+
export interface ServiceState {
|
|
7
|
+
service: string;
|
|
8
|
+
/**
|
|
9
|
+
* `running`, `exited`, `created`, `restarting`, `paused`, `dead` — the
|
|
10
|
+
* least settled state among the service's containers.
|
|
11
|
+
*/
|
|
12
|
+
state: string;
|
|
13
|
+
/** `healthy`, `unhealthy`, `starting`, or null without a healthcheck. */
|
|
14
|
+
health: PsContainer["health"];
|
|
15
|
+
/** Exit code when the service's container has exited. */
|
|
16
|
+
exitCode: number | null;
|
|
17
|
+
/** Host ports its containers publish. */
|
|
18
|
+
ports: number[];
|
|
19
|
+
}
|
|
20
|
+
/** What a service's state means for "is the stack ready?". */
|
|
21
|
+
export type ServiceVerdict = "ready" | "pending" | "failed";
|
|
22
|
+
export declare function serviceVerdict(s: ServiceState): ServiceVerdict;
|
|
23
|
+
/**
|
|
24
|
+
* The services `names` and everything they depend on, as `compose up
|
|
25
|
+
* <names>` brings them up — or null for "every service" (no names).
|
|
26
|
+
* `left` are services the stack does not run (the shared ones): they are
|
|
27
|
+
* not part of it, and neither is what only they depend on.
|
|
28
|
+
*/
|
|
29
|
+
export declare function serviceClosure(services: Pick<ComposeService, "name" | "dependsOn">[], names: readonly string[] | null | undefined, left?: ReadonlySet<string>): Set<string> | null;
|
|
30
|
+
/**
|
|
31
|
+
* The services a stack is expected to have a container for: the started
|
|
32
|
+
* selection (with what it depends on), or every service compose starts by
|
|
33
|
+
* itself — not a shared one, not one behind a profile, not one that never
|
|
34
|
+
* has a container.
|
|
35
|
+
*/
|
|
36
|
+
export declare function expectedServices(services: Pick<ComposeService, "name" | "dependsOn" | "profiles" | "noContainer">[], selection: readonly string[] | null | undefined, shared?: ReadonlySet<string>): string[];
|
|
37
|
+
/**
|
|
38
|
+
* The services of `project`, from a container listing (one-off `compose
|
|
39
|
+
* run` containers left out). With `only`, just those services: a stack
|
|
40
|
+
* started with a selection still has the stopped containers of the
|
|
41
|
+
* services left out, and their exit codes say nothing about this start.
|
|
42
|
+
*/
|
|
43
|
+
export declare function stackServices(containers: PsContainer[], project: string, only?: ReadonlySet<string> | null): ServiceState[];
|
|
44
|
+
/** `web: unhealthy`, `seed: exited (1)`, `api: starting`. */
|
|
45
|
+
export declare function describeService(s: ServiceState): string;
|
|
46
|
+
export interface Readiness {
|
|
47
|
+
ready: boolean;
|
|
48
|
+
services: ServiceState[];
|
|
49
|
+
failed: ServiceState[];
|
|
50
|
+
pending: ServiceState[];
|
|
51
|
+
}
|
|
52
|
+
export declare function readiness(services: ServiceState[]): Readiness;
|
|
53
|
+
export interface WaitOptions {
|
|
54
|
+
timeoutMs: number;
|
|
55
|
+
signal?: AbortSignal;
|
|
56
|
+
/** Called when the set of services still starting changes. */
|
|
57
|
+
onProgress?: (message: string) => void;
|
|
58
|
+
/** Lists the engine's containers (tests replace it). */
|
|
59
|
+
list?: () => PsContainer[];
|
|
60
|
+
pollMs?: number;
|
|
61
|
+
/** Judge only these services (see `stackServices`). */
|
|
62
|
+
only?: ReadonlySet<string> | null;
|
|
63
|
+
/** Services that must have a container: one that has none is still coming, not ready. */
|
|
64
|
+
expected?: readonly string[];
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Wait until every service of the stack is ready: running (and healthy,
|
|
68
|
+
* where it has a healthcheck), or finished with exit code 0. Returns as
|
|
69
|
+
* soon as one has failed — unhealthy, or exited with an error — or when
|
|
70
|
+
* the time is up; the result says which services are not ready.
|
|
71
|
+
*/
|
|
72
|
+
export declare function waitForStack(engine: ComposeEngine, project: string, opts: WaitOptions): Promise<Readiness & {
|
|
73
|
+
timedOut: boolean;
|
|
74
|
+
}>;
|
|
75
|
+
export interface LogCall {
|
|
76
|
+
cwd: string;
|
|
77
|
+
env?: ComposeCallEnv;
|
|
78
|
+
signal?: AbortSignal;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* The last lines each of `services` logged, as an indented block per
|
|
82
|
+
* service — what a person (or an agent) needs to see why a stack did not
|
|
83
|
+
* come up, without a second command. Empty when nothing could be read.
|
|
84
|
+
*/
|
|
85
|
+
export declare function serviceLogTails(engine: ComposeEngine, project: string, services: string[], call: LogCall, lines?: number): Promise<string>;
|
|
86
|
+
/**
|
|
87
|
+
* Why a stack is not up, in a few lines: each service that failed or is
|
|
88
|
+
* still starting, then the failed ones' last log lines.
|
|
89
|
+
*/
|
|
90
|
+
export declare function diagnoseStack(engine: ComposeEngine, project: string, call: LogCall, list?: () => PsContainer[], only?: ReadonlySet<string> | null): Promise<string>;
|