@kaynooo/cli-wt 0.0.0-stage → 0.1.0

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/compose.ts ADDED
@@ -0,0 +1,212 @@
1
+ import type { Worktree } from './project'
2
+ import { existsSync, readFileSync } from 'node:fs'
3
+ import { join } from 'node:path'
4
+ import { $ } from 'bun'
5
+ import { worktreeHost, WtError } from './project'
6
+
7
+ interface ServiceNetwork {
8
+ aliases?: string[]
9
+ }
10
+
11
+ interface ComposeService {
12
+ container_name?: string
13
+ image?: string
14
+ labels?: Record<string, string>
15
+ network_mode?: string
16
+ networks?: Record<string, ServiceNetwork | null>
17
+ ports?: unknown[]
18
+ volumes?: { type: string, source?: string, target?: string }[]
19
+ }
20
+
21
+ interface ComposeVolume {
22
+ name?: string
23
+ external?: boolean
24
+ driver_opts?: Record<string, string>
25
+ }
26
+
27
+ export interface ComposeConfig {
28
+ name: string
29
+ services: Record<string, ComposeService>
30
+ networks?: Record<string, unknown>
31
+ volumes?: Record<string, ComposeVolume>
32
+ }
33
+
34
+ /** The file names compose looks for in a directory, in its order. */
35
+ export const COMPOSE_FILES = ['compose.yaml', 'compose.yml', 'docker-compose.yaml', 'docker-compose.yml']
36
+ const SKIP_PROFILE = 'wt-skip'
37
+
38
+ // Compose settings wt decides on, so a COMPOSE_FILE exported in the calling shell cannot leak in.
39
+ export function composeEnv(overrides: Record<string, string> = {}): Record<string, string | undefined> {
40
+ const env = Object.entries(process.env).filter(([key]) => key !== 'COMPOSE_FILE' && key !== 'COMPOSE_PROJECT_NAME')
41
+
42
+ return { ...Object.fromEntries(env), ...overrides }
43
+ }
44
+
45
+ export async function composeConfig(dir: string, env: Record<string, string> = {}): Promise<ComposeConfig> {
46
+ const result = await $`docker compose config --format json`.cwd(dir).env(composeEnv(env)).quiet().nothrow()
47
+
48
+ if (result.exitCode !== 0)
49
+ throw new WtError(`docker compose config failed in ${dir}:\n${result.stderr.toString()}`)
50
+
51
+ return JSON.parse(result.stdout.toString()) as ComposeConfig
52
+ }
53
+
54
+ const COMPOSE_FILE_LINE = /^COMPOSE_FILE=(.*)$/m
55
+ const QUOTES_AROUND = /^["']|["']$/g
56
+
57
+ // COMPOSE_FILE from the worktree's .env without wt's own override (any file under `overrides`), falling back to the compose file found in dir.
58
+ export function baseComposeFiles(dir: string, overrides: string): string[] {
59
+ const envFile = join(dir, '.env')
60
+ const line = existsSync(envFile) ? readFileSync(envFile, 'utf8').match(COMPOSE_FILE_LINE) : null
61
+ const files = line ? line[1]!.replace(QUOTES_AROUND, '').split(':').filter(Boolean) : []
62
+ const base = files.filter(file => !file.startsWith(`${overrides}/`))
63
+
64
+ if (base.length)
65
+ return base
66
+
67
+ const found = COMPOSE_FILES.find(file => existsSync(join(dir, file)))
68
+
69
+ if (found === undefined)
70
+ throw new WtError(`no compose file in ${dir}`)
71
+
72
+ return [found]
73
+ }
74
+
75
+ const q = (value: string) => JSON.stringify(value)
76
+ const DEVPROXY_HOST_LABEL = /^devproxy\.[\w-]+\.host$/
77
+ const SERVICE = /^[^:]*/
78
+
79
+ /**
80
+ * The devproxy labels routing the worktree's hosts, by service: each manifest route on the service it names, a dev server route on the first routed service, which only has to be running.
81
+ * Host labels the stack's own compose file sets are rewritten to the worktree's hosts, so main's never get claimed twice.
82
+ */
83
+ export function proxyLabels(worktree: Worktree, config: ComposeConfig): Map<string, [string, string][]> {
84
+ const { routes, skip = [] } = worktree.project
85
+ const labels = new Map<string, [string, string][]>()
86
+ const add = (service: string, ...entries: [string, string][]) => labels.set(service, [...labels.get(service) ?? [], ...entries])
87
+ const routed = routes.find(route => route.to !== 'client')?.to
88
+ const anchor = routed === undefined ? Object.keys(config.services).find(service => !skip.includes(service)) : SERVICE.exec(routed)![0]
89
+
90
+ for (const [index, route] of routes.entries()) {
91
+ const key = `devproxy.wt${index}`
92
+ const host: [string, string] = [`${key}.host`, worktreeHost(route.host, worktree.task)]
93
+
94
+ if (route.to === 'client') {
95
+ if (anchor !== undefined)
96
+ add(anchor, host, [`${key}.to`, `host:${worktree.clientPort}`])
97
+
98
+ continue
99
+ }
100
+
101
+ const [service = '', port = ''] = route.to.split(':')
102
+
103
+ add(service, host, [`${key}.port`, port], ...route.passthrough ? [[`${key}.passthrough`, 'true'] as [string, string]] : [])
104
+ }
105
+
106
+ for (const [service, settings] of Object.entries(config.services)) {
107
+ for (const [key, value] of Object.entries(settings.labels ?? {})) {
108
+ if (DEVPROXY_HOST_LABEL.test(key))
109
+ add(service, [key, value.split(',').map(host => worktreeHost(host.trim(), worktree.task)).join(',')])
110
+ }
111
+ }
112
+
113
+ return labels
114
+ }
115
+
116
+ // Container names, host ports and shared-network aliases keep two copies of a stack from running side by side, so the override takes them away. Old container names come back as aliases inside the stack's own networks, where its config still dials them.
117
+ export function renderOverride(worktree: Worktree, config: ComposeConfig): string {
118
+ const skip = new Set(worktree.project.skip)
119
+ const routeHosts = new Set(worktree.project.routeHosts)
120
+ const labels = proxyLabels(worktree, config)
121
+ // The shared network devproxy serves from, the one named in its config.
122
+ const network = worktree.project.settings.proxyNetwork
123
+ // The proxy publishes 443 on the host, so a container reaches the worktree's own hosts through the gateway.
124
+ const extraHosts = worktree.project.routes
125
+ .filter(route => !route.host.startsWith('*.'))
126
+ .map(route => ` - ${q(`${worktreeHost(route.host, worktree.task)}:host-gateway`)}`)
127
+ const lines = [`# Generated by wt for ${worktree.id}, rewritten on every \`wt sync\`.`, 'services:']
128
+ let joined = false
129
+
130
+ for (const [name, service] of Object.entries(config.services)) {
131
+ const body: string[] = []
132
+
133
+ if (skip.has(name))
134
+ body.push(` profiles: [${q(SKIP_PROFILE)}]`)
135
+
136
+ if (service.container_name !== undefined && service.container_name !== '')
137
+ body.push(' container_name: !reset null')
138
+
139
+ if ((service.ports?.length ?? 0) > 0)
140
+ body.push(' ports: !reset []')
141
+
142
+ if (routeHosts.has(name) && extraHosts.length)
143
+ body.push(' extra_hosts:', ...extraHosts)
144
+
145
+ if (labels.has(name))
146
+ body.push(' labels:', ...labels.get(name)!.map(([key, value]) => ` ${q(key)}: ${q(value)}`))
147
+
148
+ const networks = (service.network_mode ?? '') !== '' ? [] : Object.entries(service.networks ?? {})
149
+ // devproxy dials a service it routes by container name, so one off the proxy network joins it; every other network is listed too, since a networks list in an override replaces the implicit default.
150
+ const joins = (service.network_mode ?? '') === '' && !skip.has(name) && !(network in (service.networks ?? {})) && labels.get(name)?.some(([key]) => key.endsWith('.port')) === true
151
+ const networkLines = networks.flatMap(([own, settings]) => {
152
+ if (own === network)
153
+ return (settings?.aliases?.length ?? 0) > 0 ? [` ${network}:`, ' aliases: !reset []'] : []
154
+
155
+ if (service.container_name !== undefined && service.container_name !== '')
156
+ return [` ${q(own)}:`, ` aliases: [${q(service.container_name)}]`]
157
+
158
+ return joins ? [` ${q(own)}: {}`] : []
159
+ })
160
+
161
+ if (joins) {
162
+ joined = true
163
+ networkLines.push(` ${network}: {}`)
164
+ }
165
+
166
+ if (networkLines.length)
167
+ body.push(' networks:', ...networkLines)
168
+
169
+ if (body.length)
170
+ lines.push(` ${q(name)}:`, ...body)
171
+ }
172
+
173
+ if (joined && config.networks?.[network] === undefined)
174
+ lines.push('networks:', ` ${network}:`, ` name: ${network}`, ' external: true')
175
+
176
+ return `${lines.join('\n')}\n`
177
+ }
178
+
179
+ // Things the override cannot make safe, reported rather than silently shared with main.
180
+ export function overrideWarnings(worktree: Worktree, config: ComposeConfig): string[] {
181
+ const skip = new Set(worktree.project.skip)
182
+ const network = worktree.project.settings.proxyNetwork
183
+ const warnings: string[] = []
184
+
185
+ for (const [name, service] of Object.entries(config.services)) {
186
+ if (skip.has(name))
187
+ continue
188
+
189
+ if (service.network_mode === 'host')
190
+ warnings.push(`service ${name} runs on the host network and may collide with main, consider adding it to skip`)
191
+
192
+ // Compose aliases a service under its own name on every network it joins, and `aliases: !reset []` cannot take that one away: on the shared network the worktree answers to it alongside main. Only an alias main keeps to itself leaves the two of them apart.
193
+ if (service.networks && network in service.networks && !service.networks[network]?.aliases?.some(alias => alias !== name))
194
+ 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`)
195
+ }
196
+
197
+ for (const [key, volume] of Object.entries(config.volumes ?? {})) {
198
+ if (volume.external || (volume.name !== undefined && !volume.name.startsWith(`${config.name}_`)))
199
+ warnings.push(`volume ${key} is named ${volume.name ?? key} outside the project, main and the worktree share it`)
200
+ }
201
+
202
+ return warnings
203
+ }
204
+
205
+ export async function runningProjects(): Promise<Set<string>> {
206
+ const result = await $`docker compose ls --format json`.quiet().nothrow()
207
+
208
+ if (result.exitCode !== 0)
209
+ return new Set()
210
+
211
+ return new Set((JSON.parse(result.stdout.toString()) as { Name: string }[]).map(stack => stack.Name))
212
+ }
package/src/config.ts ADDED
@@ -0,0 +1,83 @@
1
+ /** Where wt keeps things and how it runs a stack, from `~/.config/wt/config.ts` (`$WT_CONFIG`), every key optional. */
2
+
3
+ import { existsSync } from 'node:fs'
4
+ import { homedir } from 'node:os'
5
+ import { join } from 'node:path'
6
+ import { KnownError } from './errors'
7
+
8
+ export interface WtConfig {
9
+ /** The folder of `<name>.ts` manifests. */
10
+ manifests?: string
11
+ /** The folder a project's worktrees go in, `{root}` standing for its checkout's parent and `{repo}` for the checkout's folder name. */
12
+ worktrees?: string
13
+ /** The folder of the compose overrides wt generates, one per worktree. */
14
+ overrides?: string
15
+ /** Setup markers, clone progress and queues. */
16
+ state?: string
17
+ /** Job files and locks, one set per stack; another tool pointing at the same folder shares a lock per stack with wt. */
18
+ jobs?: string
19
+ /** The network the shared proxy dials the stacks on. */
20
+ proxyNetwork?: string
21
+ /** Argv templates replacing the default stack commands, `{project}`, `{task}`, `{dir}` and `{id}` filled in; `removed` runs after `wt rm`. */
22
+ runner?: { up?: string[], down?: string[], removed?: string[] }
23
+ }
24
+
25
+ export interface Resolved {
26
+ manifests: string
27
+ worktrees: string
28
+ overrides: string
29
+ state: string
30
+ jobs: string
31
+ proxyNetwork: string
32
+ runner: { up?: string[], down?: string[], removed?: string[] }
33
+ }
34
+
35
+ const LEADING_TILDE = /^~(?=\/|$)/
36
+
37
+ export function expandHome(path: string): string {
38
+ return path.replace(LEADING_TILDE, homedir())
39
+ }
40
+
41
+ export const configPath = () => process.env.WT_CONFIG ?? join((process.env.XDG_CONFIG_HOME ?? '') || join(homedir(), '.config'), 'wt', 'config.ts')
42
+
43
+ export function resolveConfig(config: WtConfig = {}): Resolved {
44
+ const configDir = join(configPath(), '..')
45
+ const state = expandHome(config.state ?? join(homedir(), '.cache', 'wt'))
46
+
47
+ return {
48
+ manifests: expandHome(config.manifests ?? join(configDir, 'projects')),
49
+ worktrees: expandHome(config.worktrees ?? '{root}/.worktrees/{repo}'),
50
+ overrides: expandHome(config.overrides ?? join(homedir(), '.local', 'share', 'wt', 'overrides')),
51
+ state,
52
+ jobs: expandHome(config.jobs ?? join(state, 'jobs')),
53
+ proxyNetwork: config.proxyNetwork ?? 'proxy',
54
+ runner: config.runner ?? {},
55
+ }
56
+ }
57
+
58
+ let loaded: Resolved | undefined
59
+
60
+ /** The config, read once per process; none at its path means every default. */
61
+ export async function wtConfig(): Promise<Resolved> {
62
+ if (loaded)
63
+ return loaded
64
+
65
+ const path = configPath()
66
+
67
+ if (!existsSync(path))
68
+ return (loaded = resolveConfig())
69
+
70
+ try {
71
+ const { default: config } = await import(path) as { default?: WtConfig }
72
+
73
+ return (loaded = resolveConfig(config))
74
+ }
75
+ catch (error) {
76
+ throw new KnownError(`${path} does not load: ${error instanceof Error ? error.message : String(error)}`)
77
+ }
78
+ }
79
+
80
+ /** Fills a runner template's placeholders. */
81
+ export function fillTemplate(argv: string[], values: Record<string, string>): string[] {
82
+ return argv.map(arg => arg.replaceAll(/\{(\w+)\}/g, (match, key: string) => values[key] ?? match))
83
+ }
@@ -0,0 +1,7 @@
1
+ import { existsSync, readFileSync } from 'node:fs'
2
+ import { entries, parseDotenv } from '@kaynooo/cli-env/dotenv'
3
+
4
+ /** The variables of a .env file, without loading them into the environment; none when the file is missing. */
5
+ export function readEnvFile(path: string): Record<string, string> {
6
+ return existsSync(path) ? Object.fromEntries(entries(parseDotenv(readFileSync(path, 'utf8')))) : {}
7
+ }
package/src/errors.ts ADDED
@@ -0,0 +1,36 @@
1
+ import { readFileSync } from 'node:fs'
2
+ import { fail } from '@kaynooo/cli-kit'
3
+
4
+ /** A failure the user is to fix (a manifest, a ref, an action refused): its message alone ends the command, where anything else shows its stack trace. */
5
+ export class KnownError extends Error {}
6
+
7
+ const BOOT_TIME_LINE = /^btime (\d+)$/m
8
+
9
+ let bootedAt: number | undefined
10
+
11
+ /** When this machine booted, in ms: lock and ticket files outlive a reboot, and a pid written before it may name an unrelated process since. */
12
+ export function bootTime(): number {
13
+ bootedAt ??= Number(readFileSync('/proc/stat', 'utf8').match(BOOT_TIME_LINE)?.[1] ?? 0) * 1000
14
+
15
+ return bootedAt
16
+ }
17
+
18
+ /** Whether a process runs under that pid, one of another user's included. */
19
+ export function alive(pid: number): boolean {
20
+ try {
21
+ process.kill(pid, 0)
22
+
23
+ return true
24
+ }
25
+ catch (error) {
26
+ return (error as NodeJS.ErrnoException).code === 'EPERM'
27
+ }
28
+ }
29
+
30
+ /** Ends the command on a known failure with its message and exit code 1; anything else is thrown on. */
31
+ export function failKnown(error: unknown): never {
32
+ if (error instanceof KnownError)
33
+ fail(error.message, 1)
34
+
35
+ throw error
36
+ }
package/src/files.ts ADDED
@@ -0,0 +1,333 @@
1
+ import type { ComposeConfig } from './compose'
2
+ import type { Route, Worktree } from './project'
3
+ import { existsSync, lstatSync, mkdirSync, readFileSync, renameSync, rmSync, statSync, symlinkSync, unlinkSync, writeFileSync } from 'node:fs'
4
+ import { constants } from 'node:os'
5
+ import { basename, dirname, join } from 'node:path'
6
+ import { escapeRegExp } from '@kaynooo/utils/text'
7
+ import { $ } from 'bun'
8
+ import { baseComposeFiles } from './compose'
9
+ import { worktreeHost, WtError } from './project'
10
+ import { inLine, queueDir } from './queue'
11
+
12
+ // A copy lands under this suffix and is renamed once whole, so one cut short is never taken for done.
13
+ const PARTIAL = '.wt-partial'
14
+ const PAUSE_SIGNALS: NodeJS.Signals[] = ['SIGINT', 'SIGTERM', 'SIGHUP']
15
+
16
+ export function writeIfChanged(path: string, content: string): boolean {
17
+ if (existsSync(path) && readFileSync(path, 'utf8') === content)
18
+ return false
19
+
20
+ mkdirSync(dirname(path), { recursive: true })
21
+ writeFileSync(path, content)
22
+
23
+ return true
24
+ }
25
+
26
+ export async function volumeExists(name: string): Promise<boolean> {
27
+ return (await $`docker volume inspect ${name}`.quiet().nothrow()).exitCode === 0
28
+ }
29
+
30
+ export async function copyFromMain(worktree: Worktree): Promise<string[]> {
31
+ const warnings: string[] = []
32
+
33
+ for (const entry of worktree.project.copy ?? []) {
34
+ const source = join(worktree.project.root, entry)
35
+ const target = join(worktree.dir, entry)
36
+
37
+ if (!existsSync(source)) {
38
+ warnings.push(`copy: ${entry} does not exist in main`)
39
+
40
+ continue
41
+ }
42
+
43
+ if (existsSync(target))
44
+ continue
45
+
46
+ const partial = `${target}${PARTIAL}`
47
+
48
+ mkdirSync(dirname(target), { recursive: true })
49
+ rmSync(partial, { recursive: true, force: true })
50
+ // A reflink shares the blocks of a vendor or node_modules copy on btrfs, and is a plain copy elsewhere.
51
+ await $`cp -a --reflink=auto ${source} ${partial}`.quiet()
52
+ renameSync(partial, target)
53
+ }
54
+
55
+ return warnings
56
+ }
57
+
58
+ export function linkFromMain(worktree: Worktree): void {
59
+ for (const entry of worktree.project.link ?? []) {
60
+ const source = join(worktree.project.root, entry)
61
+ const target = join(worktree.dir, entry)
62
+
63
+ if (existsSync(source) && !existsSync(target)) {
64
+ mkdirSync(dirname(target), { recursive: true })
65
+ symlinkSync(source, target)
66
+ }
67
+ }
68
+ }
69
+
70
+ const WILDCARD_LABEL = /^\*\./
71
+
72
+ // Main hosts become the worktree's; the lookbehind keeps api.booki.localhost whole and makes a second pass a no-op.
73
+ export function rewriteHosts(text: string, routes: Route[], task: string): string {
74
+ const hosts = [...new Set(routes.map(route => route.host.replace(WILDCARD_LABEL, '')))].sort((a, b) => b.length - a.length)
75
+ const pattern = new RegExp(`(?<![\\w.-])(\\.?)(${hosts.map(escapeRegExp).join('|')})(?![\\w.-])`, 'g')
76
+
77
+ return text.replace(pattern, (_, dot: string, host: string) => `${dot}${worktreeHost(host, task)}`)
78
+ }
79
+
80
+ export function setEnvValue(text: string, key: string, value: string): string {
81
+ const line = `${key}=${value}`
82
+ const pattern = new RegExp(`^${escapeRegExp(key)}=.*$`, 'm')
83
+
84
+ if (pattern.test(text))
85
+ return text.replace(pattern, line)
86
+
87
+ return `${text}${text === '' || text.endsWith('\n') ? '' : '\n'}${line}\n`
88
+ }
89
+
90
+ export function expandEnvValue(value: string, worktree: Worktree): string {
91
+ return value.replaceAll('{task}', worktree.task).replaceAll('{port}', String(worktree.clientPort))
92
+ }
93
+
94
+ export function writeEnvFiles(worktree: Worktree): void {
95
+ const { project, task, dir } = worktree
96
+ const envFiles = (project.copy ?? []).filter(entry => basename(entry).startsWith('.env') && statSync(join(dir, entry), { throwIfNoEntry: false })?.isFile())
97
+
98
+ for (const entry of new Set([...envFiles, '.env'])) {
99
+ const file = join(dir, entry)
100
+ let text = existsSync(file) ? rewriteHosts(readFileSync(file, 'utf8'), project.routes, task) : ''
101
+
102
+ for (const [key, value] of Object.entries(project.env?.[entry] ?? {}))
103
+ text = setEnvValue(text, key, expandEnvValue(value, worktree))
104
+
105
+ if (entry === '.env') {
106
+ text = setEnvValue(text, 'COMPOSE_PROJECT_NAME', worktree.id)
107
+ text = setEnvValue(text, 'COMPOSE_FILE', [...baseComposeFiles(dir, worktree.project.settings.overrides), worktree.files.override].join(':'))
108
+ }
109
+
110
+ writeIfChanged(file, text)
111
+ }
112
+ }
113
+
114
+ // A worktree made by the old wt symlinked its data to main's; the link goes so the copy can take its place.
115
+ function unlinkLegacySymlink(dir: string, entry: string): void {
116
+ let path = dir
117
+
118
+ for (const part of entry.split('/')) {
119
+ path = join(path, part)
120
+
121
+ const stat = lstatSync(path, { throwIfNoEntry: false })
122
+
123
+ if (!stat)
124
+ return
125
+
126
+ if (stat.isSymbolicLink())
127
+ return unlinkSync(path)
128
+ }
129
+ }
130
+
131
+ async function mainContainers(main: ComposeConfig, status: 'running' | 'paused'): Promise<string[]> {
132
+ return (await $`docker ps -q --filter ${`status=${status}`} --filter ${`label=com.docker.compose.project=${main.name}`}`.quiet().text()).split('\n').filter(Boolean)
133
+ }
134
+
135
+ // Synchronous, so a signal handler can run it before the process exits.
136
+ function unpause(ids: string[]): void {
137
+ if (ids.length)
138
+ Bun.spawnSync(['docker', 'unpause', ...ids], { stdout: 'ignore', stderr: 'ignore' })
139
+ }
140
+
141
+ /**
142
+ * A Ctrl+C or a kill of the job runs `abort` and unpauses main on the way out; only a SIGKILL leaves it paused, for the next sync to find.
143
+ * `record` is the file the paused ids are kept in meanwhile, so that sync unpauses those alone and not a container paused by hand.
144
+ */
145
+ export async function whilePaused<T>(ids: string[], work: () => Promise<T>, abort?: () => void, record?: string): Promise<T> {
146
+ const release = () => {
147
+ unpause(ids)
148
+
149
+ if (record !== undefined)
150
+ rmSync(record, { force: true })
151
+ }
152
+ const onSignal = (signal: NodeJS.Signals) => {
153
+ abort?.()
154
+ release()
155
+ process.exit(128 + constants.signals[signal])
156
+ }
157
+
158
+ for (const signal of PAUSE_SIGNALS)
159
+ process.once(signal, onSignal)
160
+
161
+ try {
162
+ if (ids.length) {
163
+ if (record !== undefined) {
164
+ mkdirSync(dirname(record), { recursive: true })
165
+ writeFileSync(record, `${ids.join('\n')}\n`)
166
+ }
167
+
168
+ await $`docker pause ${ids}`.quiet()
169
+ }
170
+
171
+ return await work()
172
+ }
173
+ finally {
174
+ for (const signal of PAUSE_SIGNALS)
175
+ process.off(signal, onSignal)
176
+
177
+ release()
178
+ }
179
+ }
180
+
181
+ /** Runs every copy to its end, a failed one included, so main is unpaused only once none is still reading it; the failures come back. */
182
+ export async function settle(copies: (() => Promise<void>)[]): Promise<unknown[]> {
183
+ const results = await Promise.allSettled(copies.map(async copy => copy()))
184
+
185
+ return results.flatMap(result => result.status === 'rejected' ? [result.reason as unknown] : [])
186
+ }
187
+
188
+ /** What a failed command said, else the error's message. */
189
+ export function failureText(error: unknown): string {
190
+ if (error instanceof $.ShellError)
191
+ return error.stderr.toString().trim() || error.message
192
+
193
+ return error instanceof Error ? error.message : String(error)
194
+ }
195
+
196
+ // The ids of main's containers a clone of the project paused, one per line, while it copies.
197
+ const pausedRecord = (worktree: Worktree) => queueDir(worktree.project.settings, `${worktree.project.name}.paused`)
198
+
199
+ function readPending(file: string): Set<string> {
200
+ return new Set(existsSync(file) ? readFileSync(file, 'utf8').split('\n').filter(Boolean) : [])
201
+ }
202
+
203
+ function writePending(file: string, volumes: Iterable<string>): void {
204
+ const text = [...volumes].join('\n')
205
+
206
+ if (!text)
207
+ return rmSync(file, { force: true })
208
+
209
+ mkdirSync(dirname(file), { recursive: true })
210
+ writeFileSync(file, `${text}\n`)
211
+ }
212
+
213
+ /**
214
+ * Each worktree starts from a copy of main's data, taken while main's containers are paused so a database is never copied mid-write.
215
+ * Clones of one project run one at a time, so no clone unpauses main while another still copies; a copy cut short is redone by the next sync, and main's containers a killed clone left paused are unpaused, those it had paused only.
216
+ */
217
+ export async function cloneData(worktree: Worktree, main: ComposeConfig): Promise<string[]> {
218
+ 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 () => {
219
+ const record = pausedRecord(worktree)
220
+ const recorded = readPending(record)
221
+ const leftover = recorded.size ? (await mainContainers(main, 'paused')).filter(id => recorded.has(id)) : []
222
+
223
+ unpause(leftover)
224
+ rmSync(record, { force: true })
225
+
226
+ const warnings = leftover.length ? [`clone: unpaused ${leftover.length} of main's containers a clone cut short had left paused`] : []
227
+
228
+ return [...warnings, ...await cloneInLine(worktree, main)]
229
+ })
230
+ }
231
+
232
+ async function cloneInLine(worktree: Worktree, main: ComposeConfig): Promise<string[]> {
233
+ const warnings: string[] = []
234
+ const pending: { volume?: string, copy: () => Promise<void> }[] = []
235
+ const unfinished = readPending(worktree.files.clonesPending)
236
+ // The copies run in containers, which a signal to this process alone would leave copying from an unpaused main
237
+ const copyLabel = `wt.clone=${worktree.id}`
238
+ const stopCopies = () => {
239
+ const ids = Bun.spawnSync(['docker', 'ps', '-q', '--filter', `label=${copyLabel}`]).stdout.toString().split('\n').filter(Boolean)
240
+
241
+ if (ids.length)
242
+ Bun.spawnSync(['docker', 'rm', '-f', ...ids], { stdout: 'ignore', stderr: 'ignore' })
243
+ }
244
+
245
+ for (const entry of worktree.project.clone ?? []) {
246
+ const source = join(worktree.project.root, entry)
247
+
248
+ if (existsSync(source)) {
249
+ unlinkLegacySymlink(worktree.dir, entry)
250
+
251
+ const target = join(worktree.dir, entry)
252
+
253
+ if (existsSync(target))
254
+ continue
255
+
256
+ if (!statSync(source).isDirectory())
257
+ throw new WtError(`clone: ${entry} is not a directory`)
258
+
259
+ // Copied beside the target then renamed, all as the container's root since what main holds may be root's
260
+ const partial = `/to/${basename(target)}${PARTIAL}`
261
+
262
+ pending.push({
263
+ copy: async () => {
264
+ mkdirSync(dirname(target), { recursive: true })
265
+ 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()
266
+ },
267
+ })
268
+
269
+ continue
270
+ }
271
+
272
+ const volume = main.volumes?.[entry]
273
+
274
+ if (!volume) {
275
+ warnings.push(`clone: ${entry} is neither a path in main nor a volume of its stack`)
276
+
277
+ continue
278
+ }
279
+
280
+ if (volume.driver_opts?.device !== undefined && volume.driver_opts.device !== '')
281
+ throw new WtError(`clone: volume ${entry} is a bind to ${volume.driver_opts.device}, list that path instead`)
282
+
283
+ const sourceVolume = volume.name ?? `${main.name}_${entry}`
284
+ const targetVolume = `${worktree.id}_${entry}`
285
+
286
+ if (await volumeExists(targetVolume)) {
287
+ if (!unfinished.has(targetVolume))
288
+ continue
289
+
290
+ const removed = await $`docker volume rm ${targetVolume}`.quiet().nothrow()
291
+
292
+ if (removed.exitCode !== 0)
293
+ 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}\``)
294
+ }
295
+
296
+ if (!await volumeExists(sourceVolume)) {
297
+ warnings.push(`clone: main has no volume ${sourceVolume} yet, the worktree starts with an empty one`)
298
+
299
+ continue
300
+ }
301
+
302
+ pending.push({
303
+ volume: targetVolume,
304
+ copy: async () => {
305
+ await $`docker volume create --label ${`com.docker.compose.project=${worktree.id}`} --label ${`com.docker.compose.volume=${entry}`} ${targetVolume}`.quiet()
306
+ await $`docker run --rm --label ${copyLabel} -v ${`${sourceVolume}:/from:ro`} -v ${`${targetVolume}:/to`} alpine cp -a /from/. /to/`.quiet()
307
+ unfinished.delete(targetVolume)
308
+ },
309
+ })
310
+ }
311
+
312
+ if (!pending.length) {
313
+ writePending(worktree.files.clonesPending, unfinished)
314
+
315
+ return warnings
316
+ }
317
+
318
+ for (const { volume } of pending) {
319
+ if (volume !== undefined)
320
+ unfinished.add(volume)
321
+ }
322
+
323
+ writePending(worktree.files.clonesPending, unfinished)
324
+
325
+ const failures = await whilePaused(await mainContainers(main, 'running'), async () => settle(pending.map(({ copy }) => copy)), stopCopies, pausedRecord(worktree))
326
+
327
+ writePending(worktree.files.clonesPending, unfinished)
328
+
329
+ if (failures.length)
330
+ throw new WtError(`clone failed, \`wt sync ${worktree.task}\` starts it again:\n${failures.map(failureText).join('\n')}`)
331
+
332
+ return warnings
333
+ }