@kaynooo/cli-wt 0.1.6 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/README.md +17 -10
  2. package/dist/cli-02xxc9xk.js +67 -0
  3. package/dist/cli-5khwxy5g.js +36 -0
  4. package/dist/cli-6k7xzgw2.js +519 -0
  5. package/dist/cli-96wy1q8e.js +97 -0
  6. package/dist/cli-9m1jyyrf.js +69 -0
  7. package/dist/cli-fa4fh6ex.js +37 -0
  8. package/dist/cli-fdwwsds1.js +153 -0
  9. package/dist/cli-jb9a8d46.js +674 -0
  10. package/dist/cli-k4471y24.js +779 -0
  11. package/dist/cli-kt6d30zz.js +106 -0
  12. package/dist/cli-q7e6gegw.js +105 -0
  13. package/dist/cli-qwe5ykfs.js +313 -0
  14. package/dist/cli-tp4q5w5x.js +251 -0
  15. package/dist/cli.js +228 -2809
  16. package/dist/commands-jqr92tk6.js +222 -0
  17. package/dist/init-0ws4c883.js +166 -0
  18. package/dist/list-83vq4jng.js +88 -0
  19. package/dist/print-d4aq2eqw.js +77 -0
  20. package/dist/prompt-y71tp3f5.js +436 -0
  21. package/dist/prune-7ex1qyd3.js +17 -0
  22. package/dist/run-jrmm9gg8.js +18 -0
  23. package/dist/shell-1smv2vf9.js +55 -0
  24. package/dist/stack-2zj6r6py.js +20 -0
  25. package/dist/tools-c4m5037s.js +13 -0
  26. package/package.json +4 -4
  27. package/src/cli.ts +250 -69
  28. package/src/commands.ts +51 -38
  29. package/src/compose.ts +37 -14
  30. package/src/config.ts +8 -1
  31. package/src/errors.ts +16 -6
  32. package/src/files.ts +14 -15
  33. package/src/git.ts +53 -7
  34. package/src/index.ts +2 -1
  35. package/src/init.ts +56 -10
  36. package/src/jobs.ts +54 -15
  37. package/src/list.ts +26 -13
  38. package/src/print.ts +76 -0
  39. package/src/project.ts +129 -28
  40. package/src/proxy.ts +5 -5
  41. package/src/prune.ts +149 -0
  42. package/src/queue.ts +27 -3
  43. package/src/run.ts +82 -23
  44. package/src/shell.ts +34 -7
  45. package/src/stack.ts +10 -12
  46. package/src/sync.ts +28 -3
  47. package/src/tools.ts +91 -0
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: string[]
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: Route[]
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,44 +138,80 @@ function assertValid(project: Project): void {
102
138
  }
103
139
  }
104
140
 
105
- export async function loadProjects(): Promise<Project[]> {
106
- const settings = await wtConfig()
107
- const files = existsSync(settings.manifests) ? readdirSync(settings.manifests).filter(file => file.endsWith('.ts')).sort() : []
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
108
145
 
109
- const projects = await Promise.all(files.map(async (file) => {
110
- const { default: config } = await import(join(settings.manifests, file)) as { default: ProjectConfig }
111
- const project = { ...config, root: expandHome(config.root), name: basename(file, '.ts'), settings }
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
+ }
112
152
 
113
- assertValid(project)
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')
114
155
 
115
- return project
116
- }))
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)
117
161
 
118
162
  // A worktree's stack is named after its checkout's folder, kept so that running stacks keep their containers and volumes.
119
- for (const [index, project] of projects.entries()) {
120
- const twin = projects.slice(0, index).find(other => basename(other.root) === basename(project.root))
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[]> {
171
+ const settings = await wtConfig()
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)}`)
179
+
180
+ continue
181
+ }
182
+
183
+ const project = result.value
184
+ const twin = projects.find(other => stackName(other) === stackName(project))
121
185
 
122
186
  if (twin)
123
- throw new WtError(`${twin.name}.ts and ${project.name}.ts both have a checkout named ${basename(project.root)}, so their worktrees would share stacks and volumes: rename the folder of a checkout that has no worktrees yet, or remove one of the files`)
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)
124
190
  }
125
191
 
126
192
  return projects
127
193
  }
128
194
 
195
+ /** The project of one manifest, the others left unread: its failure to load ends the command. */
129
196
  export async function findProject(name: string): Promise<Project> {
130
- const project = (await loadProjects()).find(p => p.name === name)
197
+ const settings = await wtConfig()
131
198
 
132
- if (!project)
133
- throw new WtError(`no project named ${name} in ${(await wtConfig()).manifests}`)
199
+ if (!existsSync(join(settings.manifests, `${name}.ts`)))
200
+ throw new WtError(`no project named ${name} in ${settings.manifests}`)
134
201
 
135
- return project
202
+ return loadManifest(settings, `${name}.ts`)
136
203
  }
137
204
 
138
205
  export const tasksDir = (project: Project) => fillTemplate([project.settings.worktrees], { root: dirname(project.root), repo: basename(project.root) })[0]!
139
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
+
140
212
  export function worktreeOf(project: Project, task: string): Worktree {
141
213
  if (task === 'main' || !TASK_PATTERN.test(task))
142
- 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"`)
143
215
 
144
216
  const mainHosts = new Set(project.routes.map(route => route.host))
145
217
  const taken = project.routes.find(route => mainHosts.has(worktreeHost(route.host, task)))
@@ -147,21 +219,21 @@ export function worktreeOf(project: Project, task: string): Worktree {
147
219
  if (taken)
148
220
  throw new WtError(`invalid task name "${task}": the worktree's ${worktreeHost(taken.host, task)} would be one of main's hosts`)
149
221
 
150
- const id = `${basename(project.root)}--${task}`
222
+ const id = `${stackName(project)}--${task}`
151
223
 
152
224
  return {
153
225
  project,
154
226
  task,
155
227
  dir: join(tasksDir(project), task),
156
228
  id,
157
- clientPort: stringToPort(`${project.name}--${task}`),
229
+ clientPort: project.ports?.[task] ?? stringToPort(`${project.name}--${task}`),
158
230
  files: worktreeFiles(project.settings, id),
159
231
  }
160
232
  }
161
233
 
162
234
  export function assertWorktreeExists(worktree: Worktree): void {
163
235
  if (!existsSync(worktree.dir))
164
- throw new WtError(`no worktree ${worktree.task}, create it with \`wt add ${worktree.task}\``)
236
+ throw new WtError(`no worktree ${worktree.task}`, `create it with \`wt add ${worktree.task}\``)
165
237
  }
166
238
 
167
239
  export function listTasks(project: Project): string[] {
@@ -170,13 +242,39 @@ export function listTasks(project: Project): string[] {
170
242
  return existsSync(dir) ? readdirSync(dir).filter(task => TASK_PATTERN.test(task)).sort() : []
171
243
  }
172
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
+
173
270
  // The project and task the working directory sits in, `main` when it is the project's own checkout.
174
271
  export async function locate(cwd: string, projectName?: string): Promise<{ project: Project, task: string }> {
175
- const here = realpathSync(cwd)
272
+ const here = resolved(cwd)
176
273
  const projects = projectName !== undefined ? [await findProject(projectName)] : await loadProjects()
177
274
 
178
275
  for (const project of projects) {
179
- const dir = tasksDir(project)
276
+ const dir = resolved(tasksDir(project))
277
+ const root = resolved(project.root)
180
278
 
181
279
  if (here.startsWith(`${dir}/`)) {
182
280
  const rest = here.slice(dir.length + 1)
@@ -185,13 +283,16 @@ export async function locate(cwd: string, projectName?: string): Promise<{ proje
185
283
  return { project, task: slash === -1 ? rest : rest.slice(0, slash) }
186
284
  }
187
285
 
188
- if (here === project.root || here.startsWith(`${project.root}/`))
286
+ if (here === root || here.startsWith(`${root}/`))
189
287
  return { project, task: 'main' }
190
288
  }
191
289
 
192
290
  if (projects.length === 1)
193
291
  return { project: projects[0]!, task: 'main' }
194
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
+
195
296
  throw new WtError('not inside a known project or worktree (cd into one, or pass --project)')
196
297
  }
197
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
 
@@ -27,11 +29,9 @@ export function devproxyArgv(path = process.env.PATH ?? ''): string[] {
27
29
  }
28
30
  }
29
31
 
30
- /** Whether devproxy is installed and has a config to serve, so wt routes through it. */
31
- export async function hasDevproxy(): Promise<boolean> {
32
- const argv = devproxyArgv()
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
- // 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.
37
- export async function inLine<T>(dir: string, label: string, waiting: (ahead: number, running: string) => void, work: () => Promise<T>): Promise<T> {
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