@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/src/project.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import type { Resolved } from './config'
|
|
2
2
|
import { existsSync, readdirSync, realpathSync } from 'node:fs'
|
|
3
3
|
import { basename, dirname, join } from 'node:path'
|
|
4
|
+
import { warn } from '@kaynooo/cli-kit'
|
|
4
5
|
import { expandHome, fillTemplate, wtConfig } from './config'
|
|
5
|
-
import { KnownError } from './errors'
|
|
6
|
+
import { failureText, KnownError } from './errors'
|
|
6
7
|
import { stringToPort } from './port'
|
|
7
8
|
|
|
8
9
|
export interface Route {
|
|
@@ -18,7 +19,7 @@ export interface ProjectConfig {
|
|
|
18
19
|
// Checkout the worktrees are made from.
|
|
19
20
|
root: string
|
|
20
21
|
// Nested repos checked out on the task branch, the root then a detached worktree; with none, the root itself is on the branch.
|
|
21
|
-
repos
|
|
22
|
+
repos?: string[]
|
|
22
23
|
// Gitignored files and dirs copied from main. Hosts in the `.env*` files among them are rewritten.
|
|
23
24
|
copy?: string[]
|
|
24
25
|
// Data each worktree gets its own copy of: a path under root, or the key of a named volume.
|
|
@@ -32,13 +33,16 @@ export interface ProjectConfig {
|
|
|
32
33
|
routeHosts?: string[]
|
|
33
34
|
// Values forced into copied env files, `{task}` standing for the task name and `{port}` for its dev server port.
|
|
34
35
|
env?: Record<string, Record<string, string>>
|
|
36
|
+
// A worktree's dev server port by task, in place of the one hashed from its name, for one that clashes.
|
|
37
|
+
ports?: Record<string, number>
|
|
35
38
|
// Where the stack's own nginx reads certificates from, relative to root. Required by passthrough routes.
|
|
36
39
|
certs?: string
|
|
37
40
|
// Commands run once with sh, in the worktree, the first time its stack comes up.
|
|
38
41
|
setup?: string[]
|
|
39
42
|
// What `wt run` needs to run a command of the worktree in a throwaway container, beside its own test database.
|
|
40
43
|
run?: RunConfig
|
|
41
|
-
routes
|
|
44
|
+
// The hosts devproxy routes to the worktree's stack; none leaves it unrouted.
|
|
45
|
+
routes?: Route[]
|
|
42
46
|
}
|
|
43
47
|
|
|
44
48
|
export interface RunConfig {
|
|
@@ -55,6 +59,8 @@ export interface RunConfig {
|
|
|
55
59
|
}
|
|
56
60
|
|
|
57
61
|
export interface Project extends ProjectConfig {
|
|
62
|
+
repos: string[]
|
|
63
|
+
routes: Route[]
|
|
58
64
|
name: string
|
|
59
65
|
// The wt config the manifest was loaded under, so every path below follows it.
|
|
60
66
|
settings: Resolved
|
|
@@ -72,10 +78,12 @@ export interface Worktree {
|
|
|
72
78
|
|
|
73
79
|
export class WtError extends KnownError {}
|
|
74
80
|
|
|
75
|
-
// A DNS label, as the task prefixes every host of the worktree.
|
|
76
|
-
const TASK_PATTERN = /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/
|
|
81
|
+
// A DNS label, as the task prefixes every host of the worktree, without the `--` that joins a project to its task in a slug: task b--c of project a would share a--b--c with task c of project a--b.
|
|
82
|
+
const TASK_PATTERN = /^(?!.*--)[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/
|
|
77
83
|
|
|
78
84
|
const ROUTE_TARGET = /^client$|^[\w.-]+:\d+$/
|
|
85
|
+
const NOT_IN_STACK_NAME = /[^\w-]/g
|
|
86
|
+
const LEADING_SEPARATORS = /^[_-]+/
|
|
79
87
|
|
|
80
88
|
// Every file wt generates for a worktree outside of it, so `wt rm` has one list to delete.
|
|
81
89
|
export function worktreeFiles(settings: Resolved, id: string) {
|
|
@@ -91,8 +99,36 @@ export function worktreeHost(host: string, task: string): string {
|
|
|
91
99
|
return host.startsWith('*.') ? `*.${task}.${host.slice(2)}` : `${task}.${host}`
|
|
92
100
|
}
|
|
93
101
|
|
|
102
|
+
// Every key of the manifest types, the compiler holding these lists to them
|
|
103
|
+
const PROJECT_KEYS = new Set(Object.keys({ root: 0, repos: 0, copy: 0, clone: 0, link: 0, skip: 0, routeHosts: 0, env: 0, ports: 0, certs: 0, setup: 0, run: 0, routes: 0 } satisfies Record<keyof ProjectConfig, 0>))
|
|
104
|
+
const RUN_KEYS = new Set(Object.keys({ service: 0, dir: 0, provision: 0, dns: 0, env: 0 } satisfies Record<keyof RunConfig, 0>))
|
|
105
|
+
|
|
106
|
+
// A misspelt key is ignored as if it were absent, so it is named rather than silently dropped.
|
|
107
|
+
function warnUnknownKeys(name: string, config: ProjectConfig): void {
|
|
108
|
+
const unknown = [
|
|
109
|
+
...Object.keys(config).filter(key => !PROJECT_KEYS.has(key)),
|
|
110
|
+
...Object.keys(config.run ?? {}).filter(key => !RUN_KEYS.has(key)).map(key => `run.${key}`),
|
|
111
|
+
]
|
|
112
|
+
|
|
113
|
+
for (const key of unknown)
|
|
114
|
+
warn(`${name}.ts: unknown key ${key}, ignored`)
|
|
115
|
+
}
|
|
116
|
+
|
|
94
117
|
// Caught when the manifest loads, not halfway through a `wt add` that has already cloned data.
|
|
95
118
|
function assertValid(project: Project): void {
|
|
119
|
+
for (const repo of ['.', ...project.repos]) {
|
|
120
|
+
const dir = join(project.root, repo)
|
|
121
|
+
|
|
122
|
+
if (!existsSync(join(dir, '.git')))
|
|
123
|
+
throw new WtError(`${project.name}: ${repo === '.' ? `root ${dir}` : `repo ${repo}`} is not a git checkout${existsSync(dir) ? '' : ', the folder is missing'}`)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
for (const [task, port] of Object.entries(project.ports ?? {})) {
|
|
127
|
+
// A port under 1024 needs root to listen on, and wt cannot tell whether one is taken
|
|
128
|
+
if (!Number.isInteger(port) || port < 1024 || port > 65535)
|
|
129
|
+
throw new WtError(`${project.name}: ports gives ${task} ${String(port)}, expected a whole number from 1024 to 65535`)
|
|
130
|
+
}
|
|
131
|
+
|
|
96
132
|
for (const route of project.routes) {
|
|
97
133
|
if (!ROUTE_TARGET.test(route.to))
|
|
98
134
|
throw new WtError(`${project.name}: route ${route.host} goes to "${route.to}", expected "client" or "<service>:<port>"`)
|
|
@@ -102,34 +138,80 @@ function assertValid(project: Project): void {
|
|
|
102
138
|
}
|
|
103
139
|
}
|
|
104
140
|
|
|
105
|
-
|
|
141
|
+
/** One manifest, checked; any failure names its file. */
|
|
142
|
+
async function loadManifest(settings: Resolved, file: string): Promise<Project> {
|
|
143
|
+
const path = join(settings.manifests, file)
|
|
144
|
+
let config: unknown
|
|
145
|
+
|
|
146
|
+
try {
|
|
147
|
+
config = ((await import(path)) as { default?: unknown }).default
|
|
148
|
+
}
|
|
149
|
+
catch (error) {
|
|
150
|
+
throw new WtError(`${path} does not load: ${error instanceof Error ? error.message : String(error)}`)
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (typeof config !== 'object' || config === null || typeof (config as { root?: unknown }).root !== 'string')
|
|
154
|
+
throw new WtError(`${path} has no default export with a \`root\``, 'export default { root: \'~/Workspace/<checkout>\', ... } satisfies ProjectConfig')
|
|
155
|
+
|
|
156
|
+
const manifest = config as ProjectConfig
|
|
157
|
+
const project: Project = { ...manifest, repos: manifest.repos ?? [], routes: manifest.routes ?? [], root: expandHome(manifest.root), name: basename(file, '.ts'), settings }
|
|
158
|
+
|
|
159
|
+
warnUnknownKeys(project.name, manifest)
|
|
160
|
+
assertValid(project)
|
|
161
|
+
|
|
162
|
+
// A worktree's stack is named after its checkout's folder, kept so that running stacks keep their containers and volumes.
|
|
163
|
+
if (stackName(project) === '')
|
|
164
|
+
throw new WtError(`${project.name}.ts: the checkout folder ${basename(project.root)} holds no letter or digit to name its compose stacks after`, 'rename the folder')
|
|
165
|
+
|
|
166
|
+
return project
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** Every project whose manifest loads; one that does not is skipped, `skipped` told why (a warning by default), so it cannot break the others. */
|
|
170
|
+
export async function loadProjects(skipped: (message: string) => void = warn): Promise<Project[]> {
|
|
106
171
|
const settings = await wtConfig()
|
|
107
172
|
const files = existsSync(settings.manifests) ? readdirSync(settings.manifests).filter(file => file.endsWith('.ts')).sort() : []
|
|
173
|
+
const loaded = await Promise.allSettled(files.map(async file => loadManifest(settings, file)))
|
|
174
|
+
const projects: Project[] = []
|
|
175
|
+
|
|
176
|
+
for (const result of loaded) {
|
|
177
|
+
if (result.status === 'rejected') {
|
|
178
|
+
skipped(`manifest skipped, ${failureText(result.reason)}`)
|
|
108
179
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
180
|
+
continue
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const project = result.value
|
|
184
|
+
const twin = projects.find(other => stackName(other) === stackName(project))
|
|
112
185
|
|
|
113
|
-
|
|
186
|
+
if (twin)
|
|
187
|
+
skipped(`${twin.name}.ts and ${project.name}.ts both have a checkout compose names ${stackName(project)}, so their worktrees would share stacks and volumes: ${project.name} skipped, rename the folder of a checkout that has no worktrees yet, or remove one of the files`)
|
|
188
|
+
else
|
|
189
|
+
projects.push(project)
|
|
190
|
+
}
|
|
114
191
|
|
|
115
|
-
|
|
116
|
-
}))
|
|
192
|
+
return projects
|
|
117
193
|
}
|
|
118
194
|
|
|
195
|
+
/** The project of one manifest, the others left unread: its failure to load ends the command. */
|
|
119
196
|
export async function findProject(name: string): Promise<Project> {
|
|
120
|
-
const
|
|
197
|
+
const settings = await wtConfig()
|
|
121
198
|
|
|
122
|
-
if (!
|
|
123
|
-
throw new WtError(`no project named ${name} in ${
|
|
199
|
+
if (!existsSync(join(settings.manifests, `${name}.ts`)))
|
|
200
|
+
throw new WtError(`no project named ${name} in ${settings.manifests}`)
|
|
124
201
|
|
|
125
|
-
return
|
|
202
|
+
return loadManifest(settings, `${name}.ts`)
|
|
126
203
|
}
|
|
127
204
|
|
|
128
205
|
export const tasksDir = (project: Project) => fillTemplate([project.settings.worktrees], { root: dirname(project.root), repo: basename(project.root) })[0]!
|
|
129
206
|
|
|
207
|
+
/** The project name compose gives the checkout's folder, which its worktrees' stacks start with: lower case, only letters, digits, _ and -, none of the last two first. */
|
|
208
|
+
export function stackName(project: Pick<Project, 'root'>): string {
|
|
209
|
+
return basename(project.root).toLowerCase().replace(NOT_IN_STACK_NAME, '').replace(LEADING_SEPARATORS, '')
|
|
210
|
+
}
|
|
211
|
+
|
|
130
212
|
export function worktreeOf(project: Project, task: string): Worktree {
|
|
131
213
|
if (task === 'main' || !TASK_PATTERN.test(task))
|
|
132
|
-
throw new WtError(`invalid task name "${task}": lowercase letters, digits and inner dashes, at most 63, and not "main"`)
|
|
214
|
+
throw new WtError(`invalid task name "${task}": lowercase letters, digits and single inner dashes, at most 63, and not "main"`)
|
|
133
215
|
|
|
134
216
|
const mainHosts = new Set(project.routes.map(route => route.host))
|
|
135
217
|
const taken = project.routes.find(route => mainHosts.has(worktreeHost(route.host, task)))
|
|
@@ -137,21 +219,21 @@ export function worktreeOf(project: Project, task: string): Worktree {
|
|
|
137
219
|
if (taken)
|
|
138
220
|
throw new WtError(`invalid task name "${task}": the worktree's ${worktreeHost(taken.host, task)} would be one of main's hosts`)
|
|
139
221
|
|
|
140
|
-
const id = `${
|
|
222
|
+
const id = `${stackName(project)}--${task}`
|
|
141
223
|
|
|
142
224
|
return {
|
|
143
225
|
project,
|
|
144
226
|
task,
|
|
145
227
|
dir: join(tasksDir(project), task),
|
|
146
228
|
id,
|
|
147
|
-
clientPort: stringToPort(`${project.name}--${task}`),
|
|
229
|
+
clientPort: project.ports?.[task] ?? stringToPort(`${project.name}--${task}`),
|
|
148
230
|
files: worktreeFiles(project.settings, id),
|
|
149
231
|
}
|
|
150
232
|
}
|
|
151
233
|
|
|
152
234
|
export function assertWorktreeExists(worktree: Worktree): void {
|
|
153
235
|
if (!existsSync(worktree.dir))
|
|
154
|
-
throw new WtError(`no worktree ${worktree.task}
|
|
236
|
+
throw new WtError(`no worktree ${worktree.task}`, `create it with \`wt add ${worktree.task}\``)
|
|
155
237
|
}
|
|
156
238
|
|
|
157
239
|
export function listTasks(project: Project): string[] {
|
|
@@ -160,13 +242,39 @@ export function listTasks(project: Project): string[] {
|
|
|
160
242
|
return existsSync(dir) ? readdirSync(dir).filter(task => TASK_PATTERN.test(task)).sort() : []
|
|
161
243
|
}
|
|
162
244
|
|
|
245
|
+
/** The directory wt runs in; once deleted from under the shell (after `wt rm --bg`), the path the shell still holds, so locate reads which worktree it was. */
|
|
246
|
+
export function currentDir(): string {
|
|
247
|
+
try {
|
|
248
|
+
return process.cwd()
|
|
249
|
+
}
|
|
250
|
+
catch {
|
|
251
|
+
const pwd = process.env.PWD ?? ''
|
|
252
|
+
|
|
253
|
+
if (pwd === '')
|
|
254
|
+
throw new WtError('the current directory is gone', 'cd elsewhere')
|
|
255
|
+
|
|
256
|
+
return pwd
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// Symlinks resolved, so a cwd the kernel hands back resolved matches a root given through a link; a path gone stays as written.
|
|
261
|
+
function resolved(path: string): string {
|
|
262
|
+
try {
|
|
263
|
+
return realpathSync(path)
|
|
264
|
+
}
|
|
265
|
+
catch {
|
|
266
|
+
return path
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
163
270
|
// The project and task the working directory sits in, `main` when it is the project's own checkout.
|
|
164
271
|
export async function locate(cwd: string, projectName?: string): Promise<{ project: Project, task: string }> {
|
|
165
|
-
const here =
|
|
272
|
+
const here = resolved(cwd)
|
|
166
273
|
const projects = projectName !== undefined ? [await findProject(projectName)] : await loadProjects()
|
|
167
274
|
|
|
168
275
|
for (const project of projects) {
|
|
169
|
-
const dir = tasksDir(project)
|
|
276
|
+
const dir = resolved(tasksDir(project))
|
|
277
|
+
const root = resolved(project.root)
|
|
170
278
|
|
|
171
279
|
if (here.startsWith(`${dir}/`)) {
|
|
172
280
|
const rest = here.slice(dir.length + 1)
|
|
@@ -175,13 +283,16 @@ export async function locate(cwd: string, projectName?: string): Promise<{ proje
|
|
|
175
283
|
return { project, task: slash === -1 ? rest : rest.slice(0, slash) }
|
|
176
284
|
}
|
|
177
285
|
|
|
178
|
-
if (here ===
|
|
286
|
+
if (here === root || here.startsWith(`${root}/`))
|
|
179
287
|
return { project, task: 'main' }
|
|
180
288
|
}
|
|
181
289
|
|
|
182
290
|
if (projects.length === 1)
|
|
183
291
|
return { project: projects[0]!, task: 'main' }
|
|
184
292
|
|
|
293
|
+
if (!existsSync(cwd))
|
|
294
|
+
throw new WtError(`the current directory ${cwd} is gone`, 'cd into a project or worktree, or pass --project')
|
|
295
|
+
|
|
185
296
|
throw new WtError('not inside a known project or worktree (cd into one, or pass --project)')
|
|
186
297
|
}
|
|
187
298
|
|
package/src/proxy.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
/** The shared proxy is devproxy (@kaynooo/devproxy): the one on PATH, else the copy installed with wt. */
|
|
2
2
|
|
|
3
|
+
import { existsSync } from 'node:fs'
|
|
3
4
|
import { dirname, join } from 'node:path'
|
|
5
|
+
import { configBase } from '@kaynooo/cli-kit'
|
|
4
6
|
import { $ } from 'bun'
|
|
5
7
|
import { WtError } from './project'
|
|
6
8
|
|
|
@@ -12,26 +14,24 @@ export interface ProxyHost {
|
|
|
12
14
|
passthrough: boolean
|
|
13
15
|
}
|
|
14
16
|
|
|
15
|
-
// PATH first, so a test sandbox's fake answers instead of the real one.
|
|
16
|
-
function devproxyArgv(): string[] {
|
|
17
|
-
const onPath = Bun.which('devproxy')
|
|
17
|
+
// PATH first, so a test sandbox's fake answers instead of the real one. The installed copy runs with devproxy's own shebang flags, so the cwd's .env (a worktree's COMPOSE_PROJECT_NAME) stays out of it.
|
|
18
|
+
export function devproxyArgv(path = process.env.PATH ?? ''): string[] {
|
|
19
|
+
const onPath = Bun.which('devproxy', { PATH: path })
|
|
18
20
|
|
|
19
21
|
if (onPath !== null)
|
|
20
22
|
return [onPath]
|
|
21
23
|
|
|
22
24
|
try {
|
|
23
|
-
return [process.execPath, join(dirname(Bun.resolveSync('@kaynooo/devproxy', import.meta.dir)), '..', 'dist', 'cli.js')]
|
|
25
|
+
return [process.execPath, '--no-env-file', '--config=/dev/null', join(dirname(Bun.resolveSync('@kaynooo/devproxy', import.meta.dir)), '..', 'dist', 'cli.js')]
|
|
24
26
|
}
|
|
25
27
|
catch {
|
|
26
28
|
return []
|
|
27
29
|
}
|
|
28
30
|
}
|
|
29
31
|
|
|
30
|
-
/** Whether devproxy is installed and has a config to serve, so wt routes through it. */
|
|
31
|
-
export
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
return argv.length > 0 && (await $`${argv} hosts --json`.quiet().nothrow()).exitCode === 0
|
|
32
|
+
/** Whether devproxy is installed and has a config to serve, so wt routes through it: its config file looked up as devproxy does, so `up` spawns it once. */
|
|
33
|
+
export function hasDevproxy(): boolean {
|
|
34
|
+
return devproxyArgv().length > 0 && existsSync(process.env.DEVPROXY_CONFIG ?? join(configBase(), 'devproxy', 'config.ts'))
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
/** Every host the proxy serves; empty without devproxy or its config. */
|
package/src/prune.ts
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
/** What worktrees removed by hand, or an add that crashed, left behind: wt's own files, job records, queues, stacks and volumes no worktree folder answers to. */
|
|
2
|
+
|
|
3
|
+
import type { GlobalOptions } from './commands'
|
|
4
|
+
import type { Project, Worktree } from './project'
|
|
5
|
+
import { existsSync, readdirSync, rmSync } from 'node:fs'
|
|
6
|
+
import { join } from 'node:path'
|
|
7
|
+
import { $ } from 'bun'
|
|
8
|
+
import { wtConfig } from './config'
|
|
9
|
+
import { git } from './git'
|
|
10
|
+
import { currentDir, listTasks, loadProjects, locate, stackName, worktreeOf } from './project'
|
|
11
|
+
|
|
12
|
+
export interface Orphan {
|
|
13
|
+
worktree: Worktree
|
|
14
|
+
/** What is left of it, in a few words each: "compose override", "2 volumes". */
|
|
15
|
+
left: string[]
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface LeftBehind {
|
|
19
|
+
project: Project
|
|
20
|
+
orphans: Orphan[]
|
|
21
|
+
/** Worktree registrations git keeps for folders gone. */
|
|
22
|
+
stale: number
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** What each project, the current one or every one with `all`, has left behind; only the projects with something left. */
|
|
26
|
+
export async function leftBehind(options: GlobalOptions & { all?: boolean }): Promise<LeftBehind[]> {
|
|
27
|
+
const projects = options.all ? await loadProjects() : [(await locate(currentDir(), options.project)).project]
|
|
28
|
+
const { jobs } = await wtConfig()
|
|
29
|
+
const found = await Promise.all(projects.map(async project => ({ project, orphans: await orphansOf(project, jobs), stale: await staleRegistrations(project) })))
|
|
30
|
+
|
|
31
|
+
return found.filter(each => each.orphans.length > 0 || each.stale > 0)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** One line per worktree left behind, saying what is left, then git's stale registrations. */
|
|
35
|
+
export function describeLeftBehind(found: LeftBehind[]): string[] {
|
|
36
|
+
return found.flatMap(({ project, orphans, stale }) => [
|
|
37
|
+
...orphans.map(({ worktree, left }) => `${project.name}/${worktree.task}: ${left.join(', ')}`),
|
|
38
|
+
...stale > 0 ? [`${project.name}: ${stale} git worktree registration${stale === 1 ? '' : 's'} for a folder gone`] : [],
|
|
39
|
+
])
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const EXTENSION = /\.[^.]*$/
|
|
43
|
+
|
|
44
|
+
// What a file of wt's state folder is, by what follows `<id>` in its name.
|
|
45
|
+
const STATE_KINDS: [suffix: RegExp, what: string][] = [
|
|
46
|
+
[/^\.setup$/, 'setup marker'],
|
|
47
|
+
[/^\.clones-pending$/, 'pending clones'],
|
|
48
|
+
[/^\.test-/, 'test database marker'],
|
|
49
|
+
]
|
|
50
|
+
|
|
51
|
+
function namesIn(dir: string): string[] {
|
|
52
|
+
return existsSync(dir) ? readdirSync(dir) : []
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** The task a name of the project's state starts with, `<prefix><task>` then a dot or, for a volume, an underscore. */
|
|
56
|
+
function taskOf(name: string, prefix: string, end: string): string | undefined {
|
|
57
|
+
if (!name.startsWith(prefix))
|
|
58
|
+
return undefined
|
|
59
|
+
|
|
60
|
+
const rest = name.slice(prefix.length)
|
|
61
|
+
const stop = rest.indexOf(end)
|
|
62
|
+
|
|
63
|
+
return stop > 0 ? rest.slice(0, stop) : undefined
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Bun's shell throws on a missing program even with nothrow, and without docker there is nothing of a stack left to list.
|
|
67
|
+
async function dockerLines(argv: string[]): Promise<string[]> {
|
|
68
|
+
if (Bun.which('docker') === null)
|
|
69
|
+
return []
|
|
70
|
+
|
|
71
|
+
const listed = await $`docker ${argv}`.quiet().nothrow()
|
|
72
|
+
|
|
73
|
+
return listed.exitCode === 0 ? listed.text().split('\n').filter(Boolean) : []
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The worktrees of a project that state still names while no folder holds them, each with what is left of it. */
|
|
77
|
+
async function orphansOf(project: Project, jobsDir: string): Promise<Orphan[]> {
|
|
78
|
+
const live = new Set(listTasks(project))
|
|
79
|
+
const stack = `${stackName(project)}--`
|
|
80
|
+
const left = new Map<string, string[]>()
|
|
81
|
+
const note = (task: string | undefined, what: string | undefined) => {
|
|
82
|
+
if (task !== undefined && what !== undefined && !live.has(task))
|
|
83
|
+
left.set(task, [...left.get(task) ?? [], what])
|
|
84
|
+
}
|
|
85
|
+
const { overrides, state } = project.settings
|
|
86
|
+
|
|
87
|
+
for (const name of namesIn(overrides))
|
|
88
|
+
note(taskOf(name, stack, '.override.yml'), 'compose override')
|
|
89
|
+
|
|
90
|
+
for (const name of namesIn(state)) {
|
|
91
|
+
const task = taskOf(name, stack, '.')
|
|
92
|
+
|
|
93
|
+
note(task, STATE_KINDS.find(([suffix]) => suffix.test(name.slice(stack.length + (task?.length ?? 0))))?.[1])
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
for (const name of namesIn(join(state, 'queues')))
|
|
97
|
+
note(taskOf(name, stack, '.'), 'queue')
|
|
98
|
+
|
|
99
|
+
// A job's json, log and lock are one record
|
|
100
|
+
for (const name of new Set(namesIn(jobsDir).map(name => name.replace(EXTENSION, ''))))
|
|
101
|
+
note(taskOf(`${name}.`, `${project.name}--`, '.'), 'job record')
|
|
102
|
+
|
|
103
|
+
for (const volume of await dockerLines(['volume', 'ls', '-q']))
|
|
104
|
+
note(taskOf(volume, stack, '_'), 'volume')
|
|
105
|
+
|
|
106
|
+
for (const label of await dockerLines(['ps', '-a', '--format', '{{.Label "com.docker.compose.project"}}']))
|
|
107
|
+
note(label.startsWith(stack) ? label.slice(stack.length) : undefined, 'container')
|
|
108
|
+
|
|
109
|
+
const orphans: Orphan[] = []
|
|
110
|
+
|
|
111
|
+
for (const [task, what] of [...left].sort(([a], [b]) => a.localeCompare(b))) {
|
|
112
|
+
try {
|
|
113
|
+
orphans.push({ worktree: worktreeOf(project, task), left: counted(what) })
|
|
114
|
+
}
|
|
115
|
+
// Not a name wt could have made, so not one of its worktrees
|
|
116
|
+
catch {}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return orphans
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Each kind once in the order first seen, a repeated one counted: "2 volumes". */
|
|
123
|
+
function counted(what: string[]): string[] {
|
|
124
|
+
const counts = new Map<string, number>()
|
|
125
|
+
|
|
126
|
+
for (const each of what)
|
|
127
|
+
counts.set(each, (counts.get(each) ?? 0) + 1)
|
|
128
|
+
|
|
129
|
+
return [...counts].map(([each, count]) => count === 1 ? each : `${count} ${each}s`)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** The worktree registrations git keeps for folders gone, which `git worktree prune` drops, over the root and its repos. */
|
|
133
|
+
async function staleRegistrations(project: Project): Promise<number> {
|
|
134
|
+
const runs = await Promise.all(['.', ...project.repos].map(async repo => git(join(project.root, repo), 'worktree', 'prune', '--dry-run', '--verbose')))
|
|
135
|
+
|
|
136
|
+
return runs.filter(run => run.ok).flatMap(run => run.out.split('\n').filter(Boolean)).length
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export async function pruneRegistrations(project: Project): Promise<void> {
|
|
140
|
+
await Promise.all(['.', ...project.repos].map(async repo => git(join(project.root, repo), 'worktree', 'prune')))
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** The queues a worktree's runs lined up in. */
|
|
144
|
+
export function removeQueues(worktree: Worktree): void {
|
|
145
|
+
const queues = join(worktree.project.settings.state, 'queues')
|
|
146
|
+
|
|
147
|
+
for (const name of namesIn(queues).filter(each => each.startsWith(`${worktree.id}.`)))
|
|
148
|
+
rmSync(join(queues, name), { recursive: true, force: true })
|
|
149
|
+
}
|
package/src/queue.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Resolved } from './config'
|
|
2
|
-
import { mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
|
|
2
|
+
import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
|
|
3
3
|
import { join } from 'node:path'
|
|
4
4
|
import { alive, bootTime } from './errors'
|
|
5
5
|
|
|
@@ -33,8 +33,8 @@ function labelOf(ticket: string): string {
|
|
|
33
33
|
}
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
//
|
|
37
|
-
|
|
36
|
+
// A ticket named by when it was taken and by whom, released when the process exits at the latest.
|
|
37
|
+
function takeTicket(dir: string, label: string): { name: string, release: () => void } {
|
|
38
38
|
mkdirSync(dir, { recursive: true })
|
|
39
39
|
|
|
40
40
|
const name = `${String(Date.now()).padStart(15, '0')}-${process.pid}`
|
|
@@ -44,6 +44,30 @@ export async function inLine<T>(dir: string, label: string, waiting: (ahead: num
|
|
|
44
44
|
writeFileSync(ticket, label)
|
|
45
45
|
process.once('exit', release)
|
|
46
46
|
|
|
47
|
+
return { name, release }
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** What the live tickets of `dir` run, oldest first: none when the folder is missing. */
|
|
51
|
+
export function holders(dir: string): string[] {
|
|
52
|
+
return existsSync(dir) ? liveTickets(dir).map(ticket => labelOf(join(dir, ticket))) : []
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Runs `work` holding a ticket in `dir`, without waiting on anyone, so `holders` tells another process it is in use. */
|
|
56
|
+
export async function holding<T>(dir: string, label: string, work: () => Promise<T>): Promise<T> {
|
|
57
|
+
const { release } = takeTicket(dir, label)
|
|
58
|
+
|
|
59
|
+
try {
|
|
60
|
+
return await work()
|
|
61
|
+
}
|
|
62
|
+
finally {
|
|
63
|
+
release()
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Runs `work` once every ticket taken before this one in `dir`, by any process on the machine, is released; `waiting` hears how many are ahead each time that changes, and what the first of them runs.
|
|
68
|
+
export async function inLine<T>(dir: string, label: string, waiting: (ahead: number, running: string) => void, work: () => Promise<T>): Promise<T> {
|
|
69
|
+
const { name, release } = takeTicket(dir, label)
|
|
70
|
+
|
|
47
71
|
try {
|
|
48
72
|
let shown = -1
|
|
49
73
|
|