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
@@ -30,6 +30,15 @@ export interface WorktreeRecord {
30
30
  id?: string;
31
31
  /** Repo-relative (`/`-separated) env files wtc wrote into the worktree. */
32
32
  envFiles?: string[];
33
+ /**
34
+ * Host ports the stack holds, by port label (the variable, or
35
+ * `<service>:<default port>` for a hardcoded port). Assigned at the first
36
+ * start and kept: a port only moves when something else has taken it
37
+ * while the stack was not running.
38
+ */
39
+ ports?: Record<string, number>;
40
+ /** Services the last `start` was limited to (absent: every service). */
41
+ services?: string[];
33
42
  }
34
43
  export interface ResolvedWorktree extends WorktreeInfo {
35
44
  index: number;
@@ -38,9 +47,13 @@ export interface ResolvedWorktree extends WorktreeInfo {
38
47
  id?: string;
39
48
  /** Env files wtc wrote into the worktree (see WorktreeRecord). */
40
49
  envFiles?: string[];
50
+ /** Host ports the stack holds (see WorktreeRecord). */
51
+ ports?: Record<string, number>;
52
+ /** Services the last `start` was limited to (see WorktreeRecord). */
53
+ services?: string[];
41
54
  }
42
55
  export interface StateFile {
43
- version: 1;
56
+ version: number;
44
57
  worktrees: WorktreeRecord[];
45
58
  /**
46
59
  * Projects of records wtc forgot (pruned or cleaned), for diagnosis. Never
@@ -54,7 +67,13 @@ export interface StateFile {
54
67
  */
55
68
  mainCheckout?: string;
56
69
  }
57
- export declare const STATE_VERSION = 1;
70
+ /**
71
+ * 2: records may hold `ports` and `services`. A wtc that only knows version
72
+ * 1 would compute other ports for the same stack, so it must refuse the
73
+ * file (NewerStateError) rather than share it. Version 1 files are read
74
+ * as they are and written back as version 2.
75
+ */
76
+ export declare const STATE_VERSION = 2;
58
77
  export declare function stateDir(commonDir: string): string;
59
78
  export declare function stateFile(commonDir: string): string;
60
79
  /**
@@ -189,6 +208,7 @@ export declare function forgetRecords(commonDir: string, projects: string[]): vo
189
208
  * relative to the worktree), merged with — and deduplicated against — those
190
209
  * already recorded. Stored repo-relative with `/` separators. Clean treats
191
210
  * exactly these as wtc's own changes, so they never make a worktree dirty.
192
- * A worktree without a record (registry not persisted yet) is skipped.
193
211
  */
194
212
  export declare function recordEnvFiles(commonDir: string, wtPath: string, files: string[]): void;
213
+ /** Remember which services `start` was limited to for the worktree at `wtPath` (null: all of them). */
214
+ export declare function recordServices(commonDir: string, wtPath: string, services: string[] | null): void;
@@ -4,7 +4,14 @@ import path from "node:path";
4
4
  import { composeProjectName } from "../utils/sanitize.js";
5
5
  import { escapesRoot } from "../utils/paths.js";
6
6
  import { isCandidate, worktreeAdminDir } from "../git/worktree.js";
7
- export const STATE_VERSION = 1;
7
+ /**
8
+ * 2: records may hold `ports` and `services`. A wtc that only knows version
9
+ * 1 would compute other ports for the same stack, so it must refuse the
10
+ * file (NewerStateError) rather than share it. Version 1 files are read
11
+ * as they are and written back as version 2.
12
+ */
13
+ export const STATE_VERSION = 2;
14
+ const READABLE_VERSIONS = [1, 2];
8
15
  const LOCK_RETRY_MS = 50;
9
16
  const LOCK_TIMEOUT_MS = 10_000;
10
17
  /**
@@ -167,7 +174,15 @@ function isRecord(value) {
167
174
  r.index > 0 &&
168
175
  (r.id === undefined || typeof r.id === "string") &&
169
176
  (r.envFiles === undefined ||
170
- (Array.isArray(r.envFiles) && r.envFiles.every((f) => typeof f === "string"))));
177
+ (Array.isArray(r.envFiles) && r.envFiles.every((f) => typeof f === "string"))) &&
178
+ (r.ports === undefined || isPortMap(r.ports)) &&
179
+ (r.services === undefined ||
180
+ (Array.isArray(r.services) && r.services.every((f) => typeof f === "string"))));
181
+ }
182
+ function isPortMap(value) {
183
+ if (!value || typeof value !== "object" || Array.isArray(value))
184
+ return false;
185
+ return Object.values(value).every((p) => Number.isInteger(p) && p > 0 && p <= 65535);
171
186
  }
172
187
  /** Thrown for a state file written by a newer wtc: never discard it. */
173
188
  export class NewerStateError extends Error {
@@ -196,7 +211,7 @@ function parseState(file, text) {
196
211
  // (e.g. a cached MCP server). Resetting it would renumber every stack.
197
212
  throw new NewerStateError(file, d.version);
198
213
  }
199
- if (d.version !== STATE_VERSION) {
214
+ if (!READABLE_VERSIONS.includes(d.version)) {
200
215
  return { ok: false, problem: `unsupported state version ${JSON.stringify(d.version)}` };
201
216
  }
202
217
  if (!Array.isArray(d.worktrees)) {
@@ -480,6 +495,8 @@ function assignIdentities(opts, listed, state, warnings, persist) {
480
495
  project: record.project,
481
496
  id: record.id,
482
497
  envFiles: record.envFiles,
498
+ ports: record.ports,
499
+ services: record.services,
483
500
  });
484
501
  }
485
502
  let mainCheckout = state.mainCheckout;
@@ -552,22 +569,14 @@ export function forgetRecords(commonDir, projects) {
552
569
  });
553
570
  }
554
571
  /**
555
- * Remember env files wtc wrote into the worktree at `wtPath` (absolute, or
556
- * relative to the worktree), merged with — and deduplicated against — those
557
- * already recorded. Stored repo-relative with `/` separators. Clean treats
558
- * exactly these as wtc's own changes, so they never make a worktree dirty.
559
- * A worktree without a record (registry not persisted yet) is skipped.
572
+ * Change the record of the worktree at `wtPath` under the lock. `change`
573
+ * returns true when it modified the record. A worktree without a record
574
+ * (registry not persisted yet) is skipped.
560
575
  */
561
- export function recordEnvFiles(commonDir, wtPath, files) {
576
+ function updateRecord(commonDir, wtPath, change) {
562
577
  if (!fs.existsSync(stateFile(commonDir)))
563
578
  return;
564
579
  const root = path.resolve(wtPath);
565
- const rel = files
566
- .map((f) => (path.isAbsolute(f) ? path.relative(root, f) : path.normalize(f)))
567
- .filter((f) => f !== "" && f !== "." && !escapesRoot(f))
568
- .map((f) => f.split(path.sep).join("/"));
569
- if (rel.length === 0)
570
- return;
571
580
  // Identify the worktree before locking (may ask git).
572
581
  const adminDir = worktreeAdminDir(root);
573
582
  const wt = {
@@ -586,10 +595,41 @@ export function recordEnvFiles(commonDir, wtPath, files) {
586
595
  const record = state?.worktrees.find((r) => isRecordOf(r, wt));
587
596
  if (!state || !record)
588
597
  return;
598
+ if (change(record))
599
+ writeState(commonDir, state);
600
+ });
601
+ }
602
+ /**
603
+ * Remember env files wtc wrote into the worktree at `wtPath` (absolute, or
604
+ * relative to the worktree), merged with — and deduplicated against — those
605
+ * already recorded. Stored repo-relative with `/` separators. Clean treats
606
+ * exactly these as wtc's own changes, so they never make a worktree dirty.
607
+ */
608
+ export function recordEnvFiles(commonDir, wtPath, files) {
609
+ const root = path.resolve(wtPath);
610
+ const rel = files
611
+ .map((f) => (path.isAbsolute(f) ? path.relative(root, f) : path.normalize(f)))
612
+ .filter((f) => f !== "" && f !== "." && !escapesRoot(f))
613
+ .map((f) => f.split(path.sep).join("/"));
614
+ if (rel.length === 0)
615
+ return;
616
+ updateRecord(commonDir, wtPath, (record) => {
589
617
  const merged = [...new Set([...(record.envFiles ?? []), ...rel])];
590
618
  if (record.envFiles && merged.length === record.envFiles.length)
591
- return;
619
+ return false;
592
620
  record.envFiles = merged;
593
- writeState(commonDir, state);
621
+ return true;
622
+ });
623
+ }
624
+ /** Remember which services `start` was limited to for the worktree at `wtPath` (null: all of them). */
625
+ export function recordServices(commonDir, wtPath, services) {
626
+ updateRecord(commonDir, wtPath, (record) => {
627
+ if (JSON.stringify(record.services ?? null) === JSON.stringify(services))
628
+ return false;
629
+ if (services)
630
+ record.services = services;
631
+ else
632
+ delete record.services;
633
+ return true;
594
634
  });
595
635
  }
@@ -10,12 +10,39 @@ export interface EnvTarget {
10
10
  envFileName: string;
11
11
  /** Per-target overrides; `${VAR}` interpolates allocated port values. */
12
12
  overrides?: Record<string, string>;
13
+ /**
14
+ * Compose reads this file by itself (the `.env` next to the compose
15
+ * file): it also gets the project name and compose file list, so a plain
16
+ * `docker compose` run from there addresses the worktree's stack.
17
+ */
18
+ compose?: boolean;
13
19
  }
20
+ /** Whether an env file's content carries the block wtc appends. */
21
+ export declare function hasOverrideBlock(content: string): boolean;
14
22
  export declare function stripOverrideBlock(content: string): string;
15
23
  /** Name of the compose project variable wtc writes into the root env file. */
16
24
  export declare const PROJECT_NAME_VAR = "COMPOSE_PROJECT_NAME";
17
25
  /** Interpolate `${VAR}` placeholders with allocated port values (and the project name). */
18
26
  export declare function resolveOverrideValue(value: string, values: Map<string, number | string>): string;
27
+ /**
28
+ * `${VAR}` placeholders an override value still holds after interpolation
29
+ * that look like one of wtc's (`…_PORT`, `WTC_…`): most likely a typo or a
30
+ * port that has no variable. Anything else is left for the app to read.
31
+ */
32
+ export declare function unresolvedPortVariables(value: string): string[];
33
+ /**
34
+ * Point `localhost:<port>` at this worktree's stack: in every `KEY=value`
35
+ * line, a main-checkout host port after a local host name (`localhost` and
36
+ * its subdomains, `127.0.0.1`, `0.0.0.0`, `[::1]`, `host.docker.internal`)
37
+ * becomes the worktree's port. Comments and everything else stay as they are. Returns
38
+ * the new content and the keys whose value changed.
39
+ */
40
+ export declare function rewriteHostPorts(content: string, allocations: PortAllocation[], moved?: ReadonlyMap<number, number>): {
41
+ content: string;
42
+ changed: Record<string, string>;
43
+ };
44
+ /** `.env`, `.env.local`, `.env.development`… but not the templates (`.env.example`, `.env.sample`). */
45
+ export declare function isLocalEnvFileName(name: string): boolean;
19
46
  export interface InjectOptions {
20
47
  /** Compose project name; always available to overrides as `${COMPOSE_PROJECT_NAME}`. */
21
48
  projectName?: string;
@@ -38,6 +65,19 @@ export interface InjectOptions {
38
65
  root?: string;
39
66
  /** The pristine env source this file was copied from; never written, even via a hard link. */
40
67
  source?: string;
68
+ /**
69
+ * Further keys to write before the ports and overrides (which win on a
70
+ * clash): values of a tracked env file rewritten for this worktree.
71
+ */
72
+ extraEntries?: Record<string, string>;
73
+ /** Receives a warning for each `${VAR}` in an override that names no known variable. */
74
+ warnings?: string[];
75
+ /**
76
+ * Leave an `extraEntries` key alone when the file already sets it outside
77
+ * wtc's block: in a `.local` sibling the user's own value overrides the
78
+ * tracked file's, so the tracked file's rewritten value has no say.
79
+ */
80
+ extraOnlyIfAbsent?: boolean;
41
81
  }
42
82
  /** Name of the compose file list variable (runtime remapping). */
43
83
  export declare const COMPOSE_FILE_VAR = "COMPOSE_FILE";
@@ -51,8 +91,8 @@ export declare function literalEnvValue(value: string): string;
51
91
  * All keys that should be written for a target: the project name and
52
92
  * compose file list (root target), allocated port variables, then the
53
93
  * target's overrides (already interpolated). Overrides win on key
54
- * conflicts. A remapped hardcoded port has no variable, so nothing is
55
- * written for it.
94
+ * conflicts. A remapped hardcoded port is written under its generated
95
+ * `WTC_<SERVICE>_PORT` name.
56
96
  */
57
97
  export declare function buildOverrideEntries(allocations: PortAllocation[], overrides?: Record<string, string>, opts?: InjectOptions): Record<string, string>;
58
98
  export declare function buildOverrideBlock(allocations: PortAllocation[], envOverrides?: Record<string, string>): string;
package/dist/sync/env.js CHANGED
@@ -5,6 +5,11 @@ export const DEFAULT_ENV_FILE = ".env";
5
5
  const EXAMPLE_ENV_FILE = ".env.example";
6
6
  const BLOCK_START = "# --- wtc port overrides ---";
7
7
  const BLOCK_END = "# --- end wtc ---";
8
+ /** Whether an env file's content carries the block wtc appends. */
9
+ export function hasOverrideBlock(content) {
10
+ const start = content.indexOf(BLOCK_START);
11
+ return start !== -1 && content.indexOf(BLOCK_END, start) !== -1;
12
+ }
8
13
  export function stripOverrideBlock(content) {
9
14
  const startIdx = content.indexOf(BLOCK_START);
10
15
  if (startIdx === -1)
@@ -26,6 +31,57 @@ export function resolveOverrideValue(value, values) {
26
31
  }
27
32
  return result;
28
33
  }
34
+ /**
35
+ * `${VAR}` placeholders an override value still holds after interpolation
36
+ * that look like one of wtc's (`…_PORT`, `WTC_…`): most likely a typo or a
37
+ * port that has no variable. Anything else is left for the app to read.
38
+ */
39
+ export function unresolvedPortVariables(value) {
40
+ return [...value.matchAll(/\$\{([A-Z_][A-Z0-9_]*)\}/g)]
41
+ .map((m) => m[1])
42
+ .filter((name) => name.endsWith("_PORT") || name === "PORT" || name.startsWith("WTC_"));
43
+ }
44
+ /** Hosts under which a port published on this machine is reached. */
45
+ const LOCAL_HOSTS = "(?:[A-Za-z0-9-]+\\.)*localhost|127\\.0\\.0\\.1|0\\.0\\.0\\.0|\\[::1\\]|host\\.docker\\.internal";
46
+ /**
47
+ * Point `localhost:<port>` at this worktree's stack: in every `KEY=value`
48
+ * line, a main-checkout host port after a local host name (`localhost` and
49
+ * its subdomains, `127.0.0.1`, `0.0.0.0`, `[::1]`, `host.docker.internal`)
50
+ * becomes the worktree's port. Comments and everything else stay as they are. Returns
51
+ * the new content and the keys whose value changed.
52
+ */
53
+ export function rewriteHostPorts(content, allocations, moved = new Map()) {
54
+ const ports = new Map();
55
+ // A port this stack held before and had to leave is as stale as the main checkout's.
56
+ for (const [from, to] of moved)
57
+ ports.set(String(from), to);
58
+ for (const a of allocations) {
59
+ if (a.defaultPort !== a.port)
60
+ ports.set(String(a.defaultPort), a.port);
61
+ }
62
+ const changed = {};
63
+ if (ports.size === 0)
64
+ return { content, changed };
65
+ const hostPort = new RegExp(`(?<![A-Za-z0-9_.-])(${LOCAL_HOSTS}):(\\d+)(?!\\d)`, "g");
66
+ const lines = content.split("\n").map((line) => {
67
+ const m = /^([ \t]*(?:export[ \t]+)?([A-Za-z_][A-Za-z0-9_.]*)[ \t]*=)(.*)$/.exec(line);
68
+ if (!m)
69
+ return line;
70
+ const value = m[3].replace(hostPort, (whole, host, port) => {
71
+ const to = ports.get(port);
72
+ return to === undefined ? whole : `${host}:${to}`;
73
+ });
74
+ if (value === m[3])
75
+ return line;
76
+ changed[m[2]] = value;
77
+ return m[1] + value;
78
+ });
79
+ return { content: lines.join("\n"), changed };
80
+ }
81
+ /** `.env`, `.env.local`, `.env.development`… but not the templates (`.env.example`, `.env.sample`). */
82
+ export function isLocalEnvFileName(name) {
83
+ return /^\.env(\..+)?$/.test(name) && !/\.(example|sample|template|tpl|dist|defaults)$/i.test(name);
84
+ }
29
85
  /** Name of the compose file list variable (runtime remapping). */
30
86
  export const COMPOSE_FILE_VAR = "COMPOSE_FILE";
31
87
  /**
@@ -42,8 +98,8 @@ export function literalEnvValue(value) {
42
98
  * All keys that should be written for a target: the project name and
43
99
  * compose file list (root target), allocated port variables, then the
44
100
  * target's overrides (already interpolated). Overrides win on key
45
- * conflicts. A remapped hardcoded port has no variable, so nothing is
46
- * written for it.
101
+ * conflicts. A remapped hardcoded port is written under its generated
102
+ * `WTC_<SERVICE>_PORT` name.
47
103
  */
48
104
  export function buildOverrideEntries(allocations, overrides, opts = {}) {
49
105
  const entries = {};
@@ -55,15 +111,24 @@ export function buildOverrideEntries(allocations, overrides, opts = {}) {
55
111
  }
56
112
  if (opts.composeFile)
57
113
  entries[COMPOSE_FILE_VAR] = literalEnvValue(opts.composeFile);
114
+ Object.assign(entries, opts.extraEntries);
58
115
  for (const a of allocations) {
59
- if (a.envVar === null)
116
+ // A hardcoded port goes by a generated `WTC_…_PORT` name: it has no variable of its own.
117
+ const name = a.envVar ?? a.variable;
118
+ if (!name)
60
119
  continue;
61
- entries[a.envVar] = String(a.port);
62
- values.set(a.envVar, a.port);
120
+ entries[name] = String(a.port);
121
+ values.set(name, a.port);
63
122
  }
64
123
  if (overrides) {
65
124
  for (const [key, template] of Object.entries(overrides)) {
66
- entries[key] = resolveOverrideValue(template, values);
125
+ const value = resolveOverrideValue(template, values);
126
+ const unknown = unresolvedPortVariables(value);
127
+ if (unknown.length > 0) {
128
+ opts.warnings?.push(`Override ${key} uses ${unknown.map((u) => `\${${u}}`).join(", ")}, which wtc does not know: it is written as it is. ` +
129
+ `Known variables: ${[...values.keys()].map((k) => `\${${k}}`).join(", ") || "(none)"}.`);
130
+ }
131
+ entries[key] = value;
67
132
  }
68
133
  }
69
134
  return entries;
@@ -116,7 +181,12 @@ export function injectPortOverrides(envPath, allocations, overrides, opts = {})
116
181
  content = fs.readFileSync(dest, "utf-8");
117
182
  }
118
183
  content = stripOverrideBlock(content);
119
- const entries = buildOverrideEntries(allocations, overrides, opts);
184
+ let extraEntries = opts.extraEntries;
185
+ if (extraEntries && opts.extraOnlyIfAbsent) {
186
+ const own = content;
187
+ extraEntries = Object.fromEntries(Object.entries(extraEntries).filter(([key]) => !new RegExp(`^[ \\t]*(?:export[ \\t]+)?${escapeRegExp(key)}[ \\t]*=`, "m").test(own)));
188
+ }
189
+ const entries = buildOverrideEntries(allocations, overrides, { ...opts, extraEntries, warnings });
120
190
  const rewritten = [];
121
191
  const appended = {};
122
192
  for (const [key, value] of Object.entries(entries)) {
@@ -175,7 +245,8 @@ export function findEnvSource(repoRoot, target) {
175
245
  export function prepareEnvFile(repoRoot, wtPath, target, warnings = []) {
176
246
  const relDest = path.join(target.dir, target.envFileName);
177
247
  const source = findEnvSource(repoRoot, target);
178
- if (!source && target.dir !== "")
248
+ // Compose reads its `.env` whether or not the project has one: those targets always get a file.
249
+ if (!source && target.dir !== "" && !target.compose)
179
250
  return null;
180
251
  const label = `${target.label} env file ${relDest}`;
181
252
  const dest = prepareWritablePath(wtPath, relDest, warnings, { label });
@@ -0,0 +1,91 @@
1
+ import type { EnvTarget } from "./env.js";
2
+ import type { PortAllocation } from "../ports/types.js";
3
+ /**
4
+ * Env files for one worktree. Two rules shape everything here:
5
+ *
6
+ * 1. wtc never changes a file the worktree's branch tracks. A tracked
7
+ * `.env.development` stays exactly as the branch has it, so nothing of
8
+ * wtc's shows up in `git status` or lands in a commit. What wtc would
9
+ * have written there goes into the file's `.local` sibling
10
+ * (`.env.development.local`), the name dotenv loaders read on top of it.
11
+ * 2. Untracked env files are local to a checkout — git cannot bring them
12
+ * into a worktree — so wtc brings them: the configured env file is
13
+ * copied fresh from the main checkout on every start, as before, and
14
+ * every other `.env*` file next to it (`.env.local`…) is copied when the
15
+ * worktree has none. A file the worktree already has and wtc did not
16
+ * put there is the user's: it is left alone.
17
+ *
18
+ * In both cases `localhost:<port>` values are pointed at this worktree's
19
+ * ports, so a `DATABASE_URL` or `BASE_URL` reaches this stack, not the
20
+ * main checkout's.
21
+ */
22
+ export interface EnvSyncInput {
23
+ repoRoot: string;
24
+ wtPath: string;
25
+ /** The git common directory (for `info/exclude`). */
26
+ commonDir: string;
27
+ targets: EnvTarget[];
28
+ allocations: PortAllocation[];
29
+ /** Rewrite `localhost:<port>` values (the config's `rewriteEnvPorts`, default true). */
30
+ rewritePorts: boolean;
31
+ /** Worktree-relative files wtc wrote on earlier starts (the registry's record). */
32
+ managed?: readonly string[];
33
+ /** Ports this stack held and had to leave (old → new): values still naming them are stale. */
34
+ moved?: ReadonlyMap<number, number>;
35
+ }
36
+ export interface EnvDestination {
37
+ target: EnvTarget;
38
+ /** Absolute path wtc writes the target's keys to, or null when the target is skipped. */
39
+ file: string | null;
40
+ /** The target's env file is tracked, so `file` is its `.local` sibling. */
41
+ sidecar: boolean;
42
+ /** Values of the tracked target rewritten for this worktree (written with the keys). */
43
+ extraEntries?: Record<string, string>;
44
+ /**
45
+ * False when the file was the user's before wtc added its block (their own
46
+ * `.env.local`): wtc keeps its block there, but the file is not "wtc's".
47
+ */
48
+ owned?: boolean;
49
+ }
50
+ export interface EnvSyncResult {
51
+ /** Where each target's keys go. */
52
+ destinations: EnvDestination[];
53
+ /** Worktree-relative files that are wtc's: copied or created by it. */
54
+ written: string[];
55
+ /** Worktree-relative files of the user's in which wtc only keeps its block. */
56
+ blockOnly: string[];
57
+ /** Tracked env file (absolute) → the `.local` sibling (absolute) that carries its rewritten values. */
58
+ sidecars: Map<string, string>;
59
+ /** What was done, for the user. */
60
+ notes: string[];
61
+ warnings: string[];
62
+ }
63
+ /**
64
+ * Copy the worktree's local env files and work out where each target's
65
+ * keys go. Nothing wtc-specific (project name, ports, overrides) is
66
+ * written yet: `injectEnvTargets` does that once the stack's compose files
67
+ * are known.
68
+ */
69
+ export declare function syncEnvFiles(input: EnvSyncInput): EnvSyncResult;
70
+ export interface EnvInjection {
71
+ projectName: string;
72
+ /** `COMPOSE_FILE` for the root target, when the stack's compose calls need one. */
73
+ composeFile: string | null;
74
+ }
75
+ /**
76
+ * Write wtc's keys — project name and compose file list (root target),
77
+ * allocated ports, overrides — to each destination `syncEnvFiles` chose.
78
+ * Returns the worktree-relative files written.
79
+ */
80
+ export declare function injectEnvTargets(input: EnvSyncInput, synced: EnvSyncResult, injection: EnvInjection): {
81
+ written: string[];
82
+ blockOnly: string[];
83
+ warnings: string[];
84
+ };
85
+ /**
86
+ * Keep files wtc created in a worktree out of `git status`: those git
87
+ * neither tracks nor ignores there are listed in `.git/info/exclude` (the
88
+ * repository's local ignore file, never committed; every worktree shares
89
+ * it). Returns the paths added.
90
+ */
91
+ export declare function excludeFromGit(commonDir: string, wtPath: string, rels: string[]): string[];