@kaynooo/cli-wt 0.0.0-stage → 0.1.1

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/src/run.ts ADDED
@@ -0,0 +1,251 @@
1
+ import type { ComposeConfig } from './compose'
2
+ import type { RunConfig, Worktree } from './project'
3
+ import { existsSync, mkdirSync, readdirSync, rmSync, writeFileSync } from 'node:fs'
4
+ import { userInfo } from 'node:os'
5
+ import { join } from 'node:path'
6
+ import { $ } from 'bun'
7
+ import { composeConfig } from './compose'
8
+ import { wtConfig } from './config'
9
+ import { readEnvFile } from './env_file'
10
+ import { assertWorktreeExists, locateWorktree, WtError } from './project'
11
+ import { inLine, queueDir } from './queue'
12
+
13
+ export interface TestDatabase {
14
+ // The host the test env dials, which the throwaway database answers to on the private network.
15
+ host: string
16
+ port: number
17
+ user: string
18
+ password: string
19
+ }
20
+
21
+ export interface RunPlan {
22
+ network: string
23
+ container: string
24
+ // `docker` arguments starting the database; its password comes from the environment, never the command line.
25
+ database: string[]
26
+ // `docker` arguments running a command of the worktree.
27
+ runner: (command: string[]) => string[]
28
+ }
29
+
30
+ const POSTGRES = /^postgres(?:ql)?:$/
31
+ const DEFAULT_PORT = 5432
32
+ const READY_TRIES = 60
33
+
34
+ const testContainer = (worktree: Worktree) => `wt-${worktree.project.name}-test-db`
35
+
36
+ // Symfony's test env reads .env (.env.dist without one), .env.test and .env.test.local, never .env.local.
37
+ export function testDatabase(dir: string): TestDatabase {
38
+ const base = existsSync(join(dir, '.env')) ? '.env' : '.env.dist'
39
+ const env: Record<string, string> = Object.assign({}, ...[base, '.env.test', '.env.test.local'].map(file => readEnvFile(join(dir, file))))
40
+
41
+ if (env.DATABASE_URL === undefined || env.DATABASE_URL === '')
42
+ throw new WtError(`no DATABASE_URL in the test env files of ${dir}`)
43
+
44
+ const url = new URL(env.DATABASE_URL)
45
+
46
+ if (!POSTGRES.test(url.protocol))
47
+ throw new WtError(`wt run only starts Postgres, and ${dir} tests against ${url.protocol.slice(0, -1)}`)
48
+
49
+ return { host: url.hostname, port: Number(url.port || DEFAULT_PORT), user: decodeURIComponent(url.username), password: decodeURIComponent(url.password) }
50
+ }
51
+
52
+ // Doctrine's dbname_suffix (`_test%env(default::TEST_TOKEN)%`) gives each worktree a database of its own on the shared server.
53
+ export function testToken(task: string): string {
54
+ return task.replaceAll('-', '_')
55
+ }
56
+
57
+ function databaseImage(config: ComposeConfig, host: string): string {
58
+ const service = config.services[host] ?? Object.values(config.services).find(each => Object.values(each.networks ?? {}).some(network => network?.aliases?.includes(host)))
59
+
60
+ if (service?.image === undefined || service.image === '')
61
+ throw new WtError(`no service of ${config.name} answers to ${host}, the test database host, with an image to start`)
62
+
63
+ return service.image
64
+ }
65
+
66
+ // The worktree's `dir` goes where the service mounts main's, so the image finds it where it expects; the database is one container per project, on a network of its own, so neither touches the user's stack.
67
+ export function runPlan(worktree: Worktree, run: RunConfig, config: ComposeConfig, database: TestDatabase, ids: { uid: number, gid: number } = userInfo(), tty = false): RunPlan {
68
+ const service = config.services[run.service]
69
+
70
+ if (!service)
71
+ throw new WtError(`${config.name} has no service ${run.service}`)
72
+
73
+ const source = join(worktree.project.root, run.dir)
74
+ const target = service.volumes?.find(volume => volume.type === 'bind' && volume.source === source)?.target
75
+
76
+ if (target === undefined)
77
+ throw new WtError(`${run.service} does not bind-mount ${source}, so wt cannot tell where the worktree's copy goes`)
78
+
79
+ const network = `wt-${worktree.project.name}-test`
80
+ const container = testContainer(worktree)
81
+
82
+ return {
83
+ network,
84
+ container,
85
+ database: [
86
+ 'run',
87
+ '-d',
88
+ '--name',
89
+ container,
90
+ '--network',
91
+ network,
92
+ '--network-alias',
93
+ database.host,
94
+ '--tmpfs',
95
+ '/var/lib/postgresql/data',
96
+ '-e',
97
+ `POSTGRES_USER=${database.user}`,
98
+ '-e',
99
+ 'POSTGRES_PASSWORD',
100
+ '-e',
101
+ `PGPORT=${database.port}`,
102
+ databaseImage(config, database.host),
103
+ ],
104
+ runner: command => [
105
+ 'run',
106
+ '--rm',
107
+ '-i',
108
+ ...tty ? ['-t'] : [],
109
+ '--pull',
110
+ 'never',
111
+ '--network',
112
+ network,
113
+ ...run.dns !== undefined ? ['--dns', run.dns] : [],
114
+ '-u',
115
+ `${ids.uid}:${ids.gid}`,
116
+ '-e',
117
+ 'HOME=/tmp',
118
+ ...Object.entries(run.env ?? {}).flatMap(([key, value]) => ['-e', `${key}=${value}`]),
119
+ '-e',
120
+ `TEST_TOKEN=${testToken(worktree.task)}`,
121
+ '-v',
122
+ `${join(worktree.dir, run.dir)}:${target}`,
123
+ '-w',
124
+ target,
125
+ service.image ?? `${config.name}-${run.service}`,
126
+ ...command,
127
+ ],
128
+ }
129
+ }
130
+
131
+ async function runningId(container: string): Promise<string | undefined> {
132
+ const result = await $`docker inspect --format ${'{{if .State.Running}}{{.Id}}{{end}}'} ${container}`.quiet().nothrow()
133
+
134
+ return result.text().trim() || undefined
135
+ }
136
+
137
+ // Started once and shared by the project's worktrees until it is removed; its data lives in memory, so a restart means fresh databases.
138
+ async function ensureDatabase(plan: RunPlan, database: TestDatabase): Promise<string> {
139
+ if ((await $`docker network inspect ${plan.network}`.quiet().nothrow()).exitCode !== 0)
140
+ await $`docker network create ${plan.network}`.quiet().nothrow()
141
+
142
+ let id = await runningId(plan.container)
143
+
144
+ if (id === undefined) {
145
+ await $`docker rm -f ${plan.container}`.quiet().nothrow()
146
+
147
+ const started = await $`docker ${plan.database}`.env({ ...process.env, POSTGRES_PASSWORD: database.password }).quiet().nothrow()
148
+
149
+ // Another `wt run` may have started it first, which leaves this one's name taken and the container running.
150
+ id = await runningId(plan.container)
151
+
152
+ if (id === undefined)
153
+ throw new WtError(`${plan.container} did not start:\n${started.stderr.toString().trim()}`)
154
+ }
155
+
156
+ // Through TCP, since the image's first boot answers on its socket while it is still initialising.
157
+ for (let tries = 0; tries < READY_TRIES; tries++) {
158
+ if ((await $`docker exec ${plan.container} pg_isready -q -h 127.0.0.1 -U ${database.user}`.quiet().nothrow()).exitCode === 0)
159
+ return id
160
+
161
+ await Bun.sleep(1000)
162
+ }
163
+
164
+ throw new WtError(`${plan.container} is not accepting connections after ${READY_TRIES} s: docker logs ${plan.container}`)
165
+ }
166
+
167
+ // Forwards an interrupt to the child, a `docker run` passing it on to the container, and resolves with its exit code.
168
+ export async function runInherit(command: string[]): Promise<number> {
169
+ const child = Bun.spawn(command, { stdio: ['inherit', 'inherit', 'inherit'] })
170
+ const forward = (signal: NodeJS.Signals) => child.kill(signal)
171
+
172
+ process.on('SIGINT', forward)
173
+ process.on('SIGTERM', forward)
174
+
175
+ try {
176
+ return await child.exited
177
+ }
178
+ finally {
179
+ process.off('SIGINT', forward)
180
+ process.off('SIGTERM', forward)
181
+ }
182
+ }
183
+
184
+ // What `wt rm` leaves behind: its databases on the project's shared test server, whose name ends in its TEST_TOKEN, and the markers saying they were provisioned.
185
+ export async function forgetTestDatabases(worktree: Worktree): Promise<void> {
186
+ const container = testContainer(worktree)
187
+
188
+ if (await runningId(container) !== undefined) {
189
+ const listed = await $`docker exec ${container} sh -c ${'psql -U "$POSTGRES_USER" -d postgres -Atc "select datname from pg_database"'}`.quiet().nothrow()
190
+ const ours = listed.text().split('\n').filter(name => name.endsWith(`_test${testToken(worktree.task)}`))
191
+
192
+ for (const name of ours)
193
+ await $`docker exec ${container} sh -c ${'dropdb -U "$POSTGRES_USER" --force "$0"'} ${name}`.quiet().nothrow()
194
+ }
195
+
196
+ const markers = worktree.project.settings.state
197
+
198
+ if (!existsSync(markers))
199
+ return
200
+
201
+ for (const file of readdirSync(markers).filter(name => name.startsWith(`${worktree.id}.test-`)))
202
+ rmSync(join(markers, file), { force: true })
203
+ }
204
+
205
+ // Once per worktree and database container, in line behind any other `wt run` of the same worktree doing it too.
206
+ async function provision(worktree: Worktree, run: RunConfig, plan: RunPlan, databaseId: string): Promise<void> {
207
+ const markers = worktree.project.settings.state
208
+ const marker = join(markers, `${worktree.id}.test-${databaseId.slice(0, 12)}`)
209
+
210
+ if (existsSync(marker))
211
+ return
212
+
213
+ 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 () => {
214
+ if (existsSync(marker))
215
+ return
216
+
217
+ console.error(`wt run: provisioning ${worktree.task}'s test database`)
218
+
219
+ for (const step of run.provision) {
220
+ if (await runInherit(['docker', ...plan.runner(['sh', '-c', step])]) !== 0)
221
+ throw new WtError(`provisioning failed on \`${step}\`, run it again with \`wt run ${worktree.task} -- sh -c '${step}'\``)
222
+ }
223
+
224
+ mkdirSync(markers, { recursive: true })
225
+ writeFileSync(marker, '')
226
+ })
227
+ }
228
+
229
+ export async function runTask(task: string, command: string[], project?: string): Promise<number> {
230
+ const worktree = await locateWorktree(process.cwd(), task, project)
231
+ const { run, name, root } = worktree.project
232
+
233
+ assertWorktreeExists(worktree)
234
+
235
+ if (!run)
236
+ throw new WtError(`${name} has no \`run\` in its manifest, so wt cannot tell which service runs its tests`)
237
+
238
+ const database = testDatabase(join(worktree.dir, run.dir))
239
+ const plan = runPlan(worktree, run, await composeConfig(root), database, userInfo(), Boolean(process.stdin.isTTY && process.stdout.isTTY))
240
+
241
+ await provision(worktree, run, plan, await ensureDatabase(plan, database))
242
+
243
+ return runInherit(['docker', ...plan.runner(command)])
244
+ }
245
+
246
+ // One heavy command at a time across every session on the machine, the rest waiting their turn in arrival order.
247
+ export async function runHeavy(command: string[]): Promise<number> {
248
+ const label = `${command.join(' ')} (in ${process.cwd()})`
249
+
250
+ return inLine(queueDir(await wtConfig(), 'heavy'), label, (ahead, running) => console.error(`wt heavy: ${ahead} ahead, running ${running}`), async () => runInherit(command))
251
+ }
package/src/shell.ts ADDED
@@ -0,0 +1,28 @@
1
+ import { KnownError } from './errors'
2
+
3
+ // What a child process cannot do for its shell: `wt cd` changes directory, and `wt rm` leaves the folder it just removed for main.
4
+ const FISH = `function wt --description 'git worktrees of a compose project, each with its own stack'
5
+ switch "$argv[1]"
6
+ case cd
7
+ set -l dir (command wt path $argv[2..-1]); or return
8
+ builtin cd $dir
9
+ case rm
10
+ set -l main (command wt path 2>/dev/null)
11
+ command wt $argv
12
+ set -l code $status
13
+ test -d $PWD; or builtin cd $main
14
+ return $code
15
+ case '*'
16
+ command wt $argv
17
+ end
18
+ end
19
+ command wt --completions fish | source
20
+ complete -c wt -n __fish_use_subcommand -a cd -d 'go to a worktree, or main'
21
+ complete -c wt -n '__fish_seen_subcommand_from cd' -f -a '(command wt tasks) main'`
22
+
23
+ export function shellWrapper(shell: string | undefined): string {
24
+ if (shell !== 'fish')
25
+ throw new KnownError(`no wrapper for ${shell ?? 'that shell'} yet, only fish`)
26
+
27
+ return FISH
28
+ }
package/src/stack.ts ADDED
@@ -0,0 +1,105 @@
1
+ /** Brings a worktree's stack up and down: the config's `runner` commands when it has them, else compose in the worktree, the manifest's `setup` run once after the first up. */
2
+
3
+ import type { Worktree } from './project'
4
+ import { existsSync, mkdirSync, writeFileSync } from 'node:fs'
5
+ import { homedir } from 'node:os'
6
+ import { dirname } from 'node:path'
7
+ import { $ } from 'bun'
8
+ import { composeEnv } from './compose'
9
+ import { configPath, fillTemplate } from './config'
10
+ import { WtError } from './project'
11
+ import { devproxyUp, hasDevproxy } from './proxy'
12
+
13
+ const LEADING_ROOT = /^[/~]+/
14
+ const UNSAFE_IN_FILE_NAME = /[^\w.-]+/g
15
+
16
+ /** The name a stack's job files go by: `booki/planning` gives `booki--planning`, as athome names them. */
17
+ export function slugOf(ref: string): string {
18
+ return ref.replace(LEADING_ROOT, '').replace(UNSAFE_IN_FILE_NAME, '--')
19
+ }
20
+
21
+ // The env is always passed: a spawn given none gets the env the process started with, missing the WT_JOBS_HELD a job sets since.
22
+ async function spawnInherit(argv: string[], cwd: string, env: Record<string, string | undefined> = process.env): Promise<number> {
23
+ return Bun.spawn(argv, { cwd, env, stdio: ['inherit', 'inherit', 'inherit'] }).exited
24
+ }
25
+
26
+ /** Runs a `runner` template, failing with the command named. */
27
+ async function runTemplate(template: string[], worktree: Worktree): Promise<void> {
28
+ const argv = fillTemplate(template, { project: worktree.project.name, task: worktree.task, dir: worktree.dir, id: worktree.id })
29
+
30
+ if (await spawnInherit(argv, worktree.dir) !== 0)
31
+ throw new WtError(`\`${argv.join(' ')}\` failed`)
32
+ }
33
+
34
+ /** The manifest's setup commands, run with sh in the worktree the first time its stack comes up; a failing one leaves them all to run again next time. */
35
+ export async function runSetup(worktree: Worktree, env: Record<string, string | undefined> = composeEnv()): Promise<void> {
36
+ const setup = worktree.project.setup ?? []
37
+ const marker = worktree.files.setupMarker
38
+
39
+ if (!setup.length || existsSync(marker))
40
+ return
41
+
42
+ for (const command of setup) {
43
+ if (await spawnInherit(['sh', '-c', command], worktree.dir, env) !== 0)
44
+ throw new WtError(`the worktree setup failed on \`${command}\``)
45
+ }
46
+
47
+ mkdirSync(dirname(marker), { recursive: true })
48
+ writeFileSync(marker, '')
49
+ }
50
+
51
+ export async function stackUp(worktree: Worktree): Promise<void> {
52
+ const { up } = worktree.project.settings.runner
53
+
54
+ if (up)
55
+ return runTemplate(up, worktree)
56
+
57
+ // The override puts routed services on the proxy's network, which must exist before compose starts them
58
+ if (await hasDevproxy())
59
+ await devproxyUp()
60
+
61
+ const network = worktree.project.settings.proxyNetwork
62
+
63
+ if (worktree.project.routes.length > 0 && (await $`docker network inspect ${network}`.quiet().nothrow()).exitCode !== 0)
64
+ throw new WtError(`the routed services join the ${network} network, which does not exist: install and configure devproxy, or set proxyNetwork in ${configPath()} to the network your proxy dials stacks on`)
65
+
66
+ if (await spawnInherit(['docker', 'compose', 'up', '-d', '--build'], worktree.dir, composeEnv()) !== 0)
67
+ throw new WtError(`docker compose up failed in ${worktree.dir}`)
68
+
69
+ await runSetup(worktree)
70
+ }
71
+
72
+ /** Stops the stack; with the worktree's folder gone, compose finds its containers by their project label. */
73
+ export async function stackDown(worktree: Worktree): Promise<void> {
74
+ const { down } = worktree.project.settings.runner
75
+
76
+ if (down && existsSync(worktree.dir))
77
+ return runTemplate(down, worktree)
78
+
79
+ const here = existsSync(worktree.dir)
80
+ // Away from the project, whose compose file compose would otherwise read under the worktree's name
81
+ const argv = here ? ['docker', 'compose', 'down', '--remove-orphans'] : ['docker', 'compose', '-p', worktree.id, 'down', '--remove-orphans']
82
+
83
+ if (await spawnInherit(argv, here ? worktree.dir : homedir(), composeEnv()) !== 0)
84
+ throw new WtError(`docker compose down failed for ${worktree.id}`)
85
+ }
86
+
87
+ /** Runs the config's `removed` command once a worktree is gone, from the project's root. */
88
+ export async function afterRemove(worktree: Worktree): Promise<void> {
89
+ const { removed } = worktree.project.settings.runner
90
+
91
+ if (!removed)
92
+ return
93
+
94
+ const argv = fillTemplate(removed, { project: worktree.project.name, task: worktree.task, dir: worktree.dir, id: worktree.id })
95
+
96
+ if (await spawnInherit(argv, worktree.project.root) !== 0)
97
+ console.warn(`wt: \`${argv.join(' ')}\` failed after removing ${worktree.id}`)
98
+ }
99
+
100
+ /** Whether some container, running or not, belongs to that compose project. */
101
+ export async function hasContainers(composeName: string): Promise<boolean> {
102
+ const found = await $`docker ps -aq --filter ${`label=com.docker.compose.project=${composeName}`}`.quiet().nothrow()
103
+
104
+ return found.stdout.toString().trim() !== ''
105
+ }
package/src/sync.ts ADDED
@@ -0,0 +1,28 @@
1
+ import type { Worktree } from './project'
2
+ import { baseComposeFiles, composeConfig, overrideWarnings, renderOverride } from './compose'
3
+ import { cloneData, copyFromMain, linkFromMain, writeEnvFiles, writeIfChanged } from './files'
4
+ import { assertWorktreeExists } from './project'
5
+ import { writeStackCerts } from './routes'
6
+
7
+ // Everything a worktree needs besides its git checkouts. Safe to run again: copies and clones never overwrite, generated files are rewritten.
8
+ export async function syncWorktree(worktree: Worktree): Promise<void> {
9
+ assertWorktreeExists(worktree)
10
+
11
+ const [warnings, main] = await Promise.all([copyFromMain(worktree), composeConfig(worktree.project.root)])
12
+
13
+ linkFromMain(worktree)
14
+ writeEnvFiles(worktree)
15
+ warnings.push(...await cloneData(worktree, main))
16
+
17
+ const config = await composeConfig(worktree.dir, { COMPOSE_FILE: baseComposeFiles(worktree.dir, worktree.project.settings.overrides).join(':'), COMPOSE_PROJECT_NAME: worktree.id })
18
+
19
+ writeIfChanged(worktree.files.override, renderOverride(worktree, config))
20
+ warnings.push(...overrideWarnings(worktree, config))
21
+
22
+ // Read back through the worktree's own .env, so a merge compose rejects fails here rather than on `wt up`.
23
+ await composeConfig(worktree.dir)
24
+ await writeStackCerts(worktree)
25
+
26
+ for (const warning of warnings)
27
+ console.warn(`wt: ${warning}`)
28
+ }