@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.
Files changed (47) hide show
  1. package/README.md +17 -10
  2. package/dist/cli-02xxc9xk.js +67 -0
  3. package/dist/cli-5khwxy5g.js +36 -0
  4. package/dist/cli-6k7xzgw2.js +519 -0
  5. package/dist/cli-96wy1q8e.js +97 -0
  6. package/dist/cli-9m1jyyrf.js +69 -0
  7. package/dist/cli-fa4fh6ex.js +37 -0
  8. package/dist/cli-fdwwsds1.js +153 -0
  9. package/dist/cli-jb9a8d46.js +674 -0
  10. package/dist/cli-k4471y24.js +779 -0
  11. package/dist/cli-kt6d30zz.js +106 -0
  12. package/dist/cli-q7e6gegw.js +105 -0
  13. package/dist/cli-qwe5ykfs.js +313 -0
  14. package/dist/cli-tp4q5w5x.js +251 -0
  15. package/dist/cli.js +228 -2809
  16. package/dist/commands-jqr92tk6.js +222 -0
  17. package/dist/init-0ws4c883.js +166 -0
  18. package/dist/list-83vq4jng.js +88 -0
  19. package/dist/print-d4aq2eqw.js +77 -0
  20. package/dist/prompt-y71tp3f5.js +436 -0
  21. package/dist/prune-7ex1qyd3.js +17 -0
  22. package/dist/run-jrmm9gg8.js +18 -0
  23. package/dist/shell-1smv2vf9.js +55 -0
  24. package/dist/stack-2zj6r6py.js +20 -0
  25. package/dist/tools-c4m5037s.js +13 -0
  26. package/package.json +4 -4
  27. package/src/cli.ts +250 -69
  28. package/src/commands.ts +51 -38
  29. package/src/compose.ts +37 -14
  30. package/src/config.ts +8 -1
  31. package/src/errors.ts +16 -6
  32. package/src/files.ts +14 -15
  33. package/src/git.ts +53 -7
  34. package/src/index.ts +2 -1
  35. package/src/init.ts +56 -10
  36. package/src/jobs.ts +54 -15
  37. package/src/list.ts +26 -13
  38. package/src/print.ts +76 -0
  39. package/src/project.ts +129 -28
  40. package/src/proxy.ts +5 -5
  41. package/src/prune.ts +149 -0
  42. package/src/queue.ts +27 -3
  43. package/src/run.ts +82 -23
  44. package/src/shell.ts +34 -7
  45. package/src/stack.ts +10 -12
  46. package/src/sync.ts +28 -3
  47. package/src/tools.ts +91 -0
@@ -0,0 +1,37 @@
1
+ // @bun
2
+ import {
3
+ configBase
4
+ } from "./cli-k4471y24.js";
5
+ import {
6
+ WtError
7
+ } from "./cli-6k7xzgw2.js";
8
+
9
+ // src/proxy.ts
10
+ import { existsSync } from "fs";
11
+ import { dirname, join } from "path";
12
+ var {$ } = globalThis.Bun;
13
+ function devproxyArgv(path = process.env.PATH ?? "") {
14
+ const onPath = Bun.which("devproxy", { PATH: path });
15
+ if (onPath !== null)
16
+ return [onPath];
17
+ try {
18
+ return [process.execPath, "--no-env-file", "--config=/dev/null", join(dirname(Bun.resolveSync("@kaynooo/devproxy", import.meta.dir)), "..", "dist", "cli.js")];
19
+ } catch {
20
+ return [];
21
+ }
22
+ }
23
+ function hasDevproxy() {
24
+ return devproxyArgv().length > 0 && existsSync(process.env.DEVPROXY_CONFIG ?? join(configBase(), "devproxy", "config.ts"));
25
+ }
26
+ async function proxyHostList() {
27
+ const argv = devproxyArgv();
28
+ const result = argv.length ? await $`${argv} hosts --json`.quiet().nothrow() : undefined;
29
+ return result?.exitCode === 0 ? JSON.parse(result.stdout.toString()) : [];
30
+ }
31
+ async function devproxyUp() {
32
+ const argv = devproxyArgv();
33
+ if (!argv.length || (await $`${argv} up`.nothrow()).exitCode !== 0)
34
+ throw new WtError("the shared proxy did not come up: `devproxy status` says why");
35
+ }
36
+
37
+ export { devproxyArgv, hasDevproxy, proxyHostList, devproxyUp };
@@ -0,0 +1,153 @@
1
+ // @bun
2
+ import {
3
+ compose
4
+ } from "./cli-jb9a8d46.js";
5
+ import {
6
+ WtError,
7
+ worktreeHost
8
+ } from "./cli-6k7xzgw2.js";
9
+
10
+ // src/compose.ts
11
+ import { existsSync, readFileSync } from "fs";
12
+ import { join } from "path";
13
+ var COMPOSE_FILES = ["compose.yaml", "compose.yml", "docker-compose.yaml", "docker-compose.yml"];
14
+ var SKIP_PROFILE = "wt-skip";
15
+ function composeEnv(overrides = {}) {
16
+ return { ...process.env, COMPOSE_FILE: undefined, COMPOSE_PROJECT_NAME: undefined, ...overrides };
17
+ }
18
+ async function composeConfig(dir, env = {}) {
19
+ const result = await compose(["config", "--format", "json"], { cwd: dir, env: composeEnv(env) });
20
+ if (result.code !== 0)
21
+ throw new WtError(`docker compose config failed in ${dir}:
22
+ ${result.stderr}`);
23
+ return JSON.parse(result.stdout);
24
+ }
25
+ var COMPOSE_FILE_LINE = /^COMPOSE_FILE=(.*)$/m;
26
+ var QUOTES_AROUND = /^["']|["']$/g;
27
+ function baseComposeFiles(dir, overrides) {
28
+ const envFile = join(dir, ".env");
29
+ const line = existsSync(envFile) ? readFileSync(envFile, "utf8").match(COMPOSE_FILE_LINE) : null;
30
+ const files = line ? line[1].replace(QUOTES_AROUND, "").split(":").filter(Boolean) : [];
31
+ const base = files.filter((file) => !file.startsWith(`${overrides}/`));
32
+ if (base.length)
33
+ return base;
34
+ const found = COMPOSE_FILES.find((file) => existsSync(join(dir, file)));
35
+ if (found === undefined)
36
+ throw new WtError(`no compose file in ${dir}`);
37
+ return [found];
38
+ }
39
+ var q = (value) => JSON.stringify(value);
40
+ var DEVPROXY_HOST_LABEL = /^devproxy\.[\w-]+\.host$/;
41
+ var SERVICE = /^[^:]*/;
42
+ function proxyLabels(worktree, config) {
43
+ const { routes, skip = [] } = worktree.project;
44
+ const labels = new Map;
45
+ const add = (service, ...entries) => labels.set(service, [...labels.get(service) ?? [], ...entries]);
46
+ const routed = routes.find((route) => route.to !== "client")?.to;
47
+ const anchor = routed === undefined ? Object.keys(config.services).find((service) => !skip.includes(service)) : SERVICE.exec(routed)[0];
48
+ for (const [index, route] of routes.entries()) {
49
+ const key = `devproxy.wt${index}`;
50
+ const host = [`${key}.host`, worktreeHost(route.host, worktree.task)];
51
+ if (route.to === "client") {
52
+ if (anchor !== undefined)
53
+ add(anchor, host, [`${key}.to`, `host:${worktree.clientPort}`]);
54
+ continue;
55
+ }
56
+ const [service = "", port = ""] = route.to.split(":");
57
+ add(service, host, [`${key}.port`, port], ...route.passthrough ? [[`${key}.passthrough`, "true"]] : []);
58
+ }
59
+ for (const [service, settings] of Object.entries(config.services)) {
60
+ for (const [key, value] of Object.entries(settings.labels ?? {})) {
61
+ if (DEVPROXY_HOST_LABEL.test(key))
62
+ add(service, [key, value.split(",").map((host) => worktreeHost(host.trim(), worktree.task)).join(",")]);
63
+ }
64
+ }
65
+ return labels;
66
+ }
67
+ function renderOverride(worktree, config) {
68
+ const skip = new Set(worktree.project.skip);
69
+ const routeHosts = new Set(worktree.project.routeHosts);
70
+ const labels = proxyLabels(worktree, config);
71
+ const network = worktree.project.settings.proxyNetwork;
72
+ const extraHosts = worktree.project.routes.filter((route) => !route.host.startsWith("*.")).map((route) => ` - ${q(`${worktreeHost(route.host, worktree.task)}:host-gateway`)}`);
73
+ const lines = [`# Generated by wt for ${worktree.id}, rewritten on every \`wt sync\`.`, "services:"];
74
+ let joined = false;
75
+ for (const [name, service] of Object.entries(config.services)) {
76
+ const body = [];
77
+ if (skip.has(name))
78
+ body.push(` profiles: [${q(SKIP_PROFILE)}]`);
79
+ if (service.container_name !== undefined && service.container_name !== "")
80
+ body.push(" container_name: !reset null");
81
+ if ((service.ports?.length ?? 0) > 0)
82
+ body.push(" ports: !reset []");
83
+ if (routeHosts.has(name) && extraHosts.length)
84
+ body.push(" extra_hosts:", ...extraHosts);
85
+ if (labels.has(name))
86
+ body.push(" labels:", ...labels.get(name).map(([key, value]) => ` ${q(key)}: ${q(value)}`));
87
+ const networks = (service.network_mode ?? "") !== "" ? [] : Object.entries(service.networks ?? {});
88
+ const joins = (service.network_mode ?? "") === "" && !skip.has(name) && !(network in (service.networks ?? {})) && labels.get(name)?.some(([key]) => key.endsWith(".port")) === true;
89
+ const networkLines = networks.flatMap(([own, settings]) => {
90
+ if (own === network)
91
+ return (settings?.aliases?.length ?? 0) > 0 ? [` ${network}:`, " aliases: !reset []"] : [];
92
+ if (service.container_name !== undefined && service.container_name !== "")
93
+ return [` ${q(own)}:`, ` aliases: [${q(service.container_name)}]`];
94
+ return joins ? [` ${q(own)}: {}`] : [];
95
+ });
96
+ if (joins) {
97
+ joined = true;
98
+ networkLines.push(` ${network}: {}`);
99
+ }
100
+ if (networkLines.length)
101
+ body.push(" networks:", ...networkLines);
102
+ if (body.length)
103
+ lines.push(` ${q(name)}:`, ...body);
104
+ }
105
+ if (joined && config.networks?.[network] === undefined)
106
+ lines.push("networks:", ` ${network}:`, ` name: ${network}`, " external: true");
107
+ return `${lines.join(`
108
+ `)}
109
+ `;
110
+ }
111
+ function overrideWarnings(worktree, config) {
112
+ const skip = new Set(worktree.project.skip);
113
+ const network = worktree.project.settings.proxyNetwork;
114
+ const warnings = [];
115
+ for (const [name, service] of Object.entries(config.services)) {
116
+ if (skip.has(name))
117
+ continue;
118
+ if (service.network_mode === "host")
119
+ warnings.push(`service ${name} runs on the host network and may collide with main, consider adding it to skip`);
120
+ if (service.networks && network in service.networks && !service.networks[network]?.aliases?.some((alias) => alias !== name))
121
+ warnings.push(`service ${name} has no alias of its own on the proxy network, so the worktree answers to ${name} there too and the shared proxy round-robins between them, give it one in the project override`);
122
+ }
123
+ for (const [key, volume] of Object.entries(config.volumes ?? {})) {
124
+ if (volume.external || volume.name !== undefined && !volume.name.startsWith(`${config.name}_`))
125
+ warnings.push(`volume ${key} is named ${volume.name ?? key} outside the project, main and the worktree share it`);
126
+ }
127
+ return warnings;
128
+ }
129
+ async function unknownServices(project, config) {
130
+ const named = [
131
+ ["skip", project.skip ?? []],
132
+ ["routeHosts", project.routeHosts ?? []],
133
+ ["routes", project.routes.filter((route) => route.to !== "client").map((route) => SERVICE.exec(route.to)[0])],
134
+ ["run.service", project.run ? [project.run.service] : []]
135
+ ];
136
+ const missing = named.flatMap(([setting, names]) => [...new Set(names)].filter((name) => config.services[name] === undefined).map((name) => [setting, name]));
137
+ if (!missing.length)
138
+ return [];
139
+ const profiled = await compose(["--profile", "*", "config", "--services"], { cwd: project.root, env: composeEnv() });
140
+ const every = new Set(profiled.code === 0 ? profiled.stdout.split(`
141
+ `).filter(Boolean) : []);
142
+ return missing.filter(([, name]) => !every.has(name)).map(([setting, name]) => `${project.name}: ${setting} names ${name}, which is not a service of ${config.name}`);
143
+ }
144
+ async function runningProjects(all = false) {
145
+ if (Bun.which("docker") === null)
146
+ return new Set;
147
+ const result = await compose(["ls", ...all ? ["-a"] : [], "--format", "json"]);
148
+ if (result.code !== 0)
149
+ return new Set;
150
+ return new Set(JSON.parse(result.stdout).map((stack) => stack.Name));
151
+ }
152
+
153
+ export { composeEnv, composeConfig, baseComposeFiles, renderOverride, overrideWarnings, unknownServices, runningProjects };