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.
- package/README.md +458 -140
- 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 +89 -11
- package/dist/compose/status.d.ts +84 -0
- package/dist/compose/status.js +227 -0
- package/dist/compose/types.d.ts +20 -0
- package/dist/config.d.ts +31 -0
- package/dist/config.js +23 -6
- package/dist/context.d.ts +89 -3
- package/dist/context.js +278 -45
- 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 -115
- package/dist/ports/allocate.d.ts +14 -3
- package/dist/ports/allocate.js +20 -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 +15 -1
- package/dist/sync/files.js +126 -38
- 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 +18 -14
- package/wtc.schema.json +62 -0
package/dist/utils/exec.js
CHANGED
|
@@ -1,23 +1,229 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
import { execFileSync, spawn } from "node:child_process";
|
|
2
|
+
/**
|
|
3
|
+
* Timeout for container-engine probes and listings: engine `version`,
|
|
4
|
+
* `ps`, `info`, the per-project fallback `compose ps`, sweep `ls`/`rm`.
|
|
5
|
+
* Never for `compose up --build` / `down`, which may legitimately run long.
|
|
6
|
+
*/
|
|
7
|
+
export const ENGINE_PROBE_TIMEOUT_MS = 20_000;
|
|
8
|
+
const MAX_BUFFER = 256 * 1024 * 1024;
|
|
9
|
+
const TAIL_BYTES = 64 * 1024;
|
|
10
|
+
const TAIL_LINES = 200;
|
|
11
|
+
/** Lines of stderr kept in a CommandError message. */
|
|
12
|
+
const MESSAGE_TAIL_LINES = 20;
|
|
13
|
+
/** Human-readable rendering of a command, for messages only — never executed. */
|
|
14
|
+
export function displayCommand(file, args) {
|
|
15
|
+
return [file, ...args].join(" ");
|
|
9
16
|
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
17
|
+
/** Last `maxLines` lines of `text`, trimmed. */
|
|
18
|
+
export function tailLines(text, maxLines) {
|
|
19
|
+
const lines = text.trim().split("\n");
|
|
20
|
+
return lines.slice(-maxLines).join("\n").trim();
|
|
21
|
+
}
|
|
22
|
+
export class CommandError extends Error {
|
|
23
|
+
/** The command as displayed to humans (args joined with spaces). */
|
|
24
|
+
command;
|
|
25
|
+
/** Exit code, or `null` when the process could not be started or was killed. */
|
|
26
|
+
code;
|
|
27
|
+
/** Trimmed stderr tail (empty for live commands, whose stderr went to the terminal). */
|
|
28
|
+
stderr;
|
|
29
|
+
constructor(command, code, stderr, reason) {
|
|
30
|
+
const tail = tailLines(stderr, MESSAGE_TAIL_LINES);
|
|
31
|
+
const head = reason
|
|
32
|
+
? `${reason}: ${command}`
|
|
33
|
+
: `Command failed${code === null ? "" : ` (exit ${code})`}: ${command}`;
|
|
34
|
+
super(tail ? `${head}\n${tail}` : head);
|
|
35
|
+
this.name = "CommandError";
|
|
36
|
+
this.command = command;
|
|
37
|
+
this.code = code;
|
|
38
|
+
this.stderr = tail;
|
|
39
|
+
}
|
|
15
40
|
}
|
|
16
|
-
|
|
41
|
+
function asText(value) {
|
|
42
|
+
if (typeof value === "string")
|
|
43
|
+
return value;
|
|
44
|
+
if (Buffer.isBuffer(value))
|
|
45
|
+
return value.toString("utf-8");
|
|
46
|
+
return "";
|
|
47
|
+
}
|
|
48
|
+
function timeoutReason(timeoutMs) {
|
|
49
|
+
return `Command timed out after ${Math.round((timeoutMs ?? 0) / 1000)}s`;
|
|
50
|
+
}
|
|
51
|
+
/** Convert a child_process failure into a CommandError with the stderr tail. */
|
|
52
|
+
function toCommandError(file, args, err, timeoutMs) {
|
|
53
|
+
const command = displayCommand(file, args);
|
|
54
|
+
const e = err;
|
|
55
|
+
if (e?.code === "ENOENT") {
|
|
56
|
+
return new CommandError(command, null, "", `Command not found (${file})`);
|
|
57
|
+
}
|
|
58
|
+
if (e?.code === "ETIMEDOUT") {
|
|
59
|
+
return new CommandError(command, null, asText(e?.stderr), timeoutReason(timeoutMs));
|
|
60
|
+
}
|
|
61
|
+
const stderr = asText(e?.stderr);
|
|
62
|
+
const code = typeof e?.status === "number" ? e.status : null;
|
|
63
|
+
if (code === null && e?.signal) {
|
|
64
|
+
return new CommandError(command, null, stderr, `Command killed by ${e.signal}`);
|
|
65
|
+
}
|
|
66
|
+
if (code === null && !stderr && e?.message) {
|
|
67
|
+
return new CommandError(command, null, e.message);
|
|
68
|
+
}
|
|
69
|
+
return new CommandError(command, code, stderr);
|
|
70
|
+
}
|
|
71
|
+
/** Run a command and return its trimmed stdout. Throws CommandError on failure. */
|
|
72
|
+
export function run(file, args, opts = {}) {
|
|
17
73
|
try {
|
|
18
|
-
|
|
74
|
+
const out = execFileSync(file, args, {
|
|
75
|
+
cwd: opts.cwd,
|
|
76
|
+
env: opts.env ?? process.env,
|
|
77
|
+
encoding: "utf-8",
|
|
78
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
79
|
+
maxBuffer: MAX_BUFFER,
|
|
80
|
+
windowsHide: true,
|
|
81
|
+
timeout: opts.timeoutMs,
|
|
82
|
+
});
|
|
83
|
+
return opts.trim === false ? out : out.trim();
|
|
84
|
+
}
|
|
85
|
+
catch (err) {
|
|
86
|
+
throw toCommandError(file, args, err, opts.timeoutMs);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/** Like `run`, but returns `null` on any failure. */
|
|
90
|
+
export function runSafe(file, args, opts = {}) {
|
|
91
|
+
try {
|
|
92
|
+
return run(file, args, opts);
|
|
19
93
|
}
|
|
20
94
|
catch {
|
|
21
95
|
return null;
|
|
22
96
|
}
|
|
23
97
|
}
|
|
98
|
+
/** Run with inherited stdio (CLI only — never from the MCP server). */
|
|
99
|
+
export function runLive(file, args, opts = {}) {
|
|
100
|
+
try {
|
|
101
|
+
execFileSync(file, args, {
|
|
102
|
+
cwd: opts.cwd,
|
|
103
|
+
env: opts.env ?? process.env,
|
|
104
|
+
stdio: "inherit",
|
|
105
|
+
windowsHide: true,
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
catch (err) {
|
|
109
|
+
throw toCommandError(file, args, err);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Bounded output collector: keeps only the last ~64 KiB / 200 lines so a
|
|
114
|
+
* chatty `docker compose up --build` cannot grow memory without bound.
|
|
115
|
+
*/
|
|
116
|
+
class Tail {
|
|
117
|
+
text = "";
|
|
118
|
+
push(chunk) {
|
|
119
|
+
this.text += chunk;
|
|
120
|
+
if (this.text.length > TAIL_BYTES * 2) {
|
|
121
|
+
this.text = this.text.slice(-TAIL_BYTES);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
value() {
|
|
125
|
+
const bounded = this.text.length > TAIL_BYTES ? this.text.slice(-TAIL_BYTES) : this.text;
|
|
126
|
+
const lines = bounded.split("\n");
|
|
127
|
+
return lines.slice(-TAIL_LINES).join("\n");
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
/** Splits streamed chunks into complete lines for `onLine`. */
|
|
131
|
+
function lineSplitter(onLine) {
|
|
132
|
+
if (!onLine)
|
|
133
|
+
return () => { };
|
|
134
|
+
let partial = "";
|
|
135
|
+
return (chunk) => {
|
|
136
|
+
const parts = (partial + chunk).split(/\r?\n|\r/);
|
|
137
|
+
partial = parts.pop() ?? "";
|
|
138
|
+
for (const line of parts)
|
|
139
|
+
if (line.trim())
|
|
140
|
+
onLine(line);
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Spawn-based variant that never blocks the event loop (the MCP server keeps
|
|
145
|
+
* answering while `docker compose up --build` runs). With `live`, stdio is
|
|
146
|
+
* inherited (CLI); otherwise output is captured as bounded tails.
|
|
147
|
+
* Resolves on exit code 0, rejects with CommandError otherwise (including
|
|
148
|
+
* when `signal` aborts it or `timeoutMs` elapses).
|
|
149
|
+
*/
|
|
150
|
+
export function runAsync(file, args, opts = {}) {
|
|
151
|
+
const command = displayCommand(file, args);
|
|
152
|
+
return new Promise((resolve, reject) => {
|
|
153
|
+
if (opts.signal?.aborted) {
|
|
154
|
+
reject(new CommandError(command, null, "", "Command cancelled"));
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
const stdout = new Tail();
|
|
158
|
+
const stderr = new Tail();
|
|
159
|
+
let child;
|
|
160
|
+
try {
|
|
161
|
+
child = spawn(file, args, {
|
|
162
|
+
cwd: opts.cwd,
|
|
163
|
+
env: opts.env ?? process.env,
|
|
164
|
+
stdio: opts.live ? "inherit" : ["ignore", "pipe", "pipe"],
|
|
165
|
+
windowsHide: true,
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
catch (err) {
|
|
169
|
+
reject(toCommandError(file, args, err));
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
let stopReason = null;
|
|
173
|
+
const stop = (reason) => {
|
|
174
|
+
if (stopReason)
|
|
175
|
+
return;
|
|
176
|
+
stopReason = reason;
|
|
177
|
+
child.kill("SIGTERM");
|
|
178
|
+
};
|
|
179
|
+
const onAbort = () => stop("Command cancelled");
|
|
180
|
+
opts.signal?.addEventListener("abort", onAbort, { once: true });
|
|
181
|
+
const timer = opts.timeoutMs !== undefined
|
|
182
|
+
? setTimeout(() => stop(timeoutReason(opts.timeoutMs)), opts.timeoutMs)
|
|
183
|
+
: null;
|
|
184
|
+
const cleanup = () => {
|
|
185
|
+
if (timer)
|
|
186
|
+
clearTimeout(timer);
|
|
187
|
+
opts.signal?.removeEventListener("abort", onAbort);
|
|
188
|
+
};
|
|
189
|
+
const outLines = lineSplitter(opts.onLine);
|
|
190
|
+
const errLines = lineSplitter(opts.onLine);
|
|
191
|
+
child.stdout?.setEncoding("utf-8");
|
|
192
|
+
child.stderr?.setEncoding("utf-8");
|
|
193
|
+
child.stdout?.on("data", (chunk) => {
|
|
194
|
+
stdout.push(chunk);
|
|
195
|
+
outLines(chunk);
|
|
196
|
+
});
|
|
197
|
+
child.stderr?.on("data", (chunk) => {
|
|
198
|
+
stderr.push(chunk);
|
|
199
|
+
errLines(chunk);
|
|
200
|
+
});
|
|
201
|
+
let settled = false;
|
|
202
|
+
child.on("error", (err) => {
|
|
203
|
+
if (settled)
|
|
204
|
+
return;
|
|
205
|
+
settled = true;
|
|
206
|
+
cleanup();
|
|
207
|
+
reject(toCommandError(file, args, err));
|
|
208
|
+
});
|
|
209
|
+
child.on("close", (code, signal) => {
|
|
210
|
+
if (settled)
|
|
211
|
+
return;
|
|
212
|
+
settled = true;
|
|
213
|
+
cleanup();
|
|
214
|
+
const result = { code: code ?? -1, stdout: stdout.value(), stderr: stderr.value() };
|
|
215
|
+
if (stopReason) {
|
|
216
|
+
reject(new CommandError(command, null, result.stderr, stopReason));
|
|
217
|
+
}
|
|
218
|
+
else if (code === 0) {
|
|
219
|
+
resolve(result);
|
|
220
|
+
}
|
|
221
|
+
else if (code === null) {
|
|
222
|
+
reject(new CommandError(command, null, result.stderr, `Command killed by ${signal ?? "signal"}`));
|
|
223
|
+
}
|
|
224
|
+
else {
|
|
225
|
+
reject(new CommandError(command, code, result.stderr || result.stdout));
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
});
|
|
229
|
+
}
|
package/dist/utils/log.d.ts
CHANGED
|
@@ -1,5 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where operations report progress. The CLI prints to the terminal; the MCP
|
|
3
|
+
* server collects lines and returns them as tool output — it must never write
|
|
4
|
+
* to stdout, which carries the JSON-RPC stream.
|
|
5
|
+
*/
|
|
6
|
+
export interface Reporter {
|
|
7
|
+
info(msg: string): void;
|
|
8
|
+
success(msg: string): void;
|
|
9
|
+
warn(msg: string): void;
|
|
10
|
+
error(msg: string): void;
|
|
11
|
+
header(msg: string): void;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Route all human-oriented output to stderr. `wtc mcp` calls this at startup
|
|
15
|
+
* so nothing but the transport can ever reach stdout.
|
|
16
|
+
*/
|
|
17
|
+
export declare function setLogStream(stream: "stdout" | "stderr"): void;
|
|
18
|
+
/** Print a plain line (tables, file lists) on the current log stream. */
|
|
19
|
+
export declare function print(...parts: unknown[]): void;
|
|
1
20
|
export declare function info(msg: string): void;
|
|
2
21
|
export declare function success(msg: string): void;
|
|
3
22
|
export declare function warn(msg: string): void;
|
|
4
23
|
export declare function error(msg: string): void;
|
|
5
24
|
export declare function header(msg: string): void;
|
|
25
|
+
/** Terminal reporter — the CLI's behavior. */
|
|
26
|
+
export declare const consoleReporter: Reporter;
|
|
27
|
+
/** Reporter that records plain-text lines (MCP tool output, tests). */
|
|
28
|
+
export declare class CollectingReporter implements Reporter {
|
|
29
|
+
private readonly onLine?;
|
|
30
|
+
lines: string[];
|
|
31
|
+
/** `onLine` sees each line as it is recorded (MCP progress notifications). */
|
|
32
|
+
constructor(onLine?: ((line: string) => void) | undefined);
|
|
33
|
+
private push;
|
|
34
|
+
info(msg: string): void;
|
|
35
|
+
success(msg: string): void;
|
|
36
|
+
warn(msg: string): void;
|
|
37
|
+
error(msg: string): void;
|
|
38
|
+
header(msg: string): void;
|
|
39
|
+
text(): string;
|
|
40
|
+
}
|
|
41
|
+
/** Reporter that discards everything. */
|
|
42
|
+
export declare const silentReporter: Reporter;
|
package/dist/utils/log.js
CHANGED
|
@@ -1,16 +1,80 @@
|
|
|
1
1
|
import chalk from "chalk";
|
|
2
|
+
let logStream = "stdout";
|
|
3
|
+
/**
|
|
4
|
+
* Route all human-oriented output to stderr. `wtc mcp` calls this at startup
|
|
5
|
+
* so nothing but the transport can ever reach stdout.
|
|
6
|
+
*/
|
|
7
|
+
export function setLogStream(stream) {
|
|
8
|
+
logStream = stream;
|
|
9
|
+
}
|
|
10
|
+
/** Print a plain line (tables, file lists) on the current log stream. */
|
|
11
|
+
export function print(...parts) {
|
|
12
|
+
if (logStream === "stderr") {
|
|
13
|
+
console.error(...parts);
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
console.log(...parts);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
2
19
|
export function info(msg) {
|
|
3
|
-
|
|
20
|
+
print(chalk.blue("ℹ"), msg);
|
|
4
21
|
}
|
|
5
22
|
export function success(msg) {
|
|
6
|
-
|
|
23
|
+
print(chalk.green("✔"), msg);
|
|
7
24
|
}
|
|
8
25
|
export function warn(msg) {
|
|
9
|
-
|
|
26
|
+
print(chalk.yellow("⚠"), msg);
|
|
10
27
|
}
|
|
11
28
|
export function error(msg) {
|
|
12
29
|
console.error(chalk.red("✖"), msg);
|
|
13
30
|
}
|
|
14
31
|
export function header(msg) {
|
|
15
|
-
|
|
32
|
+
print(chalk.bold.cyan(`\n=== ${msg} ===`));
|
|
33
|
+
}
|
|
34
|
+
/** Terminal reporter — the CLI's behavior. */
|
|
35
|
+
export const consoleReporter = {
|
|
36
|
+
info: (msg) => info(msg),
|
|
37
|
+
success: (msg) => success(msg),
|
|
38
|
+
warn: (msg) => warn(msg),
|
|
39
|
+
error: (msg) => error(msg),
|
|
40
|
+
header: (msg) => header(msg),
|
|
41
|
+
};
|
|
42
|
+
/** Reporter that records plain-text lines (MCP tool output, tests). */
|
|
43
|
+
export class CollectingReporter {
|
|
44
|
+
onLine;
|
|
45
|
+
lines = [];
|
|
46
|
+
/** `onLine` sees each line as it is recorded (MCP progress notifications). */
|
|
47
|
+
constructor(onLine) {
|
|
48
|
+
this.onLine = onLine;
|
|
49
|
+
}
|
|
50
|
+
push(line) {
|
|
51
|
+
this.lines.push(line);
|
|
52
|
+
this.onLine?.(line);
|
|
53
|
+
}
|
|
54
|
+
info(msg) {
|
|
55
|
+
this.push(msg);
|
|
56
|
+
}
|
|
57
|
+
success(msg) {
|
|
58
|
+
this.push(`✔ ${msg}`);
|
|
59
|
+
}
|
|
60
|
+
warn(msg) {
|
|
61
|
+
this.push(`⚠ ${msg}`);
|
|
62
|
+
}
|
|
63
|
+
error(msg) {
|
|
64
|
+
this.push(`✖ ${msg}`);
|
|
65
|
+
}
|
|
66
|
+
header(msg) {
|
|
67
|
+
this.push(`=== ${msg} ===`);
|
|
68
|
+
}
|
|
69
|
+
text() {
|
|
70
|
+
return this.lines.join("\n");
|
|
71
|
+
}
|
|
16
72
|
}
|
|
73
|
+
/** Reporter that discards everything. */
|
|
74
|
+
export const silentReporter = {
|
|
75
|
+
info: () => { },
|
|
76
|
+
success: () => { },
|
|
77
|
+
warn: () => { },
|
|
78
|
+
error: () => { },
|
|
79
|
+
header: () => { },
|
|
80
|
+
};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
/** True if a root-relative path escapes the root (or is absolute). */
|
|
3
|
+
export declare function escapesRoot(rel: string): boolean;
|
|
4
|
+
/**
|
|
5
|
+
* Resolve a caller-supplied relative path inside `root`, or `null` when it is
|
|
6
|
+
* absolute or would land outside `root` (including `root` itself).
|
|
7
|
+
*/
|
|
8
|
+
export declare function resolveInside(root: string, rel: string): string | null;
|
|
9
|
+
/** True if `child` is `parent` or lies inside it. */
|
|
10
|
+
export declare function isWithin(parent: string, child: string): boolean;
|
|
11
|
+
/**
|
|
12
|
+
* `lstat` that returns `undefined` when nothing is there — including ENOTDIR
|
|
13
|
+
* (a file where a directory on the way should be), which `throwIfNoEntry`
|
|
14
|
+
* does not cover.
|
|
15
|
+
*/
|
|
16
|
+
export declare function lstatQuiet(p: string): fs.Stats | undefined;
|
|
17
|
+
/**
|
|
18
|
+
* The first existing ancestor of `rel` below `root` (the final component
|
|
19
|
+
* excluded) that is a symlink — or not a directory — or `null` when every
|
|
20
|
+
* existing ancestor is a real directory. `path.resolve` containment is
|
|
21
|
+
* purely lexical: `mkdirSync`/`copyFileSync`/`rmSync` follow a symlinked
|
|
22
|
+
* directory, so `data/x.json` with `data -> /etc` lands outside `root`
|
|
23
|
+
* even though the string never leaves it. Callers refuse such paths.
|
|
24
|
+
*/
|
|
25
|
+
export declare function symlinkAncestor(root: string, rel: string): string | null;
|
|
26
|
+
/**
|
|
27
|
+
* Whether the directory that will hold `rel` stays inside `root` physically:
|
|
28
|
+
* the realpath of the nearest existing ancestor of `dirname(rel)` must be
|
|
29
|
+
* inside realpath(`root`) and be a directory. Checked before anything is
|
|
30
|
+
* created — `mkdirSync` through a symlinked directory would already create
|
|
31
|
+
* directories outside `root`. A symlinked directory that resolves inside
|
|
32
|
+
* `root` is fine.
|
|
33
|
+
*/
|
|
34
|
+
export declare function parentPlacement(root: string, rel: string): "inside" | "outside" | "not-dir";
|
|
35
|
+
/**
|
|
36
|
+
* True when `a` and `b` are the same file: the same realpath, or the same
|
|
37
|
+
* inode (a hard link). Copying or injecting "into" the source itself would
|
|
38
|
+
* rewrite the pristine original in place.
|
|
39
|
+
*/
|
|
40
|
+
export declare function isSameFile(a: string, b: string): boolean;
|
|
41
|
+
export interface WritablePathOptions {
|
|
42
|
+
/** Names the path in "Skipped <label>: …" warnings (default: `rel`). */
|
|
43
|
+
label?: string;
|
|
44
|
+
/** Names `root` in warnings (default "the worktree"). */
|
|
45
|
+
where?: string;
|
|
46
|
+
/** Warn when a symlink or special file at the path is replaced (default true). */
|
|
47
|
+
warnReplace?: boolean;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Make `<root>/<rel>` safe to write a fresh regular file to, and return its
|
|
51
|
+
* absolute path — or `null` (with a warning) when wtc must not write there.
|
|
52
|
+
*
|
|
53
|
+
* The tree under `root` is not trusted: a worktree's branch can commit
|
|
54
|
+
* `infra -> /etc` or `.env -> ~/.ssh/config`, and `copyFileSync` /
|
|
55
|
+
* `writeFileSync` follow both. So the directory that will hold the file
|
|
56
|
+
* must resolve inside `root` (see `parentPlacement`), and a symlink or
|
|
57
|
+
* special file at the final component is unlinked — never followed, never
|
|
58
|
+
* removed recursively — so the write creates a new regular file. A
|
|
59
|
+
* directory at the path is left alone.
|
|
60
|
+
*/
|
|
61
|
+
export declare function prepareWritablePath(root: string, rel: string, warnings: string[], opts?: WritablePathOptions): string | null;
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
/** True if a root-relative path escapes the root (or is absolute). */
|
|
4
|
+
export function escapesRoot(rel) {
|
|
5
|
+
if (path.isAbsolute(rel))
|
|
6
|
+
return true;
|
|
7
|
+
const normalized = path.normalize(rel);
|
|
8
|
+
return normalized === ".." || normalized.startsWith(`..${path.sep}`);
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Resolve a caller-supplied relative path inside `root`, or `null` when it is
|
|
12
|
+
* absolute or would land outside `root` (including `root` itself).
|
|
13
|
+
*/
|
|
14
|
+
export function resolveInside(root, rel) {
|
|
15
|
+
if (!rel || path.isAbsolute(rel))
|
|
16
|
+
return null;
|
|
17
|
+
const abs = path.resolve(root, rel);
|
|
18
|
+
const back = path.relative(root, abs);
|
|
19
|
+
if (back === "" || escapesRoot(back))
|
|
20
|
+
return null;
|
|
21
|
+
return abs;
|
|
22
|
+
}
|
|
23
|
+
/** True if `child` is `parent` or lies inside it. */
|
|
24
|
+
export function isWithin(parent, child) {
|
|
25
|
+
const rel = path.relative(parent, child);
|
|
26
|
+
return rel === "" || !escapesRoot(rel);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* `lstat` that returns `undefined` when nothing is there — including ENOTDIR
|
|
30
|
+
* (a file where a directory on the way should be), which `throwIfNoEntry`
|
|
31
|
+
* does not cover.
|
|
32
|
+
*/
|
|
33
|
+
export function lstatQuiet(p) {
|
|
34
|
+
try {
|
|
35
|
+
return fs.lstatSync(p, { throwIfNoEntry: false });
|
|
36
|
+
}
|
|
37
|
+
catch (err) {
|
|
38
|
+
if (err.code === "ENOTDIR")
|
|
39
|
+
return undefined;
|
|
40
|
+
throw err;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* The first existing ancestor of `rel` below `root` (the final component
|
|
45
|
+
* excluded) that is a symlink — or not a directory — or `null` when every
|
|
46
|
+
* existing ancestor is a real directory. `path.resolve` containment is
|
|
47
|
+
* purely lexical: `mkdirSync`/`copyFileSync`/`rmSync` follow a symlinked
|
|
48
|
+
* directory, so `data/x.json` with `data -> /etc` lands outside `root`
|
|
49
|
+
* even though the string never leaves it. Callers refuse such paths.
|
|
50
|
+
*/
|
|
51
|
+
export function symlinkAncestor(root, rel) {
|
|
52
|
+
const parts = path.normalize(rel).split(path.sep).filter((p) => p && p !== ".");
|
|
53
|
+
let current = root;
|
|
54
|
+
for (const part of parts.slice(0, -1)) {
|
|
55
|
+
current = path.join(current, part);
|
|
56
|
+
const stat = fs.lstatSync(current, { throwIfNoEntry: false });
|
|
57
|
+
if (!stat)
|
|
58
|
+
return null;
|
|
59
|
+
if (stat.isSymbolicLink() || !stat.isDirectory()) {
|
|
60
|
+
return path.relative(root, current);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Where `p` will physically be once its missing components are created:
|
|
67
|
+
* the realpath of its deepest existing ancestor plus the missing tail. `null`
|
|
68
|
+
* when that ancestor cannot be resolved (a dangling symlink, a loop) — a
|
|
69
|
+
* write could then land anywhere the link is later pointed.
|
|
70
|
+
*/
|
|
71
|
+
function physicalPath(p) {
|
|
72
|
+
let probe = path.resolve(p);
|
|
73
|
+
const tail = [];
|
|
74
|
+
while (!lstatQuiet(probe)) {
|
|
75
|
+
const parent = path.dirname(probe);
|
|
76
|
+
if (parent === probe)
|
|
77
|
+
return null;
|
|
78
|
+
tail.unshift(path.basename(probe));
|
|
79
|
+
probe = parent;
|
|
80
|
+
}
|
|
81
|
+
try {
|
|
82
|
+
const real = fs.realpathSync.native(probe);
|
|
83
|
+
return { real: path.join(real, ...tail), isDir: fs.statSync(real).isDirectory() };
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Whether the directory that will hold `rel` stays inside `root` physically:
|
|
91
|
+
* the realpath of the nearest existing ancestor of `dirname(rel)` must be
|
|
92
|
+
* inside realpath(`root`) and be a directory. Checked before anything is
|
|
93
|
+
* created — `mkdirSync` through a symlinked directory would already create
|
|
94
|
+
* directories outside `root`. A symlinked directory that resolves inside
|
|
95
|
+
* `root` is fine.
|
|
96
|
+
*/
|
|
97
|
+
export function parentPlacement(root, rel) {
|
|
98
|
+
const physicalRoot = physicalPath(root);
|
|
99
|
+
const parent = physicalPath(path.dirname(path.resolve(root, rel)));
|
|
100
|
+
if (!physicalRoot || !parent || !isWithin(physicalRoot.real, parent.real))
|
|
101
|
+
return "outside";
|
|
102
|
+
return parent.isDir ? "inside" : "not-dir";
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* True when `a` and `b` are the same file: the same realpath, or the same
|
|
106
|
+
* inode (a hard link). Copying or injecting "into" the source itself would
|
|
107
|
+
* rewrite the pristine original in place.
|
|
108
|
+
*/
|
|
109
|
+
export function isSameFile(a, b) {
|
|
110
|
+
try {
|
|
111
|
+
if (fs.realpathSync.native(a) === fs.realpathSync.native(b))
|
|
112
|
+
return true;
|
|
113
|
+
const sa = fs.statSync(a);
|
|
114
|
+
const sb = fs.statSync(b);
|
|
115
|
+
return sa.dev === sb.dev && sa.ino === sb.ino;
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Make `<root>/<rel>` safe to write a fresh regular file to, and return its
|
|
123
|
+
* absolute path — or `null` (with a warning) when wtc must not write there.
|
|
124
|
+
*
|
|
125
|
+
* The tree under `root` is not trusted: a worktree's branch can commit
|
|
126
|
+
* `infra -> /etc` or `.env -> ~/.ssh/config`, and `copyFileSync` /
|
|
127
|
+
* `writeFileSync` follow both. So the directory that will hold the file
|
|
128
|
+
* must resolve inside `root` (see `parentPlacement`), and a symlink or
|
|
129
|
+
* special file at the final component is unlinked — never followed, never
|
|
130
|
+
* removed recursively — so the write creates a new regular file. A
|
|
131
|
+
* directory at the path is left alone.
|
|
132
|
+
*/
|
|
133
|
+
export function prepareWritablePath(root, rel, warnings, opts = {}) {
|
|
134
|
+
const label = opts.label ?? rel;
|
|
135
|
+
const where = opts.where ?? "the worktree";
|
|
136
|
+
const abs = resolveInside(root, rel);
|
|
137
|
+
if (!abs) {
|
|
138
|
+
warnings.push(`Skipped ${label}: it is not a path inside ${where}.`);
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
const placement = parentPlacement(root, rel);
|
|
142
|
+
if (placement === "outside") {
|
|
143
|
+
warnings.push(`Skipped ${label}: it resolves outside ${where} (through a symlinked directory); wtc does not write there.`);
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
if (placement === "not-dir") {
|
|
147
|
+
warnings.push(`Skipped ${label}: a parent of it in ${where} is a file, not a directory.`);
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
const existing = fs.lstatSync(abs, { throwIfNoEntry: false });
|
|
151
|
+
if (!existing || existing.isFile())
|
|
152
|
+
return abs;
|
|
153
|
+
if (existing.isDirectory()) {
|
|
154
|
+
warnings.push(`Skipped ${label}: it is a directory in ${where}.`);
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
fs.unlinkSync(abs);
|
|
158
|
+
if (opts.warnReplace !== false) {
|
|
159
|
+
const kind = existing.isSymbolicLink() ? "a symlink" : "a special file";
|
|
160
|
+
warnings.push(`${rel} in ${where} was ${kind}; replaced it with a regular file (wtc never writes through one).`);
|
|
161
|
+
}
|
|
162
|
+
return abs;
|
|
163
|
+
}
|
package/dist/utils/sanitize.d.ts
CHANGED
|
@@ -1,2 +1,9 @@
|
|
|
1
1
|
export declare function sanitize(input: string): string;
|
|
2
2
|
export declare function composeProjectName(repoName: string, index: number, branch: string): string;
|
|
3
|
+
export declare function escapeRegExp(value: string): string;
|
|
4
|
+
/**
|
|
5
|
+
* Anchored pattern for resource names (containers, networks, volumes) that
|
|
6
|
+
* belong to this repo's worktree stacks: `<repo>-wt-<n>-…`. Anchoring matters:
|
|
7
|
+
* an unanchored `-wt-` match also hits other repos' stacks (`othermyapp-wt-1-x`).
|
|
8
|
+
*/
|
|
9
|
+
export declare function worktreeResourcePattern(repoName: string): RegExp;
|
package/dist/utils/sanitize.js
CHANGED
|
@@ -9,3 +9,15 @@ export function sanitize(input) {
|
|
|
9
9
|
export function composeProjectName(repoName, index, branch) {
|
|
10
10
|
return sanitize(`${repoName}-wt-${index}-${branch}`);
|
|
11
11
|
}
|
|
12
|
+
export function escapeRegExp(value) {
|
|
13
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Anchored pattern for resource names (containers, networks, volumes) that
|
|
17
|
+
* belong to this repo's worktree stacks: `<repo>-wt-<n>-…`. Anchoring matters:
|
|
18
|
+
* an unanchored `-wt-` match also hits other repos' stacks (`othermyapp-wt-1-x`).
|
|
19
|
+
*/
|
|
20
|
+
export function worktreeResourcePattern(repoName) {
|
|
21
|
+
const prefix = sanitize(repoName);
|
|
22
|
+
return new RegExp(`^${prefix ? `${escapeRegExp(prefix)}-` : ""}wt-\\d+-`);
|
|
23
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const VERSION: string;
|
package/dist/version.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export interface WorkspaceInfo {
|
|
2
|
+
/** Workspace directory relative to the repo root, POSIX-style ("apps/server"). */
|
|
3
|
+
dir: string;
|
|
4
|
+
/** Absolute workspace directory. */
|
|
5
|
+
absDir: string;
|
|
6
|
+
/** `package.json#name` when present, otherwise the directory basename. */
|
|
7
|
+
name: string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Workspace globs declared by the repo: `package.json#workspaces` (npm/bun/yarn
|
|
11
|
+
* style, array or `{ "packages": [...] }`) plus `pnpm-workspace.yaml#packages`.
|
|
12
|
+
*/
|
|
13
|
+
export declare function getWorkspacePatterns(repoRoot: string): string[];
|
|
14
|
+
/** Every directory matched by the declared workspace globs that has a package.json. */
|
|
15
|
+
export declare function listWorkspaces(repoRoot: string): WorkspaceInfo[];
|
|
16
|
+
/**
|
|
17
|
+
* Resolve user-supplied workspace selectors (`--workspace server`, `apps/server`,
|
|
18
|
+
* `@acme/ui`) into workspace directories, preserving request order and
|
|
19
|
+
* deduplicating. Throws with available options when a selector matches nothing.
|
|
20
|
+
*/
|
|
21
|
+
export declare function resolveWorkspaces(repoRoot: string, names: string[]): WorkspaceInfo[];
|