worktree-compose 0.1.4 → 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.
- package/README.md +560 -125
- package/SKILL.md +113 -0
- package/dist/cli.js +94 -33
- package/dist/commands/clean.d.ts +2 -1
- package/dist/commands/clean.js +8 -48
- package/dist/commands/doctor.d.ts +3 -0
- package/dist/commands/doctor.js +30 -0
- package/dist/commands/list.d.ts +4 -1
- package/dist/commands/list.js +62 -44
- package/dist/commands/promote.d.ts +3 -1
- package/dist/commands/promote.js +4 -34
- package/dist/commands/prune.d.ts +2 -0
- package/dist/commands/prune.js +7 -0
- package/dist/commands/restart.d.ts +2 -1
- package/dist/commands/restart.js +11 -5
- package/dist/commands/skill.d.ts +4 -0
- package/dist/commands/skill.js +10 -0
- package/dist/commands/start.d.ts +2 -1
- package/dist/commands/start.js +7 -34
- package/dist/commands/stop.d.ts +1 -1
- package/dist/commands/stop.js +6 -23
- package/dist/compose/detect.d.ts +1 -2
- package/dist/compose/detect.js +1 -10
- package/dist/compose/engine.d.ts +61 -0
- package/dist/compose/engine.js +129 -0
- package/dist/compose/isolation.d.ts +15 -0
- package/dist/compose/isolation.js +62 -0
- package/dist/compose/parse.d.ts +5 -0
- package/dist/compose/parse.js +200 -12
- package/dist/compose/status.d.ts +84 -0
- package/dist/compose/status.js +227 -0
- package/dist/compose/types.d.ts +25 -0
- package/dist/config.d.ts +38 -0
- package/dist/config.js +23 -6
- package/dist/context.d.ts +90 -3
- package/dist/context.js +293 -20
- package/dist/core/clean.d.ts +91 -0
- package/dist/core/clean.js +343 -0
- package/dist/core/compose.d.ts +30 -0
- package/dist/core/compose.js +45 -0
- package/dist/core/doctor.d.ts +28 -0
- package/dist/core/doctor.js +277 -0
- package/dist/core/list.d.ts +42 -0
- package/dist/core/list.js +94 -0
- package/dist/core/promote.d.ts +40 -0
- package/dist/core/promote.js +117 -0
- package/dist/core/prune.d.ts +32 -0
- package/dist/core/prune.js +75 -0
- package/dist/core/restart.d.ts +14 -0
- package/dist/core/restart.js +18 -0
- package/dist/core/start.d.ts +31 -0
- package/dist/core/start.js +99 -0
- package/dist/core/stop.d.ts +32 -0
- package/dist/core/stop.js +57 -0
- package/dist/core/teardown.d.ts +121 -0
- package/dist/core/teardown.js +298 -0
- package/dist/core/types.d.ts +15 -0
- package/dist/core/types.js +1 -0
- package/dist/git/promote.d.ts +49 -2
- package/dist/git/promote.js +160 -38
- package/dist/git/repo.d.ts +72 -0
- package/dist/git/repo.js +167 -0
- package/dist/git/status.d.ts +8 -0
- package/dist/git/status.js +25 -0
- package/dist/git/worktree.d.ts +63 -3
- package/dist/git/worktree.js +173 -29
- package/dist/mcp/server.d.ts +2 -0
- package/dist/mcp/server.js +230 -116
- package/dist/ports/allocate.d.ts +34 -2
- package/dist/ports/allocate.js +69 -4
- package/dist/selectors.d.ts +10 -0
- package/dist/selectors.js +106 -0
- package/dist/skill.d.ts +13 -0
- package/dist/skill.js +28 -0
- package/dist/state/registry.d.ts +194 -0
- package/dist/state/registry.js +595 -0
- package/dist/sync/env.d.ts +77 -2
- package/dist/sync/env.js +151 -32
- package/dist/sync/files.d.ts +16 -1
- package/dist/sync/files.js +136 -36
- package/dist/utils/exec.d.ts +69 -4
- package/dist/utils/exec.js +221 -15
- package/dist/utils/log.d.ts +37 -0
- package/dist/utils/log.js +68 -4
- package/dist/utils/paths.d.ts +61 -0
- package/dist/utils/paths.js +163 -0
- package/dist/utils/sanitize.d.ts +7 -0
- package/dist/utils/sanitize.js +12 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +3 -0
- package/dist/workspaces/resolve.d.ts +21 -0
- package/dist/workspaces/resolve.js +235 -0
- package/package.json +13 -9
- 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
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
if (
|
|
24
|
-
for (const [key, template] of Object.entries(
|
|
25
|
-
|
|
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
|
-
|
|
33
|
-
|
|
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
|
-
|
|
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(
|
|
38
|
-
content = fs.readFileSync(
|
|
97
|
+
if (fs.existsSync(dest)) {
|
|
98
|
+
content = fs.readFileSync(dest, "utf-8");
|
|
39
99
|
}
|
|
40
100
|
content = stripOverrideBlock(content);
|
|
41
|
-
const
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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
|
-
|
|
53
|
-
|
|
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
|
-
|
|
56
|
-
|
|
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
|
}
|
package/dist/sync/files.d.ts
CHANGED
|
@@ -1,2 +1,17 @@
|
|
|
1
1
|
import type { ComposeFile } from "../compose/types.js";
|
|
2
|
-
export declare function
|
|
2
|
+
export declare function getDockerfiles(composeFile: ComposeFile, repoRoot: string): string[];
|
|
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[];
|
package/dist/sync/files.js
CHANGED
|
@@ -1,59 +1,159 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
2
|
import path from "node:path";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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
|
|
36
|
+
function copyFile(run, rel) {
|
|
37
|
+
const src = path.join(run.repoRoot, rel);
|
|
10
38
|
if (!fs.existsSync(src))
|
|
11
39
|
return;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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
|
-
|
|
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@");
|
|
93
|
+
}
|
|
94
|
+
export function getDockerfiles(composeFile, repoRoot) {
|
|
25
95
|
const files = [];
|
|
26
96
|
for (const svc of composeFile.services) {
|
|
27
|
-
if (!svc.build)
|
|
97
|
+
if (!svc.build || svc.build.inline)
|
|
28
98
|
continue;
|
|
29
99
|
const context = svc.build.context ?? ".";
|
|
100
|
+
if (isRemoteContext(context))
|
|
101
|
+
continue;
|
|
30
102
|
const dockerfile = svc.build.dockerfile ?? "Dockerfile";
|
|
31
|
-
|
|
32
|
-
|
|
103
|
+
// Build contexts are relative to the file that declared the service, which
|
|
104
|
+
// may be an included file rather than the root compose file. `resolve`
|
|
105
|
+
// (not `join`) keeps absolute dockerfile paths absolute.
|
|
106
|
+
const resolvedDir = path.resolve(path.dirname(svc.sourcePath), context);
|
|
107
|
+
const fullPath = path.resolve(resolvedDir, dockerfile);
|
|
33
108
|
const rel = path.relative(repoRoot, fullPath);
|
|
109
|
+
if (rel === "" || escapesRoot(rel))
|
|
110
|
+
continue;
|
|
34
111
|
files.push(rel);
|
|
35
112
|
}
|
|
36
113
|
return [...new Set(files)];
|
|
37
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
|
+
*/
|
|
38
138
|
export function syncWorktreeFiles(repoRoot, wtPath, composeFile, extraSync) {
|
|
39
|
-
const
|
|
40
|
-
|
|
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 };
|
|
146
|
+
for (const src of composeFile.sourceFiles) {
|
|
147
|
+
const rel = path.relative(repoRoot, src);
|
|
148
|
+
if (rel === "" || escapesRoot(rel))
|
|
149
|
+
continue;
|
|
150
|
+
copyFile(run, rel);
|
|
151
|
+
}
|
|
41
152
|
for (const df of getDockerfiles(composeFile, repoRoot)) {
|
|
42
|
-
copyFile(
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
const src = path.join(repoRoot, p);
|
|
47
|
-
const dst = path.join(wtPath, p);
|
|
48
|
-
if (fs.existsSync(src)) {
|
|
49
|
-
const stat = fs.statSync(src);
|
|
50
|
-
if (stat.isDirectory()) {
|
|
51
|
-
copyDir(src, dst);
|
|
52
|
-
}
|
|
53
|
-
else {
|
|
54
|
-
copyFile(src, dst);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
}
|
|
153
|
+
copyFile(run, df);
|
|
154
|
+
}
|
|
155
|
+
for (const rel of entries) {
|
|
156
|
+
copyEntry(run, rel);
|
|
58
157
|
}
|
|
158
|
+
return warnings;
|
|
59
159
|
}
|
package/dist/utils/exec.d.ts
CHANGED
|
@@ -1,4 +1,69 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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>;
|