worktree-compose 0.2.0 → 1.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 (94) hide show
  1. package/README.md +458 -140
  2. package/SKILL.md +113 -0
  3. package/dist/cli.js +94 -33
  4. package/dist/commands/clean.d.ts +2 -1
  5. package/dist/commands/clean.js +8 -48
  6. package/dist/commands/doctor.d.ts +3 -0
  7. package/dist/commands/doctor.js +30 -0
  8. package/dist/commands/list.d.ts +4 -1
  9. package/dist/commands/list.js +62 -44
  10. package/dist/commands/promote.d.ts +3 -1
  11. package/dist/commands/promote.js +4 -34
  12. package/dist/commands/prune.d.ts +2 -0
  13. package/dist/commands/prune.js +7 -0
  14. package/dist/commands/restart.d.ts +2 -1
  15. package/dist/commands/restart.js +11 -5
  16. package/dist/commands/skill.d.ts +4 -0
  17. package/dist/commands/skill.js +10 -0
  18. package/dist/commands/start.d.ts +2 -1
  19. package/dist/commands/start.js +7 -34
  20. package/dist/commands/stop.d.ts +1 -1
  21. package/dist/commands/stop.js +6 -23
  22. package/dist/compose/detect.d.ts +1 -2
  23. package/dist/compose/detect.js +1 -10
  24. package/dist/compose/engine.d.ts +61 -0
  25. package/dist/compose/engine.js +129 -0
  26. package/dist/compose/isolation.d.ts +15 -0
  27. package/dist/compose/isolation.js +62 -0
  28. package/dist/compose/parse.d.ts +5 -0
  29. package/dist/compose/parse.js +89 -11
  30. package/dist/compose/status.d.ts +84 -0
  31. package/dist/compose/status.js +227 -0
  32. package/dist/compose/types.d.ts +20 -0
  33. package/dist/config.d.ts +31 -0
  34. package/dist/config.js +23 -6
  35. package/dist/context.d.ts +89 -3
  36. package/dist/context.js +278 -45
  37. package/dist/core/clean.d.ts +91 -0
  38. package/dist/core/clean.js +343 -0
  39. package/dist/core/compose.d.ts +30 -0
  40. package/dist/core/compose.js +45 -0
  41. package/dist/core/doctor.d.ts +28 -0
  42. package/dist/core/doctor.js +277 -0
  43. package/dist/core/list.d.ts +42 -0
  44. package/dist/core/list.js +94 -0
  45. package/dist/core/promote.d.ts +40 -0
  46. package/dist/core/promote.js +117 -0
  47. package/dist/core/prune.d.ts +32 -0
  48. package/dist/core/prune.js +75 -0
  49. package/dist/core/restart.d.ts +14 -0
  50. package/dist/core/restart.js +18 -0
  51. package/dist/core/start.d.ts +31 -0
  52. package/dist/core/start.js +99 -0
  53. package/dist/core/stop.d.ts +32 -0
  54. package/dist/core/stop.js +57 -0
  55. package/dist/core/teardown.d.ts +121 -0
  56. package/dist/core/teardown.js +298 -0
  57. package/dist/core/types.d.ts +15 -0
  58. package/dist/core/types.js +1 -0
  59. package/dist/git/promote.d.ts +49 -2
  60. package/dist/git/promote.js +160 -38
  61. package/dist/git/repo.d.ts +72 -0
  62. package/dist/git/repo.js +167 -0
  63. package/dist/git/status.d.ts +8 -0
  64. package/dist/git/status.js +25 -0
  65. package/dist/git/worktree.d.ts +63 -3
  66. package/dist/git/worktree.js +173 -29
  67. package/dist/mcp/server.d.ts +2 -0
  68. package/dist/mcp/server.js +230 -115
  69. package/dist/ports/allocate.d.ts +14 -3
  70. package/dist/ports/allocate.js +20 -4
  71. package/dist/selectors.d.ts +10 -0
  72. package/dist/selectors.js +106 -0
  73. package/dist/skill.d.ts +13 -0
  74. package/dist/skill.js +28 -0
  75. package/dist/state/registry.d.ts +194 -0
  76. package/dist/state/registry.js +595 -0
  77. package/dist/sync/env.d.ts +77 -2
  78. package/dist/sync/env.js +151 -32
  79. package/dist/sync/files.d.ts +15 -1
  80. package/dist/sync/files.js +126 -38
  81. package/dist/utils/exec.d.ts +69 -4
  82. package/dist/utils/exec.js +221 -15
  83. package/dist/utils/log.d.ts +37 -0
  84. package/dist/utils/log.js +68 -4
  85. package/dist/utils/paths.d.ts +61 -0
  86. package/dist/utils/paths.js +163 -0
  87. package/dist/utils/sanitize.d.ts +7 -0
  88. package/dist/utils/sanitize.js +12 -0
  89. package/dist/version.d.ts +1 -0
  90. package/dist/version.js +3 -0
  91. package/dist/workspaces/resolve.d.ts +21 -0
  92. package/dist/workspaces/resolve.js +235 -0
  93. package/package.json +18 -14
  94. package/wtc.schema.json +62 -0
package/dist/sync/env.js CHANGED
@@ -1,5 +1,8 @@
1
1
  import fs from "node:fs";
2
2
  import path from "node:path";
3
+ import { isSameFile, prepareWritablePath } from "../utils/paths.js";
4
+ export const DEFAULT_ENV_FILE = ".env";
5
+ const EXAMPLE_ENV_FILE = ".env.example";
3
6
  const BLOCK_START = "# --- wtc port overrides ---";
4
7
  const BLOCK_END = "# --- end wtc ---";
5
8
  export function stripOverrideBlock(content) {
@@ -13,46 +16,162 @@ export function stripOverrideBlock(content) {
13
16
  const after = content.slice(endIdx + BLOCK_END.length).trimStart();
14
17
  return [before, after].filter(Boolean).join("\n") + "\n";
15
18
  }
16
- export function buildOverrideBlock(allocations, envOverrides) {
17
- const lines = [BLOCK_START];
18
- const portValues = new Map();
19
+ /** Name of the compose project variable wtc writes into the root env file. */
20
+ export const PROJECT_NAME_VAR = "COMPOSE_PROJECT_NAME";
21
+ /** Interpolate `${VAR}` placeholders with allocated port values (and the project name). */
22
+ export function resolveOverrideValue(value, values) {
23
+ let result = value;
24
+ for (const [name, replacement] of values) {
25
+ result = result.replaceAll(`\${${name}}`, String(replacement));
26
+ }
27
+ return result;
28
+ }
29
+ /**
30
+ * All keys that should be written for a target: the project name (root
31
+ * target), allocated ports, then the target's overrides (already
32
+ * interpolated). Overrides win on key conflicts.
33
+ */
34
+ export function buildOverrideEntries(allocations, overrides, opts = {}) {
35
+ const entries = {};
36
+ const values = new Map();
37
+ if (opts.projectName !== undefined) {
38
+ if (opts.writeProjectName)
39
+ entries[PROJECT_NAME_VAR] = opts.projectName;
40
+ values.set(PROJECT_NAME_VAR, opts.projectName);
41
+ }
19
42
  for (const a of allocations) {
20
- lines.push(`${a.envVar}=${a.port}`);
21
- portValues.set(a.envVar, a.port);
22
- }
23
- if (envOverrides) {
24
- for (const [key, template] of Object.entries(envOverrides)) {
25
- let value = template;
26
- for (const [envVar, port] of portValues) {
27
- value = value.replace(`\${${envVar}}`, String(port));
28
- }
29
- lines.push(`${key}=${value}`);
43
+ entries[a.envVar] = String(a.port);
44
+ values.set(a.envVar, a.port);
45
+ }
46
+ if (overrides) {
47
+ for (const [key, template] of Object.entries(overrides)) {
48
+ entries[key] = resolveOverrideValue(template, values);
30
49
  }
31
50
  }
32
- lines.push(BLOCK_END);
33
- return lines.join("\n");
51
+ return entries;
52
+ }
53
+ function formatOverrideBlock(entries) {
54
+ return [
55
+ BLOCK_START,
56
+ ...Object.entries(entries).map(([key, value]) => `${key}=${value}`),
57
+ BLOCK_END,
58
+ ].join("\n");
59
+ }
60
+ export function buildOverrideBlock(allocations, envOverrides) {
61
+ return formatOverrideBlock(buildOverrideEntries(allocations, envOverrides));
62
+ }
63
+ function escapeRegExp(value) {
64
+ return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
34
65
  }
35
- export function injectPortOverrides(envPath, allocations, envOverrides) {
66
+ /**
67
+ * Replace the value of an existing `KEY=...` line in place, preserving
68
+ * comments, ordering, and every other line. Returns `null` when the key is
69
+ * not present in the file.
70
+ */
71
+ function rewriteExistingKey(content, key, value) {
72
+ const probe = new RegExp(`^${escapeRegExp(key)}=`, "m");
73
+ if (!probe.test(content))
74
+ return null;
75
+ const line = new RegExp(`^${escapeRegExp(key)}=.*$`, "gm");
76
+ // A function replacer avoids `$&`-style substitution in user values.
77
+ return content.replace(line, () => `${key}=${value}`);
78
+ }
79
+ /**
80
+ * Inject port allocations and overrides into an env file. Existing keys are
81
+ * rewritten in place; missing keys are appended in an idempotent block. The
82
+ * block is stripped before writing, so repeated runs never accumulate.
83
+ */
84
+ export function injectPortOverrides(envPath, allocations, overrides, opts = {}) {
85
+ // Reading and writing follow symlinks: never inject through one (it could
86
+ // point at the main checkout's real .env, or anywhere else on disk). A
87
+ // symlink here is replaced; a directory resolving outside the root refused.
88
+ const root = opts.root ?? path.dirname(envPath);
89
+ const warnings = [];
90
+ const dest = prepareWritablePath(root, path.relative(root, envPath), warnings);
91
+ if (!dest)
92
+ throw new Error(`Refusing to write ${envPath}: ${warnings.join(" ")}`);
93
+ if (opts.source && isSameFile(opts.source, dest)) {
94
+ throw new Error(`Refusing to write ${envPath}: it is the env source file ${opts.source} itself.`);
95
+ }
36
96
  let content = "";
37
- if (fs.existsSync(envPath)) {
38
- content = fs.readFileSync(envPath, "utf-8");
97
+ if (fs.existsSync(dest)) {
98
+ content = fs.readFileSync(dest, "utf-8");
39
99
  }
40
100
  content = stripOverrideBlock(content);
41
- const block = buildOverrideBlock(allocations, envOverrides);
42
- const result = content.trimEnd() + "\n\n" + block + "\n";
43
- fs.writeFileSync(envPath, result, "utf-8");
44
- }
45
- export function copyBaseEnv(repoRoot, wtPath) {
46
- const envSrc = path.join(repoRoot, ".env");
47
- const envExampleSrc = path.join(repoRoot, ".env.example");
48
- const envDst = path.join(wtPath, ".env");
49
- if (fs.existsSync(envSrc)) {
50
- fs.copyFileSync(envSrc, envDst);
101
+ const entries = buildOverrideEntries(allocations, overrides, opts);
102
+ const rewritten = [];
103
+ const appended = {};
104
+ for (const [key, value] of Object.entries(entries)) {
105
+ const replaced = rewriteExistingKey(content, key, value);
106
+ if (replaced === null) {
107
+ appended[key] = value;
108
+ }
109
+ else {
110
+ content = replaced;
111
+ rewritten.push(key);
112
+ }
51
113
  }
52
- else if (fs.existsSync(envExampleSrc)) {
53
- fs.copyFileSync(envExampleSrc, envDst);
114
+ let result = content.trimEnd();
115
+ if (Object.keys(appended).length > 0) {
116
+ result += (result ? "\n\n" : "") + formatOverrideBlock(appended) + "\n";
117
+ }
118
+ else if (result) {
119
+ result += "\n";
120
+ }
121
+ fs.mkdirSync(path.dirname(dest), { recursive: true });
122
+ fs.writeFileSync(dest, result, "utf-8");
123
+ return { rewritten, appended: Object.keys(appended), warnings };
124
+ }
125
+ /**
126
+ * Pristine source for a target's env file: `<dir>/<envFileName>` first, then
127
+ * `<dir>/.env.example`. Returns `null` when neither exists.
128
+ */
129
+ export function findEnvSource(repoRoot, target) {
130
+ const dir = target.dir ? path.join(repoRoot, target.dir) : repoRoot;
131
+ const primary = path.join(dir, target.envFileName);
132
+ if (fs.existsSync(primary))
133
+ return primary;
134
+ const example = path.join(dir, EXAMPLE_ENV_FILE);
135
+ if (fs.existsSync(example))
136
+ return example;
137
+ return null;
138
+ }
139
+ /**
140
+ * Copy the pristine env file from the main checkout into the worktree at the
141
+ * same relative path. Returns the destination path, or `null` when the target
142
+ * is skipped: a workspace with no env file (and no `.env.example`), or a
143
+ * destination wtc refuses to write (reasons pushed onto `warnings`). The
144
+ * repo root otherwise always yields a path — an empty file is created when
145
+ * nothing exists, matching the previous zero-config behavior.
146
+ *
147
+ * The worktree's branch is not trusted: a committed symlink at the env path
148
+ * (`.env -> ../somewhere`) or a symlinked workspace directory would make the
149
+ * copy write the main checkout's secrets — plus the override block — to
150
+ * wherever it points. A symlink at the env path is replaced by a regular
151
+ * copy (symlinking `.env` from main is also a common setup, and writing
152
+ * through it would rewrite main's own `.env`); a directory that resolves
153
+ * outside the worktree is refused. The env source itself is never written:
154
+ * when the destination *is* the source (same realpath or inode — the
155
+ * worktree is the main checkout, or a hard link), the target is skipped.
156
+ */
157
+ export function prepareEnvFile(repoRoot, wtPath, target, warnings = []) {
158
+ const relDest = path.join(target.dir, target.envFileName);
159
+ const source = findEnvSource(repoRoot, target);
160
+ if (!source && target.dir !== "")
161
+ return null;
162
+ const label = `${target.label} env file ${relDest}`;
163
+ const dest = prepareWritablePath(wtPath, relDest, warnings, { label });
164
+ if (!dest)
165
+ return null;
166
+ if (source && isSameFile(source, dest)) {
167
+ warnings.push(`Skipped ${label}: it is the main checkout's own env file (the worktree is the env source); wtc never rewrites it.`);
168
+ return null;
54
169
  }
55
- else {
56
- fs.writeFileSync(envDst, "", "utf-8");
170
+ fs.mkdirSync(path.dirname(dest), { recursive: true });
171
+ if (!source) {
172
+ fs.writeFileSync(dest, "", "utf-8");
173
+ return dest;
57
174
  }
175
+ fs.copyFileSync(source, dest);
176
+ return dest;
58
177
  }
@@ -1,3 +1,17 @@
1
1
  import type { ComposeFile } from "../compose/types.js";
2
2
  export declare function getDockerfiles(composeFile: ComposeFile, repoRoot: string): string[];
3
- export declare function syncWorktreeFiles(repoRoot: string, wtPath: string, composeFile: ComposeFile, extraSync?: string[]): void;
3
+ /**
4
+ * Repo-relative paths of the `sync` entries that stay inside the repo, plus a
5
+ * warning for each entry that is absolute or escapes it (`../secrets`).
6
+ */
7
+ export declare function resolveSyncEntries(repoRoot: string, entries: string[] | undefined): {
8
+ entries: string[];
9
+ warnings: string[];
10
+ };
11
+ /**
12
+ * Copy compose files (with includes), Dockerfiles and `sync` entries from the
13
+ * main checkout into a worktree. Returns warnings for skipped entries (sync
14
+ * entries outside the repo, and destinations wtc refuses to write or
15
+ * replaced instead of writing through) so `start` can report them.
16
+ */
17
+ export declare function syncWorktreeFiles(repoRoot: string, wtPath: string, composeFile: ComposeFile, extraSync?: string[]): string[];
@@ -1,71 +1,159 @@
1
1
  import fs from "node:fs";
2
2
  import path from "node:path";
3
- function copyFile(src, dst) {
4
- fs.mkdirSync(path.dirname(dst), { recursive: true });
5
- if (fs.existsSync(src)) {
6
- fs.copyFileSync(src, dst);
3
+ import { escapesRoot, isSameFile, lstatQuiet, parentPlacement, prepareWritablePath, resolveInside, } from "../utils/paths.js";
4
+ /**
5
+ * Make `rel` in the worktree a symlink carrying the same target as in main.
6
+ * Symlinks are recreated, never followed: following one could copy files
7
+ * from anywhere on disk (e.g. `secrets -> ~/.ssh`) into a worktree.
8
+ *
9
+ * - nothing there → create the link;
10
+ * - a symlink with the same target → leave it (no-op);
11
+ * - a symlink with another target, or a file → unlink (never recursive), link;
12
+ * - a real directory → leave it with a warning: it may hold the worktree's
13
+ * own data (0.2 copied a symlinked sync entry as a real directory).
14
+ */
15
+ function copySymlink(run, rel) {
16
+ const target = fs.readlinkSync(path.join(run.repoRoot, rel));
17
+ const existing = lstatQuiet(path.join(run.wtPath, rel));
18
+ if (existing?.isSymbolicLink() && fs.readlinkSync(path.join(run.wtPath, rel)) === target)
19
+ return;
20
+ if (existing?.isDirectory()) {
21
+ run.warnings.push(`Skipping sync of ${rel}: it is a symlink in the main checkout but a directory in the worktree. Remove the worktree's directory yourself to resync.`);
22
+ return;
7
23
  }
24
+ const dst = prepareWritablePath(run.wtPath, rel, run.warnings, {
25
+ label: `sync of ${rel}`,
26
+ warnReplace: false,
27
+ });
28
+ if (!dst)
29
+ return;
30
+ // A symlink or special file is already gone; a regular file is replaced too.
31
+ if (lstatQuiet(dst))
32
+ fs.unlinkSync(dst);
33
+ fs.mkdirSync(path.dirname(dst), { recursive: true });
34
+ fs.symlinkSync(target, dst);
8
35
  }
9
- function copyDir(src, dst) {
36
+ function copyFile(run, rel) {
37
+ const src = path.join(run.repoRoot, rel);
10
38
  if (!fs.existsSync(src))
11
39
  return;
12
- fs.mkdirSync(dst, { recursive: true });
13
- for (const entry of fs.readdirSync(src, { withFileTypes: true })) {
14
- const srcPath = path.join(src, entry.name);
15
- const dstPath = path.join(dst, entry.name);
16
- if (entry.isDirectory()) {
17
- copyDir(srcPath, dstPath);
18
- }
19
- else {
20
- fs.copyFileSync(srcPath, dstPath);
40
+ const dst = prepareWritablePath(run.wtPath, rel, run.warnings, { label: `sync of ${rel}` });
41
+ if (!dst)
42
+ return;
43
+ if (isSameFile(src, dst)) {
44
+ run.warnings.push(`Skipping sync of ${rel}: it is the main checkout's own file.`);
45
+ return;
46
+ }
47
+ fs.mkdirSync(path.dirname(dst), { recursive: true });
48
+ fs.copyFileSync(src, dst);
49
+ }
50
+ /** Copy one entry: directories recurse, symlinks are recreated, special files skipped. */
51
+ function copyEntry(run, rel) {
52
+ const stat = fs.lstatSync(path.join(run.repoRoot, rel), { throwIfNoEntry: false });
53
+ if (!stat)
54
+ return;
55
+ if (stat.isSymbolicLink()) {
56
+ copySymlink(run, rel);
57
+ }
58
+ else if (stat.isDirectory()) {
59
+ copyDir(run, rel);
60
+ }
61
+ else if (stat.isFile()) {
62
+ copyFile(run, rel);
63
+ }
64
+ // Sockets, FIFOs and devices are skipped: copying them blocks or fails.
65
+ }
66
+ /** Merge a main directory into the worktree's (never replacing a file the worktree has there). */
67
+ function copyDir(run, rel) {
68
+ const existing = lstatQuiet(path.join(run.wtPath, rel));
69
+ if (existing?.isFile()) {
70
+ run.warnings.push(`Skipping sync of ${rel}: the worktree has a file there, not a directory.`);
71
+ return;
72
+ }
73
+ if (existing?.isDirectory()) {
74
+ if (parentPlacement(run.wtPath, rel) !== "inside") {
75
+ run.warnings.push(`Skipping sync of ${rel}: it resolves outside the worktree (through a symlinked directory); wtc does not write there.`);
76
+ return;
21
77
  }
22
78
  }
79
+ else {
80
+ // Missing, or a symlink / special file (unlinked, not followed).
81
+ const dst = prepareWritablePath(run.wtPath, rel, run.warnings, { label: `sync of ${rel}` });
82
+ if (!dst)
83
+ return;
84
+ fs.mkdirSync(dst, { recursive: true });
85
+ }
86
+ for (const entry of fs.readdirSync(path.join(run.repoRoot, rel), { withFileTypes: true })) {
87
+ copyEntry(run, path.join(rel, entry.name));
88
+ }
23
89
  }
24
- /** True if a repo-relative path escapes the repo root (or is absolute). */
25
- function escapesRepo(rel) {
26
- return path.isAbsolute(rel) || rel === ".." || rel.startsWith(`..${path.sep}`);
90
+ /** Remote build contexts (`https://…`, `git@…`) have no local Dockerfile. */
91
+ function isRemoteContext(context) {
92
+ return /^[a-z][a-z0-9+.-]*:\/\//i.test(context) || context.startsWith("git@");
27
93
  }
28
94
  export function getDockerfiles(composeFile, repoRoot) {
29
95
  const files = [];
30
96
  for (const svc of composeFile.services) {
31
- if (!svc.build)
97
+ if (!svc.build || svc.build.inline)
32
98
  continue;
33
99
  const context = svc.build.context ?? ".";
100
+ if (isRemoteContext(context))
101
+ continue;
34
102
  const dockerfile = svc.build.dockerfile ?? "Dockerfile";
35
103
  // Build contexts are relative to the file that declared the service, which
36
- // may be an included file rather than the root compose file.
104
+ // may be an included file rather than the root compose file. `resolve`
105
+ // (not `join`) keeps absolute dockerfile paths absolute.
37
106
  const resolvedDir = path.resolve(path.dirname(svc.sourcePath), context);
38
- const fullPath = path.join(resolvedDir, dockerfile);
107
+ const fullPath = path.resolve(resolvedDir, dockerfile);
39
108
  const rel = path.relative(repoRoot, fullPath);
40
- if (escapesRepo(rel))
109
+ if (rel === "" || escapesRoot(rel))
41
110
  continue;
42
111
  files.push(rel);
43
112
  }
44
113
  return [...new Set(files)];
45
114
  }
115
+ /**
116
+ * Repo-relative paths of the `sync` entries that stay inside the repo, plus a
117
+ * warning for each entry that is absolute or escapes it (`../secrets`).
118
+ */
119
+ export function resolveSyncEntries(repoRoot, entries) {
120
+ const ok = [];
121
+ const warnings = [];
122
+ for (const entry of entries ?? []) {
123
+ const abs = resolveInside(repoRoot, entry);
124
+ if (!abs) {
125
+ warnings.push(`Skipping sync entry "${entry}": it must be a path inside the repository.`);
126
+ continue;
127
+ }
128
+ ok.push(path.relative(repoRoot, abs));
129
+ }
130
+ return { entries: ok, warnings };
131
+ }
132
+ /**
133
+ * Copy compose files (with includes), Dockerfiles and `sync` entries from the
134
+ * main checkout into a worktree. Returns warnings for skipped entries (sync
135
+ * entries outside the repo, and destinations wtc refuses to write or
136
+ * replaced instead of writing through) so `start` can report them.
137
+ */
46
138
  export function syncWorktreeFiles(repoRoot, wtPath, composeFile, extraSync) {
139
+ const { entries, warnings } = resolveSyncEntries(repoRoot, extraSync);
140
+ // Copying the main checkout onto itself would at best do nothing.
141
+ if (isSameFile(repoRoot, wtPath)) {
142
+ warnings.push(`Skipping file sync: ${wtPath} is the main checkout itself.`);
143
+ return warnings;
144
+ }
145
+ const run = { repoRoot, wtPath, warnings };
47
146
  for (const src of composeFile.sourceFiles) {
48
147
  const rel = path.relative(repoRoot, src);
49
- if (escapesRepo(rel))
148
+ if (rel === "" || escapesRoot(rel))
50
149
  continue;
51
- copyFile(path.join(repoRoot, rel), path.join(wtPath, rel));
150
+ copyFile(run, rel);
52
151
  }
53
152
  for (const df of getDockerfiles(composeFile, repoRoot)) {
54
- copyFile(path.join(repoRoot, df), path.join(wtPath, df));
153
+ copyFile(run, df);
55
154
  }
56
- if (extraSync) {
57
- for (const p of extraSync) {
58
- const src = path.join(repoRoot, p);
59
- const dst = path.join(wtPath, p);
60
- if (fs.existsSync(src)) {
61
- const stat = fs.statSync(src);
62
- if (stat.isDirectory()) {
63
- copyDir(src, dst);
64
- }
65
- else {
66
- copyFile(src, dst);
67
- }
68
- }
69
- }
155
+ for (const rel of entries) {
156
+ copyEntry(run, rel);
70
157
  }
158
+ return warnings;
71
159
  }
@@ -1,4 +1,69 @@
1
- import { type ExecSyncOptions } from "node:child_process";
2
- export declare function exec(cmd: string, opts?: ExecSyncOptions): string;
3
- export declare function execLive(cmd: string, opts?: ExecSyncOptions): void;
4
- export declare function execSafe(cmd: string, opts?: ExecSyncOptions): string | null;
1
+ /**
2
+ * Process execution without a shell. Every command is an executable plus an
3
+ * argv array, so branch names, paths, and project names reach git/docker as
4
+ * literal arguments — `$(…)`, backticks, quotes, and `;` are never
5
+ * interpreted. Never add `shell: true` here.
6
+ */
7
+ export interface RunOptions {
8
+ cwd?: string;
9
+ env?: NodeJS.ProcessEnv;
10
+ /** Trim stdout (default true). Disable for `-z` output where whitespace is data. */
11
+ trim?: boolean;
12
+ /**
13
+ * Kill the command after this many milliseconds and fail with a
14
+ * "timed out" CommandError. Used for container-engine probes (`ps`, `info`,
15
+ * `version`, sweeps): a wedged daemon must not hang wtc — or the MCP
16
+ * server, whose event loop a synchronous call blocks — indefinitely.
17
+ * Never set it on `compose up --build`, which may legitimately run long.
18
+ */
19
+ timeoutMs?: number;
20
+ }
21
+ /**
22
+ * Timeout for container-engine probes and listings: engine `version`,
23
+ * `ps`, `info`, the per-project fallback `compose ps`, sweep `ls`/`rm`.
24
+ * Never for `compose up --build` / `down`, which may legitimately run long.
25
+ */
26
+ export declare const ENGINE_PROBE_TIMEOUT_MS = 20000;
27
+ export interface RunResult {
28
+ code: number;
29
+ stdout: string;
30
+ stderr: string;
31
+ }
32
+ /** Human-readable rendering of a command, for messages only — never executed. */
33
+ export declare function displayCommand(file: string, args: string[]): string;
34
+ /** Last `maxLines` lines of `text`, trimmed. */
35
+ export declare function tailLines(text: string, maxLines: number): string;
36
+ export declare class CommandError extends Error {
37
+ /** The command as displayed to humans (args joined with spaces). */
38
+ command: string;
39
+ /** Exit code, or `null` when the process could not be started or was killed. */
40
+ code: number | null;
41
+ /** Trimmed stderr tail (empty for live commands, whose stderr went to the terminal). */
42
+ stderr: string;
43
+ constructor(command: string, code: number | null, stderr: string, reason?: string);
44
+ }
45
+ /** Run a command and return its trimmed stdout. Throws CommandError on failure. */
46
+ export declare function run(file: string, args: string[], opts?: RunOptions): string;
47
+ /** Like `run`, but returns `null` on any failure. */
48
+ export declare function runSafe(file: string, args: string[], opts?: RunOptions): string | null;
49
+ /** Run with inherited stdio (CLI only — never from the MCP server). */
50
+ export declare function runLive(file: string, args: string[], opts?: RunOptions): void;
51
+ export interface RunAsyncOptions extends RunOptions {
52
+ /** Inherit stdio (CLI) instead of capturing bounded tails (MCP). */
53
+ live?: boolean;
54
+ /**
55
+ * Abort the command (SIGTERM) when this signal fires — e.g. an MCP
56
+ * client cancelling a long `compose up --build`.
57
+ */
58
+ signal?: AbortSignal;
59
+ /** Called with each complete line of captured output (not with `live`). */
60
+ onLine?: (line: string) => void;
61
+ }
62
+ /**
63
+ * Spawn-based variant that never blocks the event loop (the MCP server keeps
64
+ * answering while `docker compose up --build` runs). With `live`, stdio is
65
+ * inherited (CLI); otherwise output is captured as bounded tails.
66
+ * Resolves on exit code 0, rejects with CommandError otherwise (including
67
+ * when `signal` aborts it or `timeoutMs` elapses).
68
+ */
69
+ export declare function runAsync(file: string, args: string[], opts?: RunAsyncOptions): Promise<RunResult>;