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.
Files changed (77) hide show
  1. package/LICENSE +2 -2
  2. package/README.md +66 -720
  3. package/SKILL.md +73 -76
  4. package/dist/cli.js +85 -10
  5. package/dist/commands/down.d.ts +2 -0
  6. package/dist/commands/down.js +7 -0
  7. package/dist/commands/list.d.ts +2 -0
  8. package/dist/commands/list.js +43 -6
  9. package/dist/commands/passthrough.d.ts +13 -0
  10. package/dist/commands/passthrough.js +27 -0
  11. package/dist/commands/stop.d.ts +3 -1
  12. package/dist/commands/stop.js +2 -2
  13. package/dist/compose/detect.d.ts +19 -0
  14. package/dist/compose/detect.js +56 -0
  15. package/dist/compose/isolation.d.ts +20 -7
  16. package/dist/compose/isolation.js +88 -36
  17. package/dist/compose/override.d.ts +54 -4
  18. package/dist/compose/override.js +179 -52
  19. package/dist/compose/parse.d.ts +11 -0
  20. package/dist/compose/parse.js +260 -13
  21. package/dist/compose/status.d.ts +14 -0
  22. package/dist/compose/status.js +58 -1
  23. package/dist/compose/types.d.ts +56 -0
  24. package/dist/config.d.ts +33 -0
  25. package/dist/context.d.ts +26 -0
  26. package/dist/context.js +140 -29
  27. package/dist/core/clean.d.ts +3 -1
  28. package/dist/core/clean.js +8 -3
  29. package/dist/core/compose.d.ts +87 -13
  30. package/dist/core/compose.js +282 -57
  31. package/dist/core/doctor.d.ts +2 -8
  32. package/dist/core/doctor.js +116 -79
  33. package/dist/core/down.d.ts +37 -0
  34. package/dist/core/down.js +54 -0
  35. package/dist/core/health.d.ts +90 -0
  36. package/dist/core/health.js +198 -0
  37. package/dist/core/list.d.ts +53 -6
  38. package/dist/core/list.js +144 -58
  39. package/dist/core/passthrough.d.ts +69 -0
  40. package/dist/core/passthrough.js +216 -0
  41. package/dist/core/promote.d.ts +4 -2
  42. package/dist/core/promote.js +5 -3
  43. package/dist/core/restart.d.ts +3 -3
  44. package/dist/core/restart.js +6 -5
  45. package/dist/core/seed.d.ts +50 -0
  46. package/dist/core/seed.js +245 -0
  47. package/dist/core/shared.d.ts +63 -0
  48. package/dist/core/shared.js +194 -0
  49. package/dist/core/start.d.ts +32 -5
  50. package/dist/core/start.js +297 -49
  51. package/dist/core/stop.d.ts +3 -1
  52. package/dist/core/stop.js +6 -3
  53. package/dist/core/teardown.d.ts +5 -2
  54. package/dist/core/teardown.js +33 -8
  55. package/dist/core/types.d.ts +2 -0
  56. package/dist/git/promote.d.ts +11 -3
  57. package/dist/git/promote.js +19 -5
  58. package/dist/mcp/server.js +154 -28
  59. package/dist/ports/allocate.d.ts +35 -23
  60. package/dist/ports/allocate.js +108 -58
  61. package/dist/ports/probe.d.ts +11 -0
  62. package/dist/ports/probe.js +40 -0
  63. package/dist/ports/types.d.ts +5 -0
  64. package/dist/selectors.d.ts +17 -0
  65. package/dist/selectors.js +19 -0
  66. package/dist/state/registry.d.ts +23 -3
  67. package/dist/state/registry.js +57 -17
  68. package/dist/sync/env.d.ts +42 -2
  69. package/dist/sync/env.js +79 -8
  70. package/dist/sync/envsync.d.ts +91 -0
  71. package/dist/sync/envsync.js +338 -0
  72. package/dist/sync/files.d.ts +87 -5
  73. package/dist/sync/files.js +343 -18
  74. package/dist/utils/paths.d.ts +2 -0
  75. package/dist/utils/paths.js +9 -0
  76. package/package.json +9 -2
  77. package/wtc.schema.json +155 -13
@@ -0,0 +1,194 @@
1
+ import { ENGINE_PROBE_TIMEOUT_MS, CommandError, run, runSafe } from "../utils/exec.js";
2
+ import { cliCall } from "../compose/engine.js";
3
+ import { field, labelsOf, listContainers, parseJsonListing } from "../compose/status.js";
4
+ import { samePath } from "../git/worktree.js";
5
+ import { expectedMainProjects } from "./seed.js";
6
+ /**
7
+ * Shared services. A heavy stack — a search cluster, a queue, an analytics
8
+ * database — is too much to run once per worktree. A service listed in the
9
+ * config's `shared` runs once, in the main checkout's stack; each worktree
10
+ * stack leaves it out and reaches the main one under the same name:
11
+ *
12
+ * - in the worktree stack the service gets a profile nothing enables, and
13
+ * is dropped from every `depends_on`, so compose neither starts it nor
14
+ * waits for it;
15
+ * - after `up`, the main checkout's container is connected to the worktree
16
+ * stack's networks with the service name (and its `container_name`) as
17
+ * aliases, so `kafka:9092` in a worktree container is the shared broker;
18
+ * - before the stack's networks are removed, it is disconnected again.
19
+ *
20
+ * Whatever lives in a shared service is shared too: that is the point,
21
+ * and the reason a database usually stays per-worktree.
22
+ */
23
+ /** What a stack's override needs for the shared services its compose file declares. */
24
+ export function sharedExtras(composeFile, shared) {
25
+ const declared = composeFile.services.filter((s) => shared.has(s.name)).map((s) => s.name);
26
+ if (declared.length === 0)
27
+ return {};
28
+ const dependsOn = {};
29
+ for (const svc of composeFile.services) {
30
+ if (shared.has(svc.name) || !svc.dependsOn)
31
+ continue;
32
+ if (!Object.keys(svc.dependsOn).some((dep) => shared.has(dep)))
33
+ continue;
34
+ dependsOn[svc.name] = Object.fromEntries(Object.entries(svc.dependsOn).filter(([dep]) => !shared.has(dep)));
35
+ }
36
+ return { shared: declared, ...(Object.keys(dependsOn).length > 0 ? { dependsOn } : {}) };
37
+ }
38
+ /** Running containers of the main checkout's own stack, by service. */
39
+ export function mainStackContainers(containers, mainDirs, worktreeProjects) {
40
+ const out = new Map();
41
+ for (const c of containers) {
42
+ if (!c.running || c.oneoff || !c.service || !c.project || !c.workingDir)
43
+ continue;
44
+ if (worktreeProjects.has(c.project))
45
+ continue;
46
+ if (!mainDirs.some((dir) => samePath(dir, c.workingDir)))
47
+ continue;
48
+ out.set(c.service, [...(out.get(c.service) ?? []), c]);
49
+ }
50
+ return out;
51
+ }
52
+ /**
53
+ * The stack's networks by the key the compose file gives them, as the
54
+ * engine reports them (compose labels each network with its project and
55
+ * key): a network may carry a name of its own (`name:
56
+ * ${COMPOSE_PROJECT_NAME}-backend`). Empty when the listing fails.
57
+ */
58
+ export function stackNetworkNames(engine, project) {
59
+ const out = new Map();
60
+ const [file, args] = cliCall(engine, "network", "ls", "--filter", `label=com.docker.compose.project=${project}`, "--format", "json");
61
+ try {
62
+ for (const network of parseJsonListing(run(file, args, { timeoutMs: ENGINE_PROBE_TIMEOUT_MS }))) {
63
+ const key = labelsOf(network)["com.docker.compose.network"];
64
+ const name = String(field(network, "Name") ?? "");
65
+ if (key && name)
66
+ out.set(key, name);
67
+ }
68
+ }
69
+ catch {
70
+ // Unknown: the default names are tried instead.
71
+ }
72
+ return out;
73
+ }
74
+ /** The stack's own networks a service joins, by their real names (`<project>_<key>` unless the engine says otherwise). */
75
+ function stackNetworks(composeFile, service, project, names) {
76
+ const svc = composeFile.services.find((s) => s.name === service);
77
+ const fixed = new Set(composeFile.sharedNetworks.map((n) => n.key));
78
+ const keys = svc?.networks && svc.networks.length > 0 ? svc.networks : ["default"];
79
+ // A network with a fixed name is one every stack is already on, the main checkout's included.
80
+ return keys.filter((key) => !fixed.has(key)).map((key) => names.get(key) ?? `${project}_${key}`);
81
+ }
82
+ /**
83
+ * Connect the main checkout's shared containers to a worktree stack's
84
+ * networks. Returns what was linked and a warning for each shared service
85
+ * the stack cannot reach.
86
+ */
87
+ export function linkSharedServices(input) {
88
+ const linked = [];
89
+ const warnings = [];
90
+ const declared = input.composeFile.services.filter((s) => input.shared.has(s.name));
91
+ if (declared.length === 0)
92
+ return { linked, warnings };
93
+ let running;
94
+ try {
95
+ running = mainStackContainers((input.list ?? (() => listContainers(input.engine)))(), input.mainDirs, input.worktreeProjects);
96
+ }
97
+ catch (err) {
98
+ return {
99
+ linked,
100
+ warnings: [`Could not look for the shared services' containers: ${err instanceof Error ? err.message.split("\n")[0] : String(err)}`],
101
+ };
102
+ }
103
+ const networkNames = stackNetworkNames(input.engine, input.project);
104
+ for (const svc of declared) {
105
+ let containers = running.get(svc.name) ?? [];
106
+ // Two stacks run from the main checkout's directory, both with this service: only the
107
+ // main stack's is the shared one. Without a way to tell, none is connected.
108
+ const projects = [...new Set(containers.map((c) => c.project))];
109
+ if (projects.length > 1) {
110
+ const main = expectedMainProjects(input.mainDirs, input.mainProjectName).find((n) => projects.includes(n));
111
+ if (!main) {
112
+ warnings.push(`Shared service "${svc.name}" runs in several stacks started from the main checkout (${projects.join(", ")}): ` +
113
+ `wtc does not guess which one this stack should use, so it is not connected. Stop the others, then run 'wtc start' again.`);
114
+ continue;
115
+ }
116
+ containers = containers.filter((c) => c.project === main);
117
+ }
118
+ if (containers.length === 0) {
119
+ warnings.push(`Shared service "${svc.name}" is not running in the main checkout, so this stack cannot reach it. ` +
120
+ `Start it there (docker compose up -d ${svc.name}), then run 'wtc start' again.`);
121
+ continue;
122
+ }
123
+ const aliases = [...new Set([svc.name, ...(svc.containerName && !svc.containerName.includes("$") ? [svc.containerName] : [])])];
124
+ const networks = stackNetworks(input.composeFile, svc.name, input.project, networkNames);
125
+ let connected = 0;
126
+ for (const network of networks) {
127
+ for (const container of containers) {
128
+ const [file, args] = cliCall(input.engine, "network", "connect", ...aliases.flatMap((a) => ["--alias", a]), network, container.id);
129
+ try {
130
+ run(file, args, { timeoutMs: ENGINE_PROBE_TIMEOUT_MS });
131
+ connected++;
132
+ }
133
+ catch (err) {
134
+ const message = err instanceof CommandError ? err.message : String(err);
135
+ // Already connected (an earlier start): nothing to do.
136
+ if (/already exists|already connected|is already attached/i.test(message)) {
137
+ connected++;
138
+ continue;
139
+ }
140
+ // A network none of the started services uses does not exist.
141
+ if (/not found|no such network/i.test(message))
142
+ continue;
143
+ warnings.push(`Could not connect shared service "${svc.name}" to ${network}: ${message.split("\n").slice(-1)[0]}`);
144
+ }
145
+ }
146
+ }
147
+ if (connected > 0) {
148
+ linked.push(svc.name);
149
+ }
150
+ else if (networks.length > 0 && !warnings.some((w) => w.includes(`"${svc.name}"`))) {
151
+ warnings.push(`Shared service "${svc.name}" could not be connected to this stack: none of its networks (${networks.join(", ")}) exists. Is a service that uses them running here?`);
152
+ }
153
+ }
154
+ return { linked, warnings };
155
+ }
156
+ /**
157
+ * Disconnect the main checkout's containers from a stack's networks, so
158
+ * the networks can be removed with the stack: the shared containers
159
+ * `linkSharedServices` connected. A container of any other project stays
160
+ * as it is — wtc did not connect it, and the engine then says what holds
161
+ * the network. Best effort; returns how many connections were removed.
162
+ */
163
+ export function unlinkSharedServices(engine, project, mainDirs) {
164
+ let removed = 0;
165
+ const opts = { timeoutMs: ENGINE_PROBE_TIMEOUT_MS };
166
+ const [lsFile, lsArgs] = cliCall(engine, "network", "ls", "--filter", `label=com.docker.compose.project=${project}`, "--format", "{{.Name}}");
167
+ const networks = (runSafe(lsFile, lsArgs, opts) ?? "").split("\n").map((l) => l.trim()).filter(Boolean);
168
+ if (networks.length === 0)
169
+ return removed;
170
+ let containers;
171
+ try {
172
+ containers = listContainers(engine);
173
+ }
174
+ catch {
175
+ return removed;
176
+ }
177
+ const foreign = containers.filter((c) => c.running &&
178
+ c.project !== null &&
179
+ c.project !== project &&
180
+ !!c.workingDir &&
181
+ mainDirs.some((dir) => samePath(dir, c.workingDir)));
182
+ for (const network of networks) {
183
+ const [inspectFile, inspectArgs] = cliCall(engine, "network", "inspect", network, "--format", "{{range $id, $c := .Containers}}{{$id}}\n{{end}}");
184
+ const attached = new Set((runSafe(inspectFile, inspectArgs, opts) ?? "").split("\n").map((l) => l.trim()).filter(Boolean));
185
+ for (const c of foreign) {
186
+ if (![...attached].some((id) => id.startsWith(c.id) || c.id.startsWith(id)))
187
+ continue;
188
+ const [file, args] = cliCall(engine, "network", "disconnect", "-f", network, c.id);
189
+ if (runSafe(file, args, opts) !== null)
190
+ removed++;
191
+ }
192
+ }
193
+ return removed;
194
+ }
@@ -2,9 +2,13 @@ import type { EnvCliOptions } from "../context.js";
2
2
  import type { WorktreeSelector } from "../selectors.js";
3
3
  import type { Reporter } from "../utils/log.js";
4
4
  import type { CoreIo } from "./types.js";
5
+ /** How long `--wait` waits for a stack unless told otherwise. */
6
+ export declare const DEFAULT_WAIT_SECONDS = 300;
5
7
  export interface StartOptions extends EnvCliOptions {
6
- /** Worktrees to start (index, branch, dir name, path, "."); empty = all. */
8
+ /** Worktrees to start (index, branch, dir name, path, "."); none: the current one, or all from the main checkout. */
7
9
  worktrees?: WorktreeSelector[];
10
+ /** Every worktree, even when run from inside one. */
11
+ all?: boolean;
8
12
  /** Pass `--build` to `compose up` (default true). */
9
13
  build?: boolean;
10
14
  /**
@@ -13,6 +17,20 @@ export interface StartOptions extends EnvCliOptions {
13
17
  * running — and holding their ports — after the restart.
14
18
  */
15
19
  removeOrphans?: boolean;
20
+ /**
21
+ * Services to bring up (with what they depend on) instead of all of
22
+ * them. Remembered per worktree: the next start or restart without this
23
+ * option uses the same ones.
24
+ */
25
+ services?: string[];
26
+ /** Bring up every service again, forgetting a remembered selection. */
27
+ allServices?: boolean;
28
+ /** Return only once every service is running and healthy (or has finished cleanly). */
29
+ wait?: boolean;
30
+ /** Seconds `wait` gives the stack (default 300). */
31
+ waitTimeoutSeconds?: number;
32
+ /** Copy the main checkout stack's volumes into a stack that has none yet (overrides `seedVolumes`). */
33
+ seed?: boolean;
16
34
  }
17
35
  export interface StartedWorktree {
18
36
  index: number;
@@ -20,14 +38,23 @@ export interface StartedWorktree {
20
38
  path: string;
21
39
  project: string;
22
40
  ports: Record<string, number>;
41
+ /** Services the start was limited to (absent: all of them). */
42
+ services?: string[];
43
+ /** With `wait`: every service is ready. */
44
+ ready?: boolean;
23
45
  }
24
46
  export interface StartResult {
25
47
  started: StartedWorktree[];
26
48
  }
27
49
  /**
28
- * Sync infra files, inject ports + project name into env files, and run
29
- * `compose up -d` for each selected worktree (with the stack's runtime
30
- * override when hardcoded ports or container names are remapped). Stops at
31
- * the first failure.
50
+ * Fail on a service selection a target's compose file cannot satisfy,
51
+ * without touching anything: `restart` asks before it stops the stacks.
52
+ */
53
+ export declare function preflightServices(options: StartOptions, io: CoreIo): void;
54
+ /**
55
+ * Sync infra files, settle the stack's ports, bring its env files up to
56
+ * date, and run `compose up -d` for each selected worktree (with the
57
+ * stack's runtime override when hardcoded settings are remapped). Stops at
58
+ * the first failure, saying which services failed and what they logged.
32
59
  */
33
60
  export declare function startWorktrees(options: StartOptions, reporter: Reporter, io: CoreIo): Promise<StartResult>;