@kaynooo/cli-wt 0.1.2 → 0.1.7
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-0gbs87r4.js +313 -0
- package/dist/cli-1nyesy1b.js +674 -0
- package/dist/cli-4g5ehhgn.js +251 -0
- package/dist/cli-6ybxw5sc.js +97 -0
- package/dist/cli-dmkfxdbr.js +153 -0
- package/dist/cli-dmzvmpge.js +106 -0
- package/dist/cli-e2h20nyy.js +36 -0
- package/dist/cli-j82fak6j.js +69 -0
- package/dist/cli-k4471y24.js +779 -0
- package/dist/cli-kek450vq.js +519 -0
- package/dist/cli-n5rt07v7.js +105 -0
- package/dist/cli-pkvw2k4x.js +37 -0
- package/dist/cli-w4w36hng.js +67 -0
- package/dist/cli.js +238 -2592
- package/dist/commands-jtnbchtb.js +222 -0
- package/dist/init-4ynjrztr.js +166 -0
- package/dist/list-js6drtvg.js +88 -0
- package/dist/print-q6qaqg22.js +77 -0
- package/dist/prompt-y71tp3f5.js +436 -0
- package/dist/prune-beweag7w.js +17 -0
- package/dist/run-7eayrqwy.js +18 -0
- package/dist/shell-xrgvqrpp.js +55 -0
- package/dist/stack-h3pgyx1e.js +20 -0
- package/dist/tools-4j56r47a.js +13 -0
- package/package.json +6 -6
- package/src/cli.ts +270 -76
- package/src/commands.ts +58 -42
- package/src/compose.ts +37 -14
- package/src/config.ts +8 -1
- package/src/errors.ts +31 -5
- package/src/files.ts +15 -23
- package/src/git.ts +53 -7
- package/src/index.ts +3 -2
- package/src/init.ts +56 -10
- package/src/jobs.ts +60 -19
- package/src/list.ts +26 -13
- package/src/print.ts +76 -0
- package/src/project.ts +134 -23
- package/src/proxy.ts +9 -9
- 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,222 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
import {
|
|
3
|
+
warn
|
|
4
|
+
} from "./cli-k4471y24.js";
|
|
5
|
+
import {
|
|
6
|
+
isListening,
|
|
7
|
+
knownMessage
|
|
8
|
+
} from "./cli-1nyesy1b.js";
|
|
9
|
+
import {
|
|
10
|
+
stringToPort,
|
|
11
|
+
WtError,
|
|
12
|
+
loadProjects,
|
|
13
|
+
worktreeOf,
|
|
14
|
+
assertWorktreeExists,
|
|
15
|
+
listTasks,
|
|
16
|
+
currentDir,
|
|
17
|
+
locate,
|
|
18
|
+
locateWorktree
|
|
19
|
+
} from "./cli-kek450vq.js";
|
|
20
|
+
import {
|
|
21
|
+
removeQueues2
|
|
22
|
+
} from "./cli-n5rt07v7.js";
|
|
23
|
+
import {
|
|
24
|
+
forgetTestDatabases2,
|
|
25
|
+
runsDir2
|
|
26
|
+
} from "./cli-0gbs87r4.js";
|
|
27
|
+
import {
|
|
28
|
+
stackUp2,
|
|
29
|
+
stackDown2,
|
|
30
|
+
afterRemove2,
|
|
31
|
+
hasContainers2
|
|
32
|
+
} from "./cli-6ybxw5sc.js";
|
|
33
|
+
import {
|
|
34
|
+
assertTools2
|
|
35
|
+
} from "./cli-w4w36hng.js";
|
|
36
|
+
import {
|
|
37
|
+
writeStackCerts,
|
|
38
|
+
urlsOf
|
|
39
|
+
} from "./cli-e2h20nyy.js";
|
|
40
|
+
import {
|
|
41
|
+
writeIfChanged,
|
|
42
|
+
copyFromMain,
|
|
43
|
+
linkFromMain,
|
|
44
|
+
writeEnvFiles,
|
|
45
|
+
cloneData
|
|
46
|
+
} from "./cli-4g5ehhgn.js";
|
|
47
|
+
import {
|
|
48
|
+
holders
|
|
49
|
+
} from "./cli-j82fak6j.js";
|
|
50
|
+
import"./cli-pkvw2k4x.js";
|
|
51
|
+
import {
|
|
52
|
+
addWorktrees,
|
|
53
|
+
removeWorktrees,
|
|
54
|
+
dirtyRepos,
|
|
55
|
+
unreachableCommits,
|
|
56
|
+
isRegistered
|
|
57
|
+
} from "./cli-dmzvmpge.js";
|
|
58
|
+
import {
|
|
59
|
+
composeConfig,
|
|
60
|
+
baseComposeFiles,
|
|
61
|
+
renderOverride,
|
|
62
|
+
overrideWarnings,
|
|
63
|
+
unknownServices,
|
|
64
|
+
runningProjects
|
|
65
|
+
} from "./cli-dmkfxdbr.js";
|
|
66
|
+
|
|
67
|
+
// src/commands.ts
|
|
68
|
+
import { existsSync, rmSync } from "fs";
|
|
69
|
+
var {$ } = globalThis.Bun;
|
|
70
|
+
|
|
71
|
+
// src/sync.ts
|
|
72
|
+
async function syncWorktree(worktree) {
|
|
73
|
+
assertWorktreeExists(worktree);
|
|
74
|
+
const [warnings, main] = await Promise.all([copyFromMain(worktree), composeConfig(worktree.project.root)]);
|
|
75
|
+
warnings.push(...await unknownServices(worktree.project, main));
|
|
76
|
+
linkFromMain(worktree);
|
|
77
|
+
writeEnvFiles(worktree);
|
|
78
|
+
warnings.push(...await cloneData(worktree, main));
|
|
79
|
+
const config = await composeConfig(worktree.dir, { COMPOSE_FILE: baseComposeFiles(worktree.dir, worktree.project.settings.overrides).join(":"), COMPOSE_PROJECT_NAME: worktree.id });
|
|
80
|
+
writeIfChanged(worktree.files.override, renderOverride(worktree, config));
|
|
81
|
+
warnings.push(...overrideWarnings(worktree, config));
|
|
82
|
+
await composeConfig(worktree.dir);
|
|
83
|
+
await writeStackCerts(worktree);
|
|
84
|
+
warnings.push(...await portClashes(worktree));
|
|
85
|
+
for (const warning of warnings)
|
|
86
|
+
warn(warning);
|
|
87
|
+
}
|
|
88
|
+
async function portClashes(worktree) {
|
|
89
|
+
const port = worktree.clientPort;
|
|
90
|
+
const sharing = [];
|
|
91
|
+
for (const project of await loadProjects(() => {
|
|
92
|
+
return;
|
|
93
|
+
})) {
|
|
94
|
+
if (stringToPort(project.name) === port)
|
|
95
|
+
sharing.push(`${project.name}'s main`);
|
|
96
|
+
for (const task of listTasks(project)) {
|
|
97
|
+
const other = worktreeOf(project, task);
|
|
98
|
+
if (other.id !== worktree.id && other.clientPort === port)
|
|
99
|
+
sharing.push(`${project.name}/${task}`);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return sharing.length ? [`dev server port ${port} of ${worktree.task} is also the one of ${sharing.join(", ")}: give one of them its own in the manifest's ports`] : [];
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// src/commands.ts
|
|
106
|
+
var ANONYMOUS_VOLUME = /^[0-9a-f]{64}$/;
|
|
107
|
+
var WHITESPACE = /\s+/;
|
|
108
|
+
async function anonymousVolumes(worktree) {
|
|
109
|
+
const containers = (await $`docker ps -aq --filter ${`label=com.docker.compose.project=${worktree.id}`}`.quiet().text()).split(`
|
|
110
|
+
`).filter(Boolean);
|
|
111
|
+
if (!containers.length)
|
|
112
|
+
return [];
|
|
113
|
+
const mounts = await $`docker inspect --format ${'{{range .Mounts}}{{if eq .Type "volume"}}{{.Name}} {{end}}{{end}}'} ${containers}`.quiet().nothrow();
|
|
114
|
+
return mounts.text().split(WHITESPACE).filter((name) => ANONYMOUS_VOLUME.test(name));
|
|
115
|
+
}
|
|
116
|
+
async function removeVolumes(worktree, anonymous) {
|
|
117
|
+
const named = (await $`docker volume ls -q --filter ${`label=com.docker.compose.project=${worktree.id}`}`.quiet().text()).split(`
|
|
118
|
+
`).filter((volume) => volume.startsWith(`${worktree.id}_`));
|
|
119
|
+
const volumes = [...new Set([...named, ...anonymous])];
|
|
120
|
+
if (!volumes.length)
|
|
121
|
+
return;
|
|
122
|
+
const removed = await $`docker volume rm ${volumes}`.quiet().nothrow();
|
|
123
|
+
if (removed.exitCode !== 0)
|
|
124
|
+
throw new WtError(`could not remove the volumes of ${worktree.id}:
|
|
125
|
+
${removed.stderr.toString().trim()}`);
|
|
126
|
+
}
|
|
127
|
+
async function addTask(task, branch, options) {
|
|
128
|
+
const { project } = await locate(currentDir(), options.project);
|
|
129
|
+
const worktree = worktreeOf(project, task);
|
|
130
|
+
const checkout = branch ?? `wt/${task}`;
|
|
131
|
+
await assertTools2(project);
|
|
132
|
+
if (isListening(worktree.clientPort))
|
|
133
|
+
warn(`dev server port ${worktree.clientPort} of ${task} is in use already: give it its own in the manifest's ports`);
|
|
134
|
+
const starts = await addWorktrees(worktree, checkout, options.from);
|
|
135
|
+
try {
|
|
136
|
+
await syncWorktree(worktree);
|
|
137
|
+
} catch (error) {
|
|
138
|
+
const way = `the worktree is checked out but not ready: \`wt sync ${task} -p ${project.name}\` resumes it, \`wt rm ${task} -p ${project.name}\` removes it`;
|
|
139
|
+
const message = knownMessage(error);
|
|
140
|
+
if (message !== undefined)
|
|
141
|
+
throw new WtError(message, way);
|
|
142
|
+
warn(way);
|
|
143
|
+
throw error;
|
|
144
|
+
}
|
|
145
|
+
console.log(`wt: created ${worktree.dir}${project.repos.length ? "" : ` ${starts[0]}`}`);
|
|
146
|
+
if (project.repos.length) {
|
|
147
|
+
for (const start of starts)
|
|
148
|
+
console.log(` ${start}`);
|
|
149
|
+
}
|
|
150
|
+
for (const url of urlsOf(worktree))
|
|
151
|
+
console.log(` ${url}`);
|
|
152
|
+
if (options.up)
|
|
153
|
+
await stackUp2(worktree);
|
|
154
|
+
else
|
|
155
|
+
console.log(` wt up ${task}`);
|
|
156
|
+
}
|
|
157
|
+
async function syncTask(task, options) {
|
|
158
|
+
const worktree = await locateWorktree(currentDir(), task, options.project);
|
|
159
|
+
await syncWorktree(worktree);
|
|
160
|
+
console.log(`wt: synced ${worktree.id}`);
|
|
161
|
+
}
|
|
162
|
+
function refuseWhileRun(worktree) {
|
|
163
|
+
const runs = holders(runsDir2(worktree));
|
|
164
|
+
if (runs.length)
|
|
165
|
+
throw new WtError(`${worktree.task} is in use by wt run (${runs.map((command) => `\`${command}\``).join(", ")})`, "remove it once that ends");
|
|
166
|
+
}
|
|
167
|
+
async function refuseLosingWork(worktree, force) {
|
|
168
|
+
if (force || !existsSync(worktree.dir))
|
|
169
|
+
return;
|
|
170
|
+
const dirty = await dirtyRepos(worktree);
|
|
171
|
+
if (dirty.length)
|
|
172
|
+
throw new WtError(`uncommitted changes in ${dirty.join(", ")}`, "commit them, or pass --force to drop them");
|
|
173
|
+
const unreachable = await unreachableCommits(worktree);
|
|
174
|
+
if (unreachable.length)
|
|
175
|
+
throw new WtError(`commits on no branch in ${unreachable.join(", ")} would be lost, \`git switch -c <branch>\` there or pass --force`);
|
|
176
|
+
}
|
|
177
|
+
async function assertRemovable(task, options) {
|
|
178
|
+
const worktree = await locateWorktree(currentDir(), task, options.project);
|
|
179
|
+
if (!existsSync(worktree.dir) && !Object.values(worktree.files).some((file) => existsSync(file)) && !await isRegistered(worktree))
|
|
180
|
+
throw new WtError(`no worktree ${worktree.task} of ${worktree.project.name}, nothing removed`);
|
|
181
|
+
refuseWhileRun(worktree);
|
|
182
|
+
await refuseLosingWork(worktree, options.force === true);
|
|
183
|
+
return worktree;
|
|
184
|
+
}
|
|
185
|
+
async function removeTask(task, options) {
|
|
186
|
+
const worktree = await locateWorktree(currentDir(), task, options.project);
|
|
187
|
+
process.chdir(worktree.project.root);
|
|
188
|
+
const anonymous = await anonymousVolumes(worktree);
|
|
189
|
+
refuseWhileRun(worktree);
|
|
190
|
+
await refuseLosingWork(worktree, options.force === true);
|
|
191
|
+
if (existsSync(worktree.dir) || await hasContainers2(worktree.id))
|
|
192
|
+
await stackDown2(worktree);
|
|
193
|
+
await removeVolumes(worktree, anonymous);
|
|
194
|
+
for (const file of Object.values(worktree.files))
|
|
195
|
+
rmSync(file, { force: true });
|
|
196
|
+
await forgetTestDatabases2(worktree);
|
|
197
|
+
removeQueues2(worktree);
|
|
198
|
+
await removeWorktrees(worktree);
|
|
199
|
+
await afterRemove2(worktree);
|
|
200
|
+
console.log(`wt: removed ${worktree.id}, its branches are kept`);
|
|
201
|
+
}
|
|
202
|
+
async function upTask(worktree) {
|
|
203
|
+
assertWorktreeExists(worktree);
|
|
204
|
+
await stackUp2(worktree);
|
|
205
|
+
}
|
|
206
|
+
async function assertStoppable(worktree) {
|
|
207
|
+
if (!existsSync(worktree.dir) && !(await runningProjects(true)).has(worktree.id))
|
|
208
|
+
assertWorktreeExists(worktree);
|
|
209
|
+
}
|
|
210
|
+
async function downTask(worktree) {
|
|
211
|
+
await assertStoppable(worktree);
|
|
212
|
+
await stackDown2(worktree);
|
|
213
|
+
}
|
|
214
|
+
export {
|
|
215
|
+
addTask,
|
|
216
|
+
assertRemovable,
|
|
217
|
+
assertStoppable,
|
|
218
|
+
downTask,
|
|
219
|
+
removeTask,
|
|
220
|
+
syncTask,
|
|
221
|
+
upTask
|
|
222
|
+
};
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
import {
|
|
3
|
+
warn
|
|
4
|
+
} from "./cli-k4471y24.js";
|
|
5
|
+
import"./cli-1nyesy1b.js";
|
|
6
|
+
import {
|
|
7
|
+
wtConfig,
|
|
8
|
+
stringToPort,
|
|
9
|
+
WtError
|
|
10
|
+
} from "./cli-kek450vq.js";
|
|
11
|
+
import {
|
|
12
|
+
volumeExists
|
|
13
|
+
} from "./cli-4g5ehhgn.js";
|
|
14
|
+
import"./cli-j82fak6j.js";
|
|
15
|
+
import {
|
|
16
|
+
hasDevproxy,
|
|
17
|
+
proxyHostList
|
|
18
|
+
} from "./cli-pkvw2k4x.js";
|
|
19
|
+
import {
|
|
20
|
+
git
|
|
21
|
+
} from "./cli-dmzvmpge.js";
|
|
22
|
+
import {
|
|
23
|
+
composeConfig
|
|
24
|
+
} from "./cli-dmkfxdbr.js";
|
|
25
|
+
|
|
26
|
+
// src/init.ts
|
|
27
|
+
import { existsSync, mkdirSync, readdirSync, statSync, writeFileSync } from "fs";
|
|
28
|
+
import { homedir } from "os";
|
|
29
|
+
import { basename, join, relative } from "path";
|
|
30
|
+
var CERT_PATH = /cert/i;
|
|
31
|
+
var HTTP_TARGET = /^(?:https?:\/\/)?([\w.-]+):(\d+)$/;
|
|
32
|
+
var PASSTHROUGH_TARGET = /^([\w.-]+):(\d+)$/;
|
|
33
|
+
async function ignoredPaths(repo, paths) {
|
|
34
|
+
if (!paths.length)
|
|
35
|
+
return [];
|
|
36
|
+
const matched = new Set((await git(repo, "check-ignore", ...paths)).out.split(`
|
|
37
|
+
`));
|
|
38
|
+
return paths.filter((path) => matched.has(path));
|
|
39
|
+
}
|
|
40
|
+
function mountedPaths(config) {
|
|
41
|
+
const binds = Object.values(config.services).flatMap((service) => (service.volumes ?? []).filter((v) => v.type === "bind").map((v) => v.source));
|
|
42
|
+
const devices = Object.values(config.volumes ?? {}).flatMap((volume) => volume.driver_opts?.device ?? []);
|
|
43
|
+
return [...new Set([...binds, ...devices])];
|
|
44
|
+
}
|
|
45
|
+
async function findRoutes(config, name, network) {
|
|
46
|
+
const serviceOf = (target) => Object.entries(config.services).find(([service, settings]) => settings.container_name === target || `${config.name}-${service}-1` === target || settings.networks?.[network]?.aliases?.includes(target))?.[0];
|
|
47
|
+
const routes = [];
|
|
48
|
+
const hosts = await proxyHostList();
|
|
49
|
+
for (const { host, group, to, passthrough } of hosts) {
|
|
50
|
+
const [, target = "", port = ""] = (passthrough ? PASSTHROUGH_TARGET : HTTP_TARGET).exec(to) ?? [];
|
|
51
|
+
const service = serviceOf(target);
|
|
52
|
+
if (target === "host" && Number(port) === stringToPort(name))
|
|
53
|
+
routes.push({ host, to: "client" });
|
|
54
|
+
else if (service !== undefined)
|
|
55
|
+
routes.push({ host, to: `${service}:${Number(port)}`, ...passthrough ? { passthrough } : {} });
|
|
56
|
+
else if (group === `docker-${config.name}`)
|
|
57
|
+
warn(`devproxy serves ${host} from ${config.name}'s labels, to ${to}, which is no service of it: add its route by hand`);
|
|
58
|
+
}
|
|
59
|
+
if (!hasDevproxy())
|
|
60
|
+
warn("devproxy is not installed or has no config, so the draft has no routes: write them by hand, or set devproxy up and run init again");
|
|
61
|
+
else if (!hosts.length)
|
|
62
|
+
warn("devproxy serves no hosts, so the draft has no routes: start main's stack for devproxy to read its labels and run init again, or write them by hand");
|
|
63
|
+
else if (!routes.length)
|
|
64
|
+
warn(`none of the hosts devproxy serves leads to ${config.name}, so the draft has no routes: start main's stack and run init again, or write them by hand`);
|
|
65
|
+
return routes;
|
|
66
|
+
}
|
|
67
|
+
async function secretsToCopy(root, repos) {
|
|
68
|
+
const perBase = await Promise.all([".", ...repos].map(async (base) => {
|
|
69
|
+
const envFiles = readdirSync(join(root, base)).filter((entry) => (entry.startsWith(".env") || entry === ".nuxtrc") && statSync(join(root, base, entry)).isFile());
|
|
70
|
+
const jwt = base !== "." && existsSync(join(root, base, "config", "jwt")) ? [join(base, "config", "jwt")] : [];
|
|
71
|
+
return [...(await ignoredPaths(join(root, base), envFiles)).map((entry) => join(base, entry)), ...jwt];
|
|
72
|
+
}));
|
|
73
|
+
return perBase.flat();
|
|
74
|
+
}
|
|
75
|
+
async function draftFor(dir, given) {
|
|
76
|
+
const top = await git(dir, "rev-parse", "--show-toplevel");
|
|
77
|
+
if (!top.ok)
|
|
78
|
+
throw new WtError(`${dir} is not inside a git repository`);
|
|
79
|
+
const root = top.out;
|
|
80
|
+
const config = await composeConfig(root);
|
|
81
|
+
const name = given ?? basename(root);
|
|
82
|
+
const mounted = mountedPaths(config).filter((path) => path.startsWith(`${root}/`)).map((path) => relative(root, path));
|
|
83
|
+
const repos = readdirSync(root).filter((entry) => existsSync(join(root, entry, ".git")) && mounted.some((path) => path === entry || path.startsWith(`${entry}/`)));
|
|
84
|
+
const inRepo = (path) => repos.some((repo) => path === repo || path.startsWith(`${repo}/`));
|
|
85
|
+
const volumes = Object.entries(config.volumes ?? {}).filter(([, volume]) => (volume.driver_opts?.device ?? "") === "");
|
|
86
|
+
const [secrets, ignoredMounts, volumesExist] = await Promise.all([
|
|
87
|
+
secretsToCopy(root, repos),
|
|
88
|
+
ignoredPaths(root, mounted.filter((path) => !inRepo(path) && existsSync(join(root, path)))),
|
|
89
|
+
Promise.all(volumes.map(async ([key, volume]) => volumeExists(volume.name ?? `${config.name}_${key}`)))
|
|
90
|
+
]);
|
|
91
|
+
const routes = await findRoutes(config, name, (await wtConfig()).proxyNetwork);
|
|
92
|
+
const certs = routes.some((route) => route.passthrough) ? mounted.find((path) => CERT_PATH.test(path)) : undefined;
|
|
93
|
+
return {
|
|
94
|
+
name,
|
|
95
|
+
root: fromHome(root, homedir()),
|
|
96
|
+
repos,
|
|
97
|
+
copy: [...secrets, ...ignoredMounts.filter((path) => CERT_PATH.test(path))],
|
|
98
|
+
clone: [...ignoredMounts.filter((path) => !CERT_PATH.test(path)), ...volumes.filter((_, index) => volumesExist[index]).map(([key]) => key)],
|
|
99
|
+
skip: Object.entries(config.services).filter(([, service]) => service.network_mode === "host").map(([service]) => service),
|
|
100
|
+
...certs !== undefined && { certs },
|
|
101
|
+
routes
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
function fromHome(path, home) {
|
|
105
|
+
return path === home || path.startsWith(`${home}/`) ? `~${path.slice(home.length)}` : path;
|
|
106
|
+
}
|
|
107
|
+
var QUOTED = /['\\\n\r]/g;
|
|
108
|
+
var ESCAPES = { "'": "\\'", "\\": "\\\\", "\n": "\\n", "\r": "\\r" };
|
|
109
|
+
var str = (value) => `'${value.replace(QUOTED, (char) => ESCAPES[char])}'`;
|
|
110
|
+
var list = (values) => `[${values.map(str).join(", ")}]`;
|
|
111
|
+
function render(draft) {
|
|
112
|
+
const routes = draft.routes.map((route) => ` { host: ${str(route.host)}, to: ${str(route.to)}${route.passthrough ? ", passthrough: true" : ""} },`);
|
|
113
|
+
return [
|
|
114
|
+
`import type { ProjectConfig } from '@kaynooo/cli-wt'`,
|
|
115
|
+
"",
|
|
116
|
+
"// Drafted by `wt init`: check every list against the stack before the first `wt add`.",
|
|
117
|
+
"export default {",
|
|
118
|
+
` root: ${str(draft.root)},`,
|
|
119
|
+
` repos: ${list(draft.repos)},`,
|
|
120
|
+
` copy: ${list(draft.copy)},`,
|
|
121
|
+
` clone: ${list(draft.clone)},`,
|
|
122
|
+
...draft.skip.length ? [` skip: ${list(draft.skip)},`] : [],
|
|
123
|
+
...draft.certs !== undefined ? [` certs: ${str(draft.certs)},`] : [],
|
|
124
|
+
" setup: [],",
|
|
125
|
+
" routes: [",
|
|
126
|
+
...routes,
|
|
127
|
+
" ],",
|
|
128
|
+
"} satisfies ProjectConfig",
|
|
129
|
+
""
|
|
130
|
+
].join(`
|
|
131
|
+
`);
|
|
132
|
+
}
|
|
133
|
+
async function initProject(dir, options) {
|
|
134
|
+
const draft = await draftFor(dir, options.name);
|
|
135
|
+
const content = render(draft);
|
|
136
|
+
if (options.print)
|
|
137
|
+
return console.log(content);
|
|
138
|
+
const { manifests } = await wtConfig();
|
|
139
|
+
const file = join(manifests, `${draft.name}.ts`);
|
|
140
|
+
mkdirSync(manifests, { recursive: true });
|
|
141
|
+
if (existsSync(file))
|
|
142
|
+
throw new WtError(`${file} already exists`, "compare it with `wt init --print`");
|
|
143
|
+
writeFileSync(file, content);
|
|
144
|
+
writeTsconfig(manifests);
|
|
145
|
+
console.log(`wt: drafted ${file}, review it before \`wt add\``);
|
|
146
|
+
}
|
|
147
|
+
var PACKAGE_ROOT = join(import.meta.dir, "..");
|
|
148
|
+
function writeTsconfig(manifests) {
|
|
149
|
+
const file = join(manifests, "tsconfig.json");
|
|
150
|
+
if (existsSync(file))
|
|
151
|
+
return;
|
|
152
|
+
const compilerOptions = {
|
|
153
|
+
target: "ESNext",
|
|
154
|
+
module: "Preserve",
|
|
155
|
+
moduleResolution: "bundler",
|
|
156
|
+
strict: true,
|
|
157
|
+
noEmit: true,
|
|
158
|
+
skipLibCheck: true,
|
|
159
|
+
paths: { "@kaynooo/cli-wt": [join(PACKAGE_ROOT, "src", "index.ts")] }
|
|
160
|
+
};
|
|
161
|
+
writeFileSync(file, `${JSON.stringify({ compilerOptions }, null, 2)}
|
|
162
|
+
`);
|
|
163
|
+
}
|
|
164
|
+
export {
|
|
165
|
+
initProject
|
|
166
|
+
};
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
import {
|
|
3
|
+
table,
|
|
4
|
+
printJson
|
|
5
|
+
} from "./cli-k4471y24.js";
|
|
6
|
+
import"./cli-1nyesy1b.js";
|
|
7
|
+
import {
|
|
8
|
+
slugOf,
|
|
9
|
+
wtJobs,
|
|
10
|
+
loadProjects,
|
|
11
|
+
stackName,
|
|
12
|
+
worktreeOf,
|
|
13
|
+
listTasks,
|
|
14
|
+
currentDir,
|
|
15
|
+
locate
|
|
16
|
+
} from "./cli-kek450vq.js";
|
|
17
|
+
import {
|
|
18
|
+
urlsOf
|
|
19
|
+
} from "./cli-e2h20nyy.js";
|
|
20
|
+
import {
|
|
21
|
+
git,
|
|
22
|
+
unreachableCount,
|
|
23
|
+
currentBranch
|
|
24
|
+
} from "./cli-dmzvmpge.js";
|
|
25
|
+
import {
|
|
26
|
+
runningProjects
|
|
27
|
+
} from "./cli-dmkfxdbr.js";
|
|
28
|
+
|
|
29
|
+
// src/list.ts
|
|
30
|
+
import { basename, join } from "path";
|
|
31
|
+
var UNSAFE_IN_SESSION = /[.:\s/]/g;
|
|
32
|
+
var sessionOf = (name) => name.replace(UNSAFE_IN_SESSION, "-");
|
|
33
|
+
async function projectsToList(options) {
|
|
34
|
+
let here = null;
|
|
35
|
+
try {
|
|
36
|
+
here = (await locate(currentDir(), options.project)).project;
|
|
37
|
+
} catch (error) {
|
|
38
|
+
if (options.project !== undefined)
|
|
39
|
+
throw error;
|
|
40
|
+
}
|
|
41
|
+
const everywhere = options.all === true || !here && !options.json;
|
|
42
|
+
return { here, projects: everywhere ? await loadProjects() : [here].filter((project) => project !== null) };
|
|
43
|
+
}
|
|
44
|
+
function changedFiles(outputs) {
|
|
45
|
+
return outputs.flatMap((out) => out.split(`
|
|
46
|
+
`)).filter((line) => line.trim() !== "").length;
|
|
47
|
+
}
|
|
48
|
+
async function dirtyCount(dir, repos) {
|
|
49
|
+
const statuses = await Promise.all([".", ...repos].map(async (repo) => git(join(dir, repo), "--no-optional-locks", "status", "--porcelain")));
|
|
50
|
+
return changedFiles(statuses.filter((status) => status.ok).map((status) => status.out));
|
|
51
|
+
}
|
|
52
|
+
async function checkoutsOf(project, running, jobs, states) {
|
|
53
|
+
const branchDir = project.repos[0] ?? ".";
|
|
54
|
+
const mainHost = project.routes.find((route) => !route.host.startsWith("*."))?.host;
|
|
55
|
+
const main = { id: stackName(project), task: "main", dir: project.root, url: mainHost !== undefined ? `https://${mainHost}` : "" };
|
|
56
|
+
const worktrees = listTasks(project).map((task) => {
|
|
57
|
+
const worktree = worktreeOf(project, task);
|
|
58
|
+
return { id: worktree.id, task, dir: worktree.dir, url: urlsOf(worktree)[0] ?? "" };
|
|
59
|
+
});
|
|
60
|
+
return Promise.all([main, ...worktrees].map(async ({ id, task, dir, url }) => ({
|
|
61
|
+
project: project.name,
|
|
62
|
+
task,
|
|
63
|
+
dir,
|
|
64
|
+
branch: await currentBranch(join(dir, branchDir)),
|
|
65
|
+
url,
|
|
66
|
+
running: running.has(id),
|
|
67
|
+
dirty: await dirtyCount(dir, project.repos),
|
|
68
|
+
unbranched: task === "main" ? 0 : await unreachableCount(dir, project.repos),
|
|
69
|
+
session: sessionOf(task === "main" ? basename(dir) : id),
|
|
70
|
+
job: jobs.summary(states.get(slugOf(`${project.name}/${task}`)) ?? {})
|
|
71
|
+
})));
|
|
72
|
+
}
|
|
73
|
+
async function listWorktrees(options) {
|
|
74
|
+
const [running, { here, projects }, jobs] = await Promise.all([runningProjects(), projectsToList(options), wtJobs()]);
|
|
75
|
+
const states = jobs.states();
|
|
76
|
+
const checkouts = (await Promise.all(projects.map(async (project) => checkoutsOf(project, running, jobs, states)))).flat();
|
|
77
|
+
if (options.json) {
|
|
78
|
+
printJson({ project: here?.name ?? null, checkouts });
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
if (!checkouts.length)
|
|
82
|
+
return;
|
|
83
|
+
const rows = checkouts.map((checkout) => [checkout.running ? "*" : "", checkout.project, checkout.task, checkout.branch, checkout.url, checkout.job, [checkout.dirty ? `${checkout.dirty} changed` : "", checkout.unbranched ? `${checkout.unbranched} on no branch` : ""].filter(Boolean).join(", ")]);
|
|
84
|
+
console.log(table([["up", "project", "task", "branch", "url", "job", "changes"], ...rows], { header: true }));
|
|
85
|
+
}
|
|
86
|
+
export {
|
|
87
|
+
listWorktrees
|
|
88
|
+
};
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
import {
|
|
3
|
+
warn
|
|
4
|
+
} from "./cli-k4471y24.js";
|
|
5
|
+
import {
|
|
6
|
+
openUrl
|
|
7
|
+
} from "./cli-1nyesy1b.js";
|
|
8
|
+
import {
|
|
9
|
+
slugOf,
|
|
10
|
+
WtError,
|
|
11
|
+
worktreeOf,
|
|
12
|
+
assertWorktreeExists,
|
|
13
|
+
listTasks,
|
|
14
|
+
currentDir,
|
|
15
|
+
locate,
|
|
16
|
+
locateWorktree
|
|
17
|
+
} from "./cli-kek450vq.js";
|
|
18
|
+
import {
|
|
19
|
+
urlsOf
|
|
20
|
+
} from "./cli-e2h20nyy.js";
|
|
21
|
+
|
|
22
|
+
// src/print.ts
|
|
23
|
+
import { existsSync } from "fs";
|
|
24
|
+
async function printJobLog(worktree, jobs) {
|
|
25
|
+
const ref = `${worktree.project.name}/${worktree.task}`;
|
|
26
|
+
const slug = slugOf(ref);
|
|
27
|
+
const { log } = jobs.paths(slug);
|
|
28
|
+
if (!existsSync(log))
|
|
29
|
+
throw new WtError(`no job of ${ref} ran with --bg, which alone keeps a log`);
|
|
30
|
+
const state = jobs.states().get(slug) ?? {};
|
|
31
|
+
if (state.running?.log === "")
|
|
32
|
+
warn(`${ref} is ${jobs.summary(state)} in a terminal; this log is of the last --bg one`);
|
|
33
|
+
else if (state.running === undefined && state.last?.log === "")
|
|
34
|
+
warn(`the last job (${jobs.summary(state)}) ran in a terminal; this log is of the last --bg one`);
|
|
35
|
+
await Bun.write(Bun.stdout, Bun.file(log));
|
|
36
|
+
}
|
|
37
|
+
async function printUrls(task, options) {
|
|
38
|
+
for (const url of urlsOf(await locateWorktree(currentDir(), task, options.project)))
|
|
39
|
+
console.log(url);
|
|
40
|
+
}
|
|
41
|
+
async function printPath(task, options) {
|
|
42
|
+
const { project } = await locate(currentDir(), options.project);
|
|
43
|
+
if (task === undefined || task === "main")
|
|
44
|
+
return console.log(project.root);
|
|
45
|
+
const worktree = worktreeOf(project, task);
|
|
46
|
+
assertWorktreeExists(worktree);
|
|
47
|
+
console.log(worktree.dir);
|
|
48
|
+
}
|
|
49
|
+
async function openTask(task, options) {
|
|
50
|
+
const here = await locate(currentDir(), options.project);
|
|
51
|
+
const name = task ?? here.task;
|
|
52
|
+
let url;
|
|
53
|
+
if (name === "main") {
|
|
54
|
+
const host = here.project.routes.find((route) => !route.host.startsWith("*."))?.host;
|
|
55
|
+
url = host === undefined ? undefined : `https://${host}`;
|
|
56
|
+
} else {
|
|
57
|
+
const worktree = worktreeOf(here.project, name);
|
|
58
|
+
assertWorktreeExists(worktree);
|
|
59
|
+
url = urlsOf(worktree)[0];
|
|
60
|
+
}
|
|
61
|
+
if (url === undefined)
|
|
62
|
+
throw new WtError(`${here.project.name} routes no host, so ${name} has no URL to open`);
|
|
63
|
+
if (!openUrl(url))
|
|
64
|
+
throw new WtError(`no browser could be started for ${url}`);
|
|
65
|
+
console.log(`wt: opened ${url}`);
|
|
66
|
+
}
|
|
67
|
+
async function printTasks(options) {
|
|
68
|
+
for (const task of listTasks((await locate(currentDir(), options.project)).project))
|
|
69
|
+
console.log(task);
|
|
70
|
+
}
|
|
71
|
+
export {
|
|
72
|
+
openTask,
|
|
73
|
+
printJobLog,
|
|
74
|
+
printPath,
|
|
75
|
+
printTasks,
|
|
76
|
+
printUrls
|
|
77
|
+
};
|