@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
package/README.md
CHANGED
|
@@ -12,24 +12,30 @@ bun install -g @kaynooo/cli-wt
|
|
|
12
12
|
|
|
13
13
|
| Command | What it does |
|
|
14
14
|
| --- | --- |
|
|
15
|
-
| `wt add <task> [branch]` | creates the worktree on `branch` (`wt/<task>`), copies and clones from main, routes its subdomains |
|
|
15
|
+
| `wt add <task> [branch] [--from <ref>] [--up]` | creates the worktree on `branch` (`wt/<task>`), a new one forked from main's current commit or `--from`, copies and clones from main, routes its subdomains; `--up` starts it too |
|
|
16
16
|
| `wt sync [task]` | provisions it again after a manifest change |
|
|
17
|
-
| `wt up [task]` / `wt down [task]` | starts or stops its stack |
|
|
18
|
-
| `wt rm <task>` | stops the stack, deletes the checkouts, volumes, certificates and override, keeps the branches; refuses uncommitted changes or commits on no branch unless `--force` |
|
|
19
|
-
| `wt
|
|
17
|
+
| `wt up [task]` / `wt down [task]` | starts or stops its stack, `--all` every worktree's in turn; `down` also stops a stack whose folder is gone |
|
|
18
|
+
| `wt rm <task>` | stops the stack, deletes the checkouts, volumes, certificates and override, keeps the branches; refuses uncommitted changes or commits on no branch unless `--force`; gitignored files (a copied `.env.local` edited since, uploads) go with the folder unchecked |
|
|
19
|
+
| `wt open [task]` | opens its main URL in the browser |
|
|
20
|
+
| `wt logs [task] [-f]` | prints the log of its last `--bg` job, or with `-f` follows its stack's compose logs |
|
|
21
|
+
| `wt prune [-a] [-y]` | lists what worktrees deleted by hand left behind (files, job records, volumes, git registrations) and removes it once confirmed |
|
|
22
|
+
| `wt ls` | main and the worktrees of the current project (`-a` every project), with their last job and changed files |
|
|
23
|
+
| `wt cd [task]` | goes to a worktree, or main, through the shell function of `wt shell` |
|
|
20
24
|
| `wt path [task]` | the directory of a worktree, or of main |
|
|
25
|
+
| `wt url [task]` / `wt tasks` | its URLs; the worktree names of the project, one per line |
|
|
26
|
+
| `wt doctor` | lists the programs wt runs (git, docker, compose, mkcert, devproxy, notify-send), where each is or how to get it; `wt add` refuses before changing anything when one it needs is missing |
|
|
21
27
|
| `wt init [dir]` | drafts a manifest from the compose project in `dir` into the manifests folder (`--print` to stdout) |
|
|
22
28
|
| `wt run <task> -- <cmd>` | runs a command in a throwaway container of the manifest's `run.service`, against a test database of the worktree's own |
|
|
23
29
|
| `wt heavy -- <cmd>` | runs a command once the heavy ones queued before it, from any session, are done |
|
|
24
|
-
| `wt shell fish` | the shell function giving `wt cd <task>`, and `wt rm` leaving the folder it removed |
|
|
30
|
+
| `wt shell fish\|bash\|zsh` | the shell function giving `wt cd <task>`, and `wt rm` leaving the folder it removed, with the completion |
|
|
25
31
|
|
|
26
32
|
A task defaults to the worktree the current directory is in, and `-p <name>` picks the project when the current directory is in none. `add`, `rm`, `up` and `down` take `--bg` to run detached, ending with a desktop notification. One action runs per stack at a time: a second one is refused, naming what holds it.
|
|
27
33
|
|
|
28
|
-
|
|
34
|
+
For `wt cd`, save the wrapper where the shell reads it at start, so no process runs then (again after upgrading wt): `wt shell fish > ~/.config/fish/conf.d/wt.fish`, or `wt shell bash > ~/.local/share/wt/wt.bash` sourced from `~/.bashrc` (zsh likewise, after `compinit`).
|
|
29
35
|
|
|
30
36
|
## Manifests
|
|
31
37
|
|
|
32
|
-
One file per project in `~/.config/wt/projects/<name>.ts`, `wt init` writing a first draft:
|
|
38
|
+
One file per project in `~/.config/wt/projects/<name>.ts`, `wt init` writing a first draft, and a `tsconfig.json` beside it pointing `@kaynooo/cli-wt` at the installed wt so an editor type-checks the manifests (delete it to have the next `wt init` write it again after moving wt):
|
|
33
39
|
|
|
34
40
|
```ts
|
|
35
41
|
import type { ProjectConfig } from '@kaynooo/cli-wt'
|
|
@@ -51,17 +57,18 @@ export default {
|
|
|
51
57
|
| Key | |
|
|
52
58
|
| --- | --- |
|
|
53
59
|
| `root` | the main checkout |
|
|
54
|
-
| `repos` | nested repos put on the task branch;
|
|
60
|
+
| `repos` | nested repos put on the task branch; none when the root itself is the repo |
|
|
55
61
|
| `copy` | gitignored files and folders copied from main, hosts rewritten in the `.env*` ones |
|
|
56
62
|
| `clone` | data each worktree gets its own copy of: a path under root or a named volume's key |
|
|
57
63
|
| `link` | paths shared with main through a symlink |
|
|
58
64
|
| `skip` | services left out of a worktree's stack |
|
|
59
65
|
| `routeHosts` | services that dial the stack's own hosts from inside a container, given the worktree's in `/etc/hosts` |
|
|
60
66
|
| `env` | values forced into copied env files, per file; `{task}` and `{port}` (the dev server's) filled in |
|
|
67
|
+
| `ports` | a worktree's dev server port by task, from 1024 to 65535, for one whose port, hashed from its name, is taken: `wt add` warns when something listens on it, `wt sync` when another worktree has it |
|
|
61
68
|
| `certs` | where the stack's nginx reads certificates, required by passthrough routes |
|
|
62
69
|
| `setup` | `sh -c` commands run in the worktree the first time its stack comes up |
|
|
63
70
|
| `run` | `wt run`'s `service`, `dir`, `provision` commands, optional `dns` and `env` |
|
|
64
|
-
| `routes` | `host` main answers on, `to` `client` (the dev server on the host) or `<service>:<port>`, `passthrough` to leave TLS to the stack |
|
|
71
|
+
| `routes` | none leaves the worktree unrouted; `host` main answers on, `to` `client` (the dev server on the host) or `<service>:<port>`, `passthrough` to leave TLS to the stack |
|
|
65
72
|
|
|
66
73
|
## Config
|
|
67
74
|
|
|
@@ -83,7 +90,7 @@ export default {
|
|
|
83
90
|
| `state` | `~/.cache/wt` | setup markers, clone progress, queues |
|
|
84
91
|
| `jobs` | `<state>/jobs` | job records and per-stack locks; a tool pointing at the same folder shares the locks |
|
|
85
92
|
| `proxyNetwork` | `proxy` | the network devproxy reaches the stacks on |
|
|
86
|
-
| `runner` | | `up`, `down` and `removed` argv replacing `docker compose up -d --build`, `docker compose down` and nothing; `{project}`, `{task}`, `{dir}` and `{id}` filled in |
|
|
93
|
+
| `runner` | | `up`, `down` and `removed` argv replacing `docker compose up -d --build`, `docker compose down` and nothing; `{project}`, `{task}`, `{dir}` and `{id}` filled in; a worktree whose folder is gone skips `down`, `{dir}` having gone with it, for `docker compose -p <id> down` |
|
|
87
94
|
|
|
88
95
|
Without a runner, `wt up` brings devproxy up first when it is installed (on PATH or as this package's dependency), then compose, then the manifest's `setup` once. A runner command that itself runs wt, or another tool sharing the `jobs` folder, works inside the job that started it instead of being refused by its lock.
|
|
89
96
|
|
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
import {
|
|
3
|
+
holdSignals
|
|
4
|
+
} from "./cli-1nyesy1b.js";
|
|
5
|
+
import {
|
|
6
|
+
wtConfig,
|
|
7
|
+
WtError,
|
|
8
|
+
assertWorktreeExists,
|
|
9
|
+
currentDir,
|
|
10
|
+
locateWorktree
|
|
11
|
+
} from "./cli-kek450vq.js";
|
|
12
|
+
import {
|
|
13
|
+
queueDir,
|
|
14
|
+
holding,
|
|
15
|
+
inLine
|
|
16
|
+
} from "./cli-j82fak6j.js";
|
|
17
|
+
import {
|
|
18
|
+
composeConfig,
|
|
19
|
+
baseComposeFiles
|
|
20
|
+
} from "./cli-dmkfxdbr.js";
|
|
21
|
+
|
|
22
|
+
// src/run.ts
|
|
23
|
+
import { existsSync as existsSync2, mkdirSync, readdirSync, readFileSync as readFileSync2, rmSync, statSync, writeFileSync } from "fs";
|
|
24
|
+
import { userInfo } from "os";
|
|
25
|
+
import { join, resolve } from "path";
|
|
26
|
+
var {$ } = globalThis.Bun;
|
|
27
|
+
|
|
28
|
+
// src/env_file.ts
|
|
29
|
+
import { existsSync, readFileSync } from "fs";
|
|
30
|
+
|
|
31
|
+
// ../env/src/dotenv.ts
|
|
32
|
+
var ASSIGNMENT = /^\s*(?:export\s+)?([A-Z_][\w.-]*)\s*=(.*)$/i;
|
|
33
|
+
var INLINE_COMMENT = /\s+#.*$/;
|
|
34
|
+
var CARRIAGE_RETURN = /\r$/;
|
|
35
|
+
var ESCAPES = { n: `
|
|
36
|
+
`, r: "\r", t: "\t", '"': '"', "\\": "\\", $: "$" };
|
|
37
|
+
function closingQuote(text, quote) {
|
|
38
|
+
for (let index = 0;index < text.length; index++) {
|
|
39
|
+
if (quote === '"' && text[index] === "\\")
|
|
40
|
+
index++;
|
|
41
|
+
else if (text[index] === quote)
|
|
42
|
+
return index;
|
|
43
|
+
}
|
|
44
|
+
return -1;
|
|
45
|
+
}
|
|
46
|
+
function unescapeDollar(text) {
|
|
47
|
+
return text.replaceAll("\\$", "$");
|
|
48
|
+
}
|
|
49
|
+
function unescapeDouble(text) {
|
|
50
|
+
let out = "";
|
|
51
|
+
for (let index = 0;index < text.length; index++) {
|
|
52
|
+
const next = text[index + 1];
|
|
53
|
+
if (text[index] === "\\" && next !== undefined && next in ESCAPES) {
|
|
54
|
+
out += ESCAPES[next];
|
|
55
|
+
index++;
|
|
56
|
+
} else {
|
|
57
|
+
out += text[index];
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return out;
|
|
61
|
+
}
|
|
62
|
+
function parseDotenv(text) {
|
|
63
|
+
const physical = text.split(`
|
|
64
|
+
`);
|
|
65
|
+
const lines = [];
|
|
66
|
+
for (let index = 0;index < physical.length; index++) {
|
|
67
|
+
const line = physical[index] ?? "";
|
|
68
|
+
const match = ASSIGNMENT.exec(line.replace(CARRIAGE_RETURN, ""));
|
|
69
|
+
if (match === null) {
|
|
70
|
+
lines.push({ raw: line });
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
const [, key = "", rest = ""] = match;
|
|
74
|
+
const start = rest.trimStart();
|
|
75
|
+
const quote = start[0];
|
|
76
|
+
if (quote !== '"' && quote !== "'" && quote !== "`") {
|
|
77
|
+
lines.push({ raw: line, key, value: unescapeDollar(start.replace(INLINE_COMMENT, "").trim()) });
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
let body = start.slice(1);
|
|
81
|
+
let raw = line;
|
|
82
|
+
let end = closingQuote(body, quote);
|
|
83
|
+
const first = index;
|
|
84
|
+
while (end === -1 && index + 1 < physical.length) {
|
|
85
|
+
index++;
|
|
86
|
+
body += `
|
|
87
|
+
${(physical[index] ?? "").replace(CARRIAGE_RETURN, "")}`;
|
|
88
|
+
raw += `
|
|
89
|
+
${physical[index] ?? ""}`;
|
|
90
|
+
end = closingQuote(body, quote);
|
|
91
|
+
}
|
|
92
|
+
if (end === -1) {
|
|
93
|
+
index = first;
|
|
94
|
+
lines.push({ raw: line, key, value: unescapeDollar(start.replace(INLINE_COMMENT, "").trim()), unterminated: true });
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
const inner = body.slice(0, end);
|
|
98
|
+
lines.push({ raw, key, value: quote === '"' ? unescapeDouble(inner) : unescapeDollar(inner) });
|
|
99
|
+
}
|
|
100
|
+
return { lines };
|
|
101
|
+
}
|
|
102
|
+
function entries(file) {
|
|
103
|
+
const map = new Map;
|
|
104
|
+
for (const line of file.lines) {
|
|
105
|
+
if (line.key !== undefined)
|
|
106
|
+
map.set(line.key, line.value ?? "");
|
|
107
|
+
}
|
|
108
|
+
return map;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// src/env_file.ts
|
|
112
|
+
function readEnvFile(path) {
|
|
113
|
+
return existsSync(path) ? Object.fromEntries(entries(parseDotenv(readFileSync(path, "utf8")))) : {};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// src/run.ts
|
|
117
|
+
var POSTGRES = /^postgres(?:ql)?:$/;
|
|
118
|
+
var DEFAULT_PORT = 5432;
|
|
119
|
+
var READY_TRIES = 60;
|
|
120
|
+
var testContainer = (worktree) => `wt-${worktree.project.name}-test-db`;
|
|
121
|
+
function testDatabase(dir) {
|
|
122
|
+
const base = existsSync2(join(dir, ".env")) ? ".env" : ".env.dist";
|
|
123
|
+
const env = Object.assign({}, ...[base, ".env.test", ".env.test.local"].map((file) => readEnvFile(join(dir, file))));
|
|
124
|
+
if (env.DATABASE_URL === undefined || env.DATABASE_URL === "")
|
|
125
|
+
throw new WtError(`no DATABASE_URL in the test env files of ${dir}`);
|
|
126
|
+
const url = new URL(env.DATABASE_URL);
|
|
127
|
+
if (!POSTGRES.test(url.protocol))
|
|
128
|
+
throw new WtError(`wt run only starts Postgres, and ${dir} tests against ${url.protocol.slice(0, -1)}`);
|
|
129
|
+
return { host: url.hostname, port: Number(url.port || DEFAULT_PORT), user: decodeURIComponent(url.username), password: decodeURIComponent(url.password) };
|
|
130
|
+
}
|
|
131
|
+
function testToken(task) {
|
|
132
|
+
return task.replaceAll("-", "_");
|
|
133
|
+
}
|
|
134
|
+
function databaseImage(config, host) {
|
|
135
|
+
const service = config.services[host] ?? Object.values(config.services).find((each) => Object.values(each.networks ?? {}).some((network) => network?.aliases?.includes(host)));
|
|
136
|
+
if (service?.image === undefined || service.image === "")
|
|
137
|
+
throw new WtError(`no service of ${config.name} answers to ${host}, the test database host, with an image to start`);
|
|
138
|
+
return service.image;
|
|
139
|
+
}
|
|
140
|
+
function runInputs(root, run, config, host) {
|
|
141
|
+
const service = config.services[run.service];
|
|
142
|
+
if (!service)
|
|
143
|
+
throw new WtError(`${config.name} has no service ${run.service}`);
|
|
144
|
+
const source = join(root, run.dir);
|
|
145
|
+
const target = service.volumes?.find((volume) => volume.type === "bind" && volume.source === source)?.target;
|
|
146
|
+
if (target === undefined)
|
|
147
|
+
throw new WtError(`${run.service} does not bind-mount ${source}, so wt cannot tell where the worktree's copy goes`);
|
|
148
|
+
return { target, image: service.image ?? `${config.name}-${run.service}`, databaseImage: databaseImage(config, host) };
|
|
149
|
+
}
|
|
150
|
+
function inputsKey(root, overrides, run, host) {
|
|
151
|
+
const files = [".env", ...baseComposeFiles(root, overrides)].map((file) => resolve(root, file));
|
|
152
|
+
return JSON.stringify([run.service, run.dir, host, ...files.map((file) => {
|
|
153
|
+
const stat = statSync(file, { throwIfNoEntry: false });
|
|
154
|
+
return [file, stat?.mtimeMs ?? null, stat?.size ?? null];
|
|
155
|
+
})]);
|
|
156
|
+
}
|
|
157
|
+
async function cachedRunInputs(worktree, run, host) {
|
|
158
|
+
const { root, name, settings } = worktree.project;
|
|
159
|
+
const file = join(settings.state, `${name}.run.json`);
|
|
160
|
+
const key = inputsKey(root, settings.overrides, run, host);
|
|
161
|
+
try {
|
|
162
|
+
const cached = JSON.parse(readFileSync2(file, "utf8"));
|
|
163
|
+
if (cached.key === key)
|
|
164
|
+
return cached.inputs;
|
|
165
|
+
} catch {}
|
|
166
|
+
const inputs = runInputs(root, run, await composeConfig(root), host);
|
|
167
|
+
mkdirSync(settings.state, { recursive: true });
|
|
168
|
+
writeFileSync(file, JSON.stringify({ key, inputs }));
|
|
169
|
+
return inputs;
|
|
170
|
+
}
|
|
171
|
+
function runPlan(worktree, run, inputs, database, ids = userInfo(), tty = false) {
|
|
172
|
+
const { target } = inputs;
|
|
173
|
+
const network = `wt-${worktree.project.name}-test`;
|
|
174
|
+
const container = testContainer(worktree);
|
|
175
|
+
return {
|
|
176
|
+
network,
|
|
177
|
+
container,
|
|
178
|
+
database: [
|
|
179
|
+
"run",
|
|
180
|
+
"-d",
|
|
181
|
+
"--name",
|
|
182
|
+
container,
|
|
183
|
+
"--network",
|
|
184
|
+
network,
|
|
185
|
+
"--network-alias",
|
|
186
|
+
database.host,
|
|
187
|
+
"--tmpfs",
|
|
188
|
+
"/var/lib/postgresql/data",
|
|
189
|
+
"-e",
|
|
190
|
+
`POSTGRES_USER=${database.user}`,
|
|
191
|
+
"-e",
|
|
192
|
+
"POSTGRES_PASSWORD",
|
|
193
|
+
"-e",
|
|
194
|
+
`PGPORT=${database.port}`,
|
|
195
|
+
inputs.databaseImage
|
|
196
|
+
],
|
|
197
|
+
runner: (command) => [
|
|
198
|
+
"run",
|
|
199
|
+
"--rm",
|
|
200
|
+
"-i",
|
|
201
|
+
...tty ? ["-t"] : [],
|
|
202
|
+
"--pull",
|
|
203
|
+
"never",
|
|
204
|
+
"--network",
|
|
205
|
+
network,
|
|
206
|
+
...run.dns !== undefined ? ["--dns", run.dns] : [],
|
|
207
|
+
"-u",
|
|
208
|
+
`${ids.uid}:${ids.gid}`,
|
|
209
|
+
"-e",
|
|
210
|
+
"HOME=/tmp",
|
|
211
|
+
...Object.entries(run.env ?? {}).flatMap(([key, value]) => ["-e", `${key}=${value}`]),
|
|
212
|
+
"-e",
|
|
213
|
+
`TEST_TOKEN=${testToken(worktree.task)}`,
|
|
214
|
+
"-v",
|
|
215
|
+
`${join(worktree.dir, run.dir)}:${target}`,
|
|
216
|
+
"-w",
|
|
217
|
+
target,
|
|
218
|
+
inputs.image,
|
|
219
|
+
...command
|
|
220
|
+
]
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
async function runningId(container) {
|
|
224
|
+
const result = await $`docker inspect --format ${"{{if .State.Running}}{{.Id}}{{end}}"} ${container}`.quiet().nothrow();
|
|
225
|
+
return result.text().trim() || undefined;
|
|
226
|
+
}
|
|
227
|
+
async function answeringDatabase(plan, database) {
|
|
228
|
+
const result = await $`docker exec ${plan.container} sh -c ${'pg_isready -q -h 127.0.0.1 -U "$1" && hostname'} sh ${database.user}`.quiet().nothrow();
|
|
229
|
+
return result.exitCode === 0 ? result.text().trim() || undefined : undefined;
|
|
230
|
+
}
|
|
231
|
+
async function ensureDatabase(plan, database) {
|
|
232
|
+
if ((await $`docker network inspect ${plan.network}`.quiet().nothrow()).exitCode !== 0)
|
|
233
|
+
await $`docker network create ${plan.network}`.quiet().nothrow();
|
|
234
|
+
let id = await runningId(plan.container);
|
|
235
|
+
if (id === undefined) {
|
|
236
|
+
await $`docker rm -f ${plan.container}`.quiet().nothrow();
|
|
237
|
+
const started = await $`docker ${plan.database}`.env({ ...process.env, POSTGRES_PASSWORD: database.password }).quiet().nothrow();
|
|
238
|
+
id = await runningId(plan.container);
|
|
239
|
+
if (id === undefined)
|
|
240
|
+
throw new WtError(`${plan.container} did not start:
|
|
241
|
+
${started.stderr.toString().trim()}`);
|
|
242
|
+
}
|
|
243
|
+
for (let tries = 0;tries < READY_TRIES; tries++) {
|
|
244
|
+
if ((await $`docker exec ${plan.container} pg_isready -q -h 127.0.0.1 -U ${database.user}`.quiet().nothrow()).exitCode === 0)
|
|
245
|
+
return id;
|
|
246
|
+
await Bun.sleep(1000);
|
|
247
|
+
}
|
|
248
|
+
throw new WtError(`${plan.container} is not accepting connections after ${READY_TRIES} s: docker logs ${plan.container}`);
|
|
249
|
+
}
|
|
250
|
+
async function runInherit(command) {
|
|
251
|
+
const held = holdSignals();
|
|
252
|
+
const child = Bun.spawn(command, { stdio: ["inherit", "inherit", "inherit"] });
|
|
253
|
+
held.forward(child);
|
|
254
|
+
try {
|
|
255
|
+
return await child.exited;
|
|
256
|
+
} finally {
|
|
257
|
+
held.release();
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
async function forgetTestDatabases2(worktree) {
|
|
261
|
+
const container = testContainer(worktree);
|
|
262
|
+
if (await runningId(container) !== undefined) {
|
|
263
|
+
const listed = await $`docker exec ${container} sh -c ${'psql -U "$POSTGRES_USER" -d postgres -Atc "select datname from pg_database"'}`.quiet().nothrow();
|
|
264
|
+
const ours = listed.text().split(`
|
|
265
|
+
`).filter((name) => name.endsWith(`_test${testToken(worktree.task)}`));
|
|
266
|
+
for (const name of ours)
|
|
267
|
+
await $`docker exec ${container} sh -c ${'dropdb -U "$POSTGRES_USER" --force "$0"'} ${name}`.quiet().nothrow();
|
|
268
|
+
}
|
|
269
|
+
const markers = worktree.project.settings.state;
|
|
270
|
+
if (!existsSync2(markers))
|
|
271
|
+
return;
|
|
272
|
+
for (const file of readdirSync(markers).filter((name) => name.startsWith(`${worktree.id}.test-`)))
|
|
273
|
+
rmSync(join(markers, file), { force: true });
|
|
274
|
+
}
|
|
275
|
+
async function provision(worktree, run, plan, databaseId) {
|
|
276
|
+
const markers = worktree.project.settings.state;
|
|
277
|
+
const marker = join(markers, `${worktree.id}.test-${databaseId.slice(0, 12)}`);
|
|
278
|
+
if (existsSync2(marker))
|
|
279
|
+
return;
|
|
280
|
+
await inLine(queueDir(worktree.project.settings, `${worktree.id}.provision`), "provisioning", () => console.error(`wt run: waiting for ${worktree.task}'s test database to be provisioned`), async () => {
|
|
281
|
+
if (existsSync2(marker))
|
|
282
|
+
return;
|
|
283
|
+
console.error(`wt run: provisioning ${worktree.task}'s test database`);
|
|
284
|
+
for (const step of run.provision) {
|
|
285
|
+
if (await runInherit(["docker", ...plan.runner(["sh", "-c", step])]) !== 0)
|
|
286
|
+
throw new WtError(`provisioning failed on \`${step}\``, `run it again with \`wt run ${worktree.task} -- sh -c '${step}'\``);
|
|
287
|
+
}
|
|
288
|
+
mkdirSync(markers, { recursive: true });
|
|
289
|
+
writeFileSync(marker, "");
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
async function runTask2(task, command, project) {
|
|
293
|
+
const worktree = await locateWorktree(currentDir(), task, project);
|
|
294
|
+
const { run, name } = worktree.project;
|
|
295
|
+
assertWorktreeExists(worktree);
|
|
296
|
+
if (!run)
|
|
297
|
+
throw new WtError(`${name} has no \`run\` in its manifest, so wt cannot tell which service runs its tests`);
|
|
298
|
+
return holding(runsDir2(worktree), command.join(" "), async () => {
|
|
299
|
+
const database = testDatabase(join(worktree.dir, run.dir));
|
|
300
|
+
const plan = runPlan(worktree, run, await cachedRunInputs(worktree, run, database.host), database, userInfo(), Boolean(process.stdin.isTTY && process.stdout.isTTY));
|
|
301
|
+
await provision(worktree, run, plan, await answeringDatabase(plan, database) ?? await ensureDatabase(plan, database));
|
|
302
|
+
return runInherit(["docker", ...plan.runner(command)]);
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
function runsDir2(worktree) {
|
|
306
|
+
return queueDir(worktree.project.settings, `${worktree.id}.runs`);
|
|
307
|
+
}
|
|
308
|
+
async function runHeavy2(command) {
|
|
309
|
+
const label = `${command.join(" ")} (in ${currentDir()})`;
|
|
310
|
+
return inLine(queueDir(await wtConfig(), "heavy"), label, (ahead, running) => console.error(`wt heavy: ${ahead} ahead, running ${running}`), async () => runInherit(command));
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
export { forgetTestDatabases2, runTask2, runsDir2, runHeavy2 };
|