@kaynooo/cli-wt 0.1.6 → 0.1.8
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 +17 -10
- package/dist/cli-02xxc9xk.js +67 -0
- package/dist/cli-5khwxy5g.js +36 -0
- package/dist/cli-6k7xzgw2.js +519 -0
- package/dist/cli-96wy1q8e.js +97 -0
- package/dist/cli-9m1jyyrf.js +69 -0
- package/dist/cli-fa4fh6ex.js +37 -0
- package/dist/cli-fdwwsds1.js +153 -0
- package/dist/cli-jb9a8d46.js +674 -0
- package/dist/cli-k4471y24.js +779 -0
- package/dist/cli-kt6d30zz.js +106 -0
- package/dist/cli-q7e6gegw.js +105 -0
- package/dist/cli-qwe5ykfs.js +313 -0
- package/dist/cli-tp4q5w5x.js +251 -0
- package/dist/cli.js +228 -2809
- package/dist/commands-jqr92tk6.js +222 -0
- package/dist/init-0ws4c883.js +166 -0
- package/dist/list-83vq4jng.js +88 -0
- package/dist/print-d4aq2eqw.js +77 -0
- package/dist/prompt-y71tp3f5.js +436 -0
- package/dist/prune-7ex1qyd3.js +17 -0
- package/dist/run-jrmm9gg8.js +18 -0
- package/dist/shell-1smv2vf9.js +55 -0
- package/dist/stack-2zj6r6py.js +20 -0
- package/dist/tools-c4m5037s.js +13 -0
- package/package.json +4 -4
- package/src/cli.ts +250 -69
- package/src/commands.ts +51 -38
- package/src/compose.ts +37 -14
- package/src/config.ts +8 -1
- package/src/errors.ts +16 -6
- package/src/files.ts +14 -15
- package/src/git.ts +53 -7
- package/src/index.ts +2 -1
- package/src/init.ts +56 -10
- package/src/jobs.ts +54 -15
- package/src/list.ts +26 -13
- package/src/print.ts +76 -0
- package/src/project.ts +129 -28
- package/src/proxy.ts +5 -5
- package/src/prune.ts +149 -0
- package/src/queue.ts +27 -3
- package/src/run.ts +82 -23
- package/src/shell.ts +34 -7
- package/src/stack.ts +10 -12
- package/src/sync.ts +28 -3
- package/src/tools.ts +91 -0
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
import {
|
|
3
|
+
U
|
|
4
|
+
} from "./cli-k4471y24.js";
|
|
5
|
+
import {
|
|
6
|
+
holdSignals,
|
|
7
|
+
failureText
|
|
8
|
+
} from "./cli-jb9a8d46.js";
|
|
9
|
+
import {
|
|
10
|
+
WtError,
|
|
11
|
+
worktreeHost
|
|
12
|
+
} from "./cli-6k7xzgw2.js";
|
|
13
|
+
import {
|
|
14
|
+
queueDir,
|
|
15
|
+
inLine
|
|
16
|
+
} from "./cli-9m1jyyrf.js";
|
|
17
|
+
import {
|
|
18
|
+
baseComposeFiles
|
|
19
|
+
} from "./cli-fdwwsds1.js";
|
|
20
|
+
|
|
21
|
+
// src/files.ts
|
|
22
|
+
import { existsSync, lstatSync, mkdirSync, readFileSync, renameSync, rmSync, statSync, symlinkSync, unlinkSync, writeFileSync } from "fs";
|
|
23
|
+
import { basename, dirname, join } from "path";
|
|
24
|
+
var {$ } = globalThis.Bun;
|
|
25
|
+
var PARTIAL = ".wt-partial";
|
|
26
|
+
function writeIfChanged(path, content) {
|
|
27
|
+
if (existsSync(path) && readFileSync(path, "utf8") === content)
|
|
28
|
+
return false;
|
|
29
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
30
|
+
writeFileSync(path, content);
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
async function volumeExists(name) {
|
|
34
|
+
return (await $`docker volume inspect ${name}`.quiet().nothrow()).exitCode === 0;
|
|
35
|
+
}
|
|
36
|
+
async function copyFromMain(worktree) {
|
|
37
|
+
const warnings = [];
|
|
38
|
+
for (const entry of worktree.project.copy ?? []) {
|
|
39
|
+
const source = join(worktree.project.root, entry);
|
|
40
|
+
const target = join(worktree.dir, entry);
|
|
41
|
+
if (!existsSync(source)) {
|
|
42
|
+
warnings.push(`copy: ${entry} does not exist in main`);
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
if (existsSync(target))
|
|
46
|
+
continue;
|
|
47
|
+
const partial = `${target}${PARTIAL}`;
|
|
48
|
+
mkdirSync(dirname(target), { recursive: true });
|
|
49
|
+
rmSync(partial, { recursive: true, force: true });
|
|
50
|
+
await $`cp -a --reflink=auto ${source} ${partial}`.quiet();
|
|
51
|
+
renameSync(partial, target);
|
|
52
|
+
}
|
|
53
|
+
return warnings;
|
|
54
|
+
}
|
|
55
|
+
function linkFromMain(worktree) {
|
|
56
|
+
for (const entry of worktree.project.link ?? []) {
|
|
57
|
+
const source = join(worktree.project.root, entry);
|
|
58
|
+
const target = join(worktree.dir, entry);
|
|
59
|
+
if (existsSync(source) && !existsSync(target)) {
|
|
60
|
+
mkdirSync(dirname(target), { recursive: true });
|
|
61
|
+
symlinkSync(source, target);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
var WILDCARD_LABEL = /^\*\./;
|
|
66
|
+
function rewriteHosts(text, routes, task) {
|
|
67
|
+
const hosts = [...new Set(routes.map((route) => route.host.replace(WILDCARD_LABEL, "")))].sort((a, b) => b.length - a.length);
|
|
68
|
+
if (hosts.length === 0)
|
|
69
|
+
return text;
|
|
70
|
+
const pattern = new RegExp(`(?<![\\w.-])(\\.?)(${hosts.map(U).join("|")})(?![\\w.-])`, "g");
|
|
71
|
+
return text.replace(pattern, (_, dot, host) => `${dot}${worktreeHost(host, task)}`);
|
|
72
|
+
}
|
|
73
|
+
function setEnvValue(text, key, value) {
|
|
74
|
+
const line = `${key}=${value}`;
|
|
75
|
+
const pattern = new RegExp(`^${U(key)}=.*$`, "m");
|
|
76
|
+
if (pattern.test(text))
|
|
77
|
+
return text.replace(pattern, () => line);
|
|
78
|
+
return `${text}${text === "" || text.endsWith(`
|
|
79
|
+
`) ? "" : `
|
|
80
|
+
`}${line}
|
|
81
|
+
`;
|
|
82
|
+
}
|
|
83
|
+
function expandEnvValue(value, worktree) {
|
|
84
|
+
return value.replaceAll("{task}", worktree.task).replaceAll("{port}", String(worktree.clientPort));
|
|
85
|
+
}
|
|
86
|
+
function writeEnvFiles(worktree) {
|
|
87
|
+
const { project, task, dir } = worktree;
|
|
88
|
+
const envFiles = (project.copy ?? []).filter((entry) => basename(entry).startsWith(".env") && statSync(join(dir, entry), { throwIfNoEntry: false })?.isFile());
|
|
89
|
+
for (const entry of new Set([...envFiles, ".env"])) {
|
|
90
|
+
const file = join(dir, entry);
|
|
91
|
+
let text = existsSync(file) ? rewriteHosts(readFileSync(file, "utf8"), project.routes, task) : "";
|
|
92
|
+
for (const [key, value] of Object.entries(project.env?.[entry] ?? {}))
|
|
93
|
+
text = setEnvValue(text, key, expandEnvValue(value, worktree));
|
|
94
|
+
if (entry === ".env") {
|
|
95
|
+
text = setEnvValue(text, "COMPOSE_PROJECT_NAME", worktree.id);
|
|
96
|
+
text = setEnvValue(text, "COMPOSE_FILE", [...baseComposeFiles(dir, worktree.project.settings.overrides), worktree.files.override].join(":"));
|
|
97
|
+
}
|
|
98
|
+
writeIfChanged(file, text);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
function unlinkLegacySymlink(dir, entry) {
|
|
102
|
+
let path = dir;
|
|
103
|
+
for (const part of entry.split("/")) {
|
|
104
|
+
path = join(path, part);
|
|
105
|
+
const stat = lstatSync(path, { throwIfNoEntry: false });
|
|
106
|
+
if (!stat)
|
|
107
|
+
return;
|
|
108
|
+
if (stat.isSymbolicLink())
|
|
109
|
+
return unlinkSync(path);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
async function mainContainers(main, status) {
|
|
113
|
+
return (await $`docker ps -q --filter ${`status=${status}`} --filter ${`label=com.docker.compose.project=${main.name}`}`.quiet().text()).split(`
|
|
114
|
+
`).filter(Boolean);
|
|
115
|
+
}
|
|
116
|
+
function unpause(ids) {
|
|
117
|
+
if (ids.length)
|
|
118
|
+
Bun.spawnSync(["docker", "unpause", ...ids], { stdout: "ignore", stderr: "ignore" });
|
|
119
|
+
}
|
|
120
|
+
async function whilePaused(ids, work, abort, record) {
|
|
121
|
+
const release = () => {
|
|
122
|
+
unpause(ids);
|
|
123
|
+
if (record !== undefined)
|
|
124
|
+
rmSync(record, { force: true });
|
|
125
|
+
};
|
|
126
|
+
const held = holdSignals(() => {
|
|
127
|
+
abort?.();
|
|
128
|
+
release();
|
|
129
|
+
});
|
|
130
|
+
try {
|
|
131
|
+
if (ids.length) {
|
|
132
|
+
if (record !== undefined) {
|
|
133
|
+
mkdirSync(dirname(record), { recursive: true });
|
|
134
|
+
writeFileSync(record, `${ids.join(`
|
|
135
|
+
`)}
|
|
136
|
+
`);
|
|
137
|
+
}
|
|
138
|
+
await $`docker pause ${ids}`.quiet();
|
|
139
|
+
}
|
|
140
|
+
return await work();
|
|
141
|
+
} finally {
|
|
142
|
+
held.release();
|
|
143
|
+
release();
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
async function settle(copies) {
|
|
147
|
+
const results = await Promise.allSettled(copies.map(async (copy) => copy()));
|
|
148
|
+
return results.flatMap((result) => result.status === "rejected" ? [result.reason] : []);
|
|
149
|
+
}
|
|
150
|
+
var pausedRecord = (worktree) => queueDir(worktree.project.settings, `${worktree.project.name}.paused`);
|
|
151
|
+
function readPending(file) {
|
|
152
|
+
return new Set(existsSync(file) ? readFileSync(file, "utf8").split(`
|
|
153
|
+
`).filter(Boolean) : []);
|
|
154
|
+
}
|
|
155
|
+
function writePending(file, volumes) {
|
|
156
|
+
const text = [...volumes].join(`
|
|
157
|
+
`);
|
|
158
|
+
if (!text)
|
|
159
|
+
return rmSync(file, { force: true });
|
|
160
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
161
|
+
writeFileSync(file, `${text}
|
|
162
|
+
`);
|
|
163
|
+
}
|
|
164
|
+
async function cloneData(worktree, main) {
|
|
165
|
+
return inLine(queueDir(worktree.project.settings, `${worktree.project.name}.clone`), `cloning ${worktree.task}`, (_, running) => console.error(`wt: ${worktree.project.name}'s main is busy ${running || "cloning"}, waiting`), async () => {
|
|
166
|
+
const record = pausedRecord(worktree);
|
|
167
|
+
const recorded = readPending(record);
|
|
168
|
+
const leftover = recorded.size ? (await mainContainers(main, "paused")).filter((id) => recorded.has(id)) : [];
|
|
169
|
+
unpause(leftover);
|
|
170
|
+
rmSync(record, { force: true });
|
|
171
|
+
const warnings = leftover.length ? [`clone: unpaused ${leftover.length} of main's containers a clone cut short had left paused`] : [];
|
|
172
|
+
return [...warnings, ...await cloneInLine(worktree, main)];
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
async function cloneInLine(worktree, main) {
|
|
176
|
+
const warnings = [];
|
|
177
|
+
const pending = [];
|
|
178
|
+
const unfinished = readPending(worktree.files.clonesPending);
|
|
179
|
+
const copyLabel = `wt.clone=${worktree.id}`;
|
|
180
|
+
const stopCopies = () => {
|
|
181
|
+
const ids = Bun.spawnSync(["docker", "ps", "-q", "--filter", `label=${copyLabel}`]).stdout.toString().split(`
|
|
182
|
+
`).filter(Boolean);
|
|
183
|
+
if (ids.length)
|
|
184
|
+
Bun.spawnSync(["docker", "rm", "-f", ...ids], { stdout: "ignore", stderr: "ignore" });
|
|
185
|
+
};
|
|
186
|
+
for (const entry of worktree.project.clone ?? []) {
|
|
187
|
+
const source = join(worktree.project.root, entry);
|
|
188
|
+
if (existsSync(source)) {
|
|
189
|
+
unlinkLegacySymlink(worktree.dir, entry);
|
|
190
|
+
const target = join(worktree.dir, entry);
|
|
191
|
+
if (existsSync(target))
|
|
192
|
+
continue;
|
|
193
|
+
if (!statSync(source).isDirectory())
|
|
194
|
+
throw new WtError(`clone: ${entry} is not a directory`);
|
|
195
|
+
const partial = `/to/${basename(target)}${PARTIAL}`;
|
|
196
|
+
pending.push({
|
|
197
|
+
copy: async () => {
|
|
198
|
+
mkdirSync(dirname(target), { recursive: true });
|
|
199
|
+
await $`docker run --rm --label ${copyLabel} -v ${`${source}:/from:ro`} -v ${`${dirname(target)}:/to`} alpine sh -c ${`rm -rf '${partial}' && mkdir '${partial}' && cp -a /from/. '${partial}/' && mv '${partial}' '/to/${basename(target)}'`}`.quiet();
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
const volume = main.volumes?.[entry];
|
|
205
|
+
if (!volume) {
|
|
206
|
+
warnings.push(`clone: ${entry} is neither a path in main nor a volume of its stack`);
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
if (volume.driver_opts?.device !== undefined && volume.driver_opts.device !== "")
|
|
210
|
+
throw new WtError(`clone: volume ${entry} is a bind to ${volume.driver_opts.device}`, "list that path in clone instead");
|
|
211
|
+
const sourceVolume = volume.name ?? `${main.name}_${entry}`;
|
|
212
|
+
const targetVolume = `${worktree.id}_${entry}`;
|
|
213
|
+
if (await volumeExists(targetVolume)) {
|
|
214
|
+
if (!unfinished.has(targetVolume))
|
|
215
|
+
continue;
|
|
216
|
+
const removed = await $`docker volume rm ${targetVolume}`.quiet().nothrow();
|
|
217
|
+
if (removed.exitCode !== 0)
|
|
218
|
+
throw new WtError(`clone: volume ${targetVolume} was cut short mid-copy and cannot be emptied (${removed.stderr.toString().trim()})`, `\`wt down ${worktree.task}\` then \`wt sync ${worktree.task}\``);
|
|
219
|
+
}
|
|
220
|
+
if (!await volumeExists(sourceVolume)) {
|
|
221
|
+
warnings.push(`clone: main has no volume ${sourceVolume} yet, the worktree starts with an empty one`);
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
pending.push({
|
|
225
|
+
volume: targetVolume,
|
|
226
|
+
copy: async () => {
|
|
227
|
+
await $`docker volume create --label ${`com.docker.compose.project=${worktree.id}`} --label ${`com.docker.compose.volume=${entry}`} ${targetVolume}`.quiet();
|
|
228
|
+
await $`docker run --rm --label ${copyLabel} -v ${`${sourceVolume}:/from:ro`} -v ${`${targetVolume}:/to`} alpine cp -a /from/. /to/`.quiet();
|
|
229
|
+
unfinished.delete(targetVolume);
|
|
230
|
+
}
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
if (!pending.length) {
|
|
234
|
+
writePending(worktree.files.clonesPending, unfinished);
|
|
235
|
+
return warnings;
|
|
236
|
+
}
|
|
237
|
+
for (const { volume } of pending) {
|
|
238
|
+
if (volume !== undefined)
|
|
239
|
+
unfinished.add(volume);
|
|
240
|
+
}
|
|
241
|
+
writePending(worktree.files.clonesPending, unfinished);
|
|
242
|
+
const failures = await whilePaused(await mainContainers(main, "running"), async () => settle(pending.map(({ copy }) => copy)), stopCopies, pausedRecord(worktree));
|
|
243
|
+
writePending(worktree.files.clonesPending, unfinished);
|
|
244
|
+
if (failures.length)
|
|
245
|
+
throw new WtError(`clone failed:
|
|
246
|
+
${failures.map(failureText).join(`
|
|
247
|
+
`)}`, `\`wt sync ${worktree.task}\` starts it again`);
|
|
248
|
+
return warnings;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
export { writeIfChanged, volumeExists, copyFromMain, linkFromMain, writeEnvFiles, cloneData };
|