@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.
Files changed (47) hide show
  1. package/README.md +17 -10
  2. package/dist/cli-0gbs87r4.js +313 -0
  3. package/dist/cli-1nyesy1b.js +674 -0
  4. package/dist/cli-4g5ehhgn.js +251 -0
  5. package/dist/cli-6ybxw5sc.js +97 -0
  6. package/dist/cli-dmkfxdbr.js +153 -0
  7. package/dist/cli-dmzvmpge.js +106 -0
  8. package/dist/cli-e2h20nyy.js +36 -0
  9. package/dist/cli-j82fak6j.js +69 -0
  10. package/dist/cli-k4471y24.js +779 -0
  11. package/dist/cli-kek450vq.js +519 -0
  12. package/dist/cli-n5rt07v7.js +105 -0
  13. package/dist/cli-pkvw2k4x.js +37 -0
  14. package/dist/cli-w4w36hng.js +67 -0
  15. package/dist/cli.js +238 -2592
  16. package/dist/commands-jtnbchtb.js +222 -0
  17. package/dist/init-4ynjrztr.js +166 -0
  18. package/dist/list-js6drtvg.js +88 -0
  19. package/dist/print-q6qaqg22.js +77 -0
  20. package/dist/prompt-y71tp3f5.js +436 -0
  21. package/dist/prune-beweag7w.js +17 -0
  22. package/dist/run-7eayrqwy.js +18 -0
  23. package/dist/shell-xrgvqrpp.js +55 -0
  24. package/dist/stack-h3pgyx1e.js +20 -0
  25. package/dist/tools-4j56r47a.js +13 -0
  26. package/package.json +6 -6
  27. package/src/cli.ts +270 -76
  28. package/src/commands.ts +58 -42
  29. package/src/compose.ts +37 -14
  30. package/src/config.ts +8 -1
  31. package/src/errors.ts +31 -5
  32. package/src/files.ts +15 -23
  33. package/src/git.ts +53 -7
  34. package/src/index.ts +3 -2
  35. package/src/init.ts +56 -10
  36. package/src/jobs.ts +60 -19
  37. package/src/list.ts +26 -13
  38. package/src/print.ts +76 -0
  39. package/src/project.ts +134 -23
  40. package/src/proxy.ts +9 -9
  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/cli.ts CHANGED
@@ -1,41 +1,58 @@
1
1
  #!/usr/bin/env -S bun --no-env-file --config=/dev/null
2
2
 
3
3
  import type { GlobalOptions } from './commands'
4
+ import type { Jobs } from './jobs'
5
+ import type { Worktree } from './project'
4
6
  import process from 'node:process'
5
- import { badUsage, parse, runCli } from '@kaynooo/cli-kit'
6
- import { addTask, assertRemovable, downTask, printPath, printTasks, printUrls, removeTask, syncTask, upTask } from './commands'
7
- import { wtConfig } from './config'
8
- import { failKnown } from './errors'
9
- import { initProject } from './init'
10
- import { Jobs } from './jobs'
11
- import { listWorktrees } from './list'
12
- import { listTasks, loadProjects, locate, locateWorktree } from './project'
13
- import { runHeavy, runTask } from './run'
14
- import { shellWrapper } from './shell'
15
- import { slugOf } from './stack'
16
-
17
- async function jobs(): Promise<Jobs> {
18
- return new Jobs({ dir: (await wtConfig()).jobs, app: 'wt' })
19
- }
7
+ import { badUsage, fail, isInteractive, jsonOption, parse, printError, runCli, warn } from '@kaynooo/cli-kit'
8
+ import { wtJobs } from './config'
9
+ import { failKnown, failureText, knownHint, knownMessage } from './errors'
10
+ import { canNotify, notifyRefused, slugOf } from './jobs'
11
+ import { assertWorktreeExists, currentDir, listTasks, loadProjects, locate, locateWorktree, worktreeOf } from './project'
12
+
13
+ // Each command loads its modules when it runs, so `wt path`, `wt tasks` and Tab completion do not pay for the whole tool.
14
+ const commands = async () => import('./commands')
15
+ const printing = async () => import('./print')
16
+
17
+ const APP = 'wt'
18
+
19
+ const jobs = wtJobs
20
+
21
+ // A config that does not load fails like any other known error; `bg` names a `--bg` command, whose refusal is notified too.
22
+ async function guarded(action: () => Promise<number | void>, bg?: string): Promise<number> {
23
+ let built: Jobs
20
24
 
21
- // A config that does not load fails like any other known error.
22
- async function guarded(action: () => Promise<number | void>): Promise<number> {
23
25
  try {
24
- return await (await jobs()).guarded(action)
26
+ built = await jobs()
25
27
  }
26
28
  catch (error) {
29
+ if (bg !== undefined)
30
+ await notifyRefused(APP, bg, failureText(error))
31
+
27
32
  return failKnown(error)
28
33
  }
34
+
35
+ return built.guarded(action, bg)
36
+ }
37
+
38
+ // The name a `--bg` command is notified under, none without `--bg`.
39
+ function bgName(values: { bg?: boolean }, ...words: (string | undefined)[]): string | undefined {
40
+ return values.bg ? [APP, ...words.filter(word => word !== undefined)].join(' ') : undefined
29
41
  }
30
42
 
31
43
  async function projectNames(): Promise<string[]> {
32
- return (await loadProjects()).map(project => project.name)
44
+ try {
45
+ return (await loadProjects(() => undefined)).map(project => project.name)
46
+ }
47
+ catch {
48
+ return []
49
+ }
33
50
  }
34
51
 
35
52
  // The worktrees of the project the current directory belongs to, none outside one.
36
53
  async function taskNames(): Promise<string[]> {
37
54
  try {
38
- return listTasks((await locate(process.cwd())).project)
55
+ return listTasks((await locate(currentDir())).project)
39
56
  }
40
57
  catch {
41
58
  return []
@@ -49,9 +66,8 @@ const PROJECT_OPTION = { type: 'string', short: 'p', candidates: projectNames, d
49
66
 
50
67
  const BG_OPTION = { type: 'boolean', description: 'run it away from any terminal, its output in the worktree\'s job log, a notification saying how it went' } as const
51
68
 
52
- // Every action on a worktree holds its stack's lock, so an up cannot race a removal, and --bg detaches it.
53
- async function asWorktreeJob(verb: 'add' | 'sync' | 'rm' | 'up' | 'down', task: string, options: GlobalOptions & { bg?: boolean }, argv: string[], work: () => Promise<void>): Promise<number> {
54
- const { project } = await locate(process.cwd(), options.project)
69
+ // Every action on a worktree holds its stack's lock, so an up cannot race a removal, and --bg detaches it. Its caller checks the worktree first: a job claimed for one that cannot be acted on leaves a record behind, or a `--bg` one failing out of sight.
70
+ async function asWorktreeJob(verb: 'add' | 'add --up' | 'sync' | 'rm' | 'up' | 'down', { project, task }: Worktree, options: GlobalOptions & { bg?: boolean }, argv: string[], work: () => Promise<void>): Promise<number> {
55
71
  const ref = `${project.name}/${task}`
56
72
  const stack = { ref, slug: slugOf(ref) }
57
73
  const queue = await jobs()
@@ -66,25 +82,87 @@ async function asWorktreeJob(verb: 'add' | 'sync' | 'rm' | 'up' | 'down', task:
66
82
  return code
67
83
  }
68
84
 
69
- try {
70
- const job = queue.detach(stack, verb, [import.meta.path, ...argv, '-p', project.name])
85
+ const job = queue.detach(stack, verb, [import.meta.path, ...argv, '-p', project.name])
86
+
87
+ console.log(`wt: ${verb} ${ref} runs out of sight, its output in ${job.log}${canNotify() ? '' : `; nothing here notifies its end, \`wt ls\` shows how it went`}`)
88
+
89
+ return 0
90
+ }
91
+
92
+ // Acts on each worktree in turn, one refused or failed not stopping the rest: 1 when any failed.
93
+ async function inTurn(command: string, worktrees: Worktree[], act: (worktree: Worktree) => Promise<number>): Promise<number> {
94
+ let code = 0
71
95
 
72
- console.log(`wt: ${verb} ${ref} runs out of sight, its output in ${job.log}`)
96
+ for (const worktree of worktrees) {
97
+ try {
98
+ code = Math.max(code, await act(worktree))
99
+ }
100
+ catch (error) {
101
+ const message = knownMessage(error)
102
+
103
+ if (message === undefined)
104
+ throw error
105
+
106
+ printError(`${APP} ${command}`, message, knownHint(error))
107
+ code = 1
108
+ }
73
109
  }
74
- catch (error) {
75
- await queue.notifyRefused(`wt ${verb} ${ref}`, error instanceof Error ? error.message : String(error))
76
110
 
77
- throw error
111
+ return code
112
+ }
113
+
114
+ async function eachWorktree(command: string, projectName: string | undefined, act: (worktree: Worktree) => Promise<number>): Promise<number> {
115
+ const { project } = await locate(currentDir(), projectName)
116
+ const tasks = listTasks(project)
117
+
118
+ if (!tasks.length)
119
+ console.log(`wt: ${project.name} has no worktree`)
120
+
121
+ return inTurn(command, tasks.map(task => worktreeOf(project, task)), act)
122
+ }
123
+
124
+ // Lists what removed worktrees left behind and, once confirmed, removes each as `wt rm --force` would, then git's stale registrations.
125
+ async function prune(options: GlobalOptions & { all?: boolean, yes?: boolean }): Promise<number> {
126
+ const { describeLeftBehind, leftBehind, pruneRegistrations } = await import('./prune')
127
+ const found = await leftBehind(options)
128
+
129
+ if (!found.length) {
130
+ console.log('wt: nothing left behind')
131
+
132
+ return 0
78
133
  }
79
134
 
80
- return 0
135
+ console.log(describeLeftBehind(found).join('\n'))
136
+
137
+ if (!options.yes) {
138
+ if (!isInteractive()) {
139
+ warn('nothing removed, `wt prune -y` removes it')
140
+
141
+ return 0
142
+ }
143
+
144
+ const { confirm } = await import('@kaynooo/cli-kit/prompt')
145
+
146
+ if (await confirm('Remove it all?', { default: false }) !== true)
147
+ return 1
148
+ }
149
+
150
+ const { removeTask } = await commands()
151
+ const code = await inTurn('prune', found.flatMap(each => each.orphans.map(orphan => orphan.worktree)), async worktree => asWorktreeJob('rm', worktree, { project: worktree.project.name }, [], async () => removeTask(worktree.task, { project: worktree.project.name, force: true })))
152
+
153
+ for (const { project } of found)
154
+ await pruneRegistrations(project)
155
+
156
+ return code
81
157
  }
82
158
 
83
- const ADD_USAGE = `wt add <task> [branch] [-p <name>] [--bg]
159
+ const ALL_OPTION = { type: 'boolean', description: 'every worktree of the project, one after the other' } as const
160
+
161
+ const ADD_USAGE = `wt add <task> [branch] [--from <ref>] [--up] [-p <name>] [--bg]
84
162
 
85
- Creates a worktree, copies its files and data from main, and routes its subdomains. The task is a lowercase name, also the subdomain label; the branch defaults to wt/<task>.
163
+ Creates a worktree, copies its files and data from main, and routes its subdomains; --up starts its stack too. The task is a lowercase name, also the subdomain label; the branch defaults to wt/<task>. A branch that does not exist yet forks from main's current commit, or from --from, and the output says which.
86
164
 
87
- Exits 0 once the worktree is ready, 1 when it cannot be made or provisioned, 2 on a usage error.`
165
+ Exits 0 once the worktree is ready (and up), 1 when it cannot be made, provisioned or started, 2 on a usage error.`
88
166
 
89
167
  const SYNC_USAGE = `wt sync [task] [-p <name>]
90
168
 
@@ -94,27 +172,66 @@ Exits 0 once it is provisioned, 1 when a step fails, 2 on a usage error.`
94
172
 
95
173
  const RM_USAGE = `wt rm <task> [-f] [-p <name>] [--bg]
96
174
 
97
- Stops a worktree stack and deletes its checkouts, volumes, certificates and routes, keeping its branches.
175
+ Stops a worktree stack and deletes its checkouts, volumes, certificates and routes, keeping its branches. Gitignored files go with the folder unchecked: edits to a copied .env.local, uploads and the like are lost, so save them first.
98
176
 
99
177
  Exits 0 once it is gone, 1 when uncommitted changes or lost commits refuse it (unless --force) or a step fails, 2 on a usage error.`
100
178
 
101
- const UP_USAGE = `wt up [task] [-p <name>] [--bg]
179
+ const UP_USAGE = `wt up [task | --all] [-p <name>] [--bg]
180
+
181
+ Starts a worktree stack: \`docker compose up -d --build\` in the worktree then the manifest's setup the first time, or the config's runner.up. The task defaults to the worktree the current directory is in; --all starts every worktree of the project in turn.
182
+
183
+ Exits 0 once the stacks are up, 1 when a command fails, 2 on a usage error.`
184
+
185
+ const DOWN_USAGE = `wt down [task | --all] [-p <name>] [--bg]
186
+
187
+ Stops a worktree stack: \`docker compose down\` in the worktree, or the config's runner.down. A stack whose folder is gone is stopped by its compose project name, runner.down skipped. The task defaults to the worktree the current directory is in; --all stops every worktree of the project in turn.
188
+
189
+ Exits 0 once the stacks are down, 1 when a command fails, 2 on a usage error.`
190
+
191
+ const LOGS_USAGE = `wt logs [task] [-f] [-p <name>]
192
+
193
+ Prints the log of the worktree's last --bg job, a job run in a terminal having printed there; -f follows its stack's compose logs instead, from their last 100 lines. The task defaults to the worktree the current directory is in.
194
+
195
+ Exits 0 once printed, 1 when no --bg job left a log, compose's code with -f, 2 on a usage error.`
196
+
197
+ const PRUNE_USAGE = `wt prune [-a] [-y] [-p <name>]
198
+
199
+ Lists what worktrees removed by hand, or an add that crashed, left behind: wt's files, job records, queues, containers and volumes, and git's registrations of folders gone. Once confirmed, removes each as \`wt rm --force\` would. The current project, or every project with -a.
102
200
 
103
- Starts a worktree stack: \`docker compose up -d --build\` in the worktree then the manifest's setup the first time, or the config's runner.up. The task defaults to the worktree the current directory is in.
201
+ Exits 0 once removed, or when nothing is left or nothing could be asked, 1 when a removal fails or is declined, 2 on a usage error.`
104
202
 
105
- Exits 0 once the stack is up, 1 when a command fails, 2 on a usage error.`
203
+ // `wt up` and `wt down`, of one worktree or with --all of each in turn.
204
+ async function startOrStop(verb: 'up' | 'down', args: string[]): Promise<number> {
205
+ const { values, positionals: [task] } = parse(args, verb === 'up' ? UP_USAGE : DOWN_USAGE, { project: PROJECT_OPTION, all: ALL_OPTION, bg: BG_OPTION }, { positionals: ['[task]'], candidates: TASK })
206
+ const act = async (worktree: Worktree) => asWorktreeJob(verb, worktree, values, [verb, worktree.task], async () => {
207
+ const { downTask, upTask } = await commands()
106
208
 
107
- const DOWN_USAGE = `wt down [task] [-p <name>] [--bg]
209
+ return verb === 'up' ? upTask(worktree) : downTask(worktree)
210
+ })
108
211
 
109
- Stops a worktree stack: \`docker compose down\` in the worktree, or the config's runner.down. The task defaults to the worktree the current directory is in.
212
+ if (values.all && task !== undefined)
213
+ badUsage(`wt ${verb} takes a task or --all, not both`)
110
214
 
111
- Exits 0 once the stack is down, 1 when a command fails, 2 on a usage error.`
215
+ return guarded(async () => {
216
+ if (values.all)
217
+ return eachWorktree(verb, values.project, act)
218
+
219
+ const worktree = await locateWorktree(currentDir(), task, values.project)
220
+
221
+ if (verb === 'up')
222
+ assertWorktreeExists(worktree)
223
+ else
224
+ await (await commands()).assertStoppable(worktree)
225
+
226
+ return act(worktree)
227
+ }, bgName(values, verb, values.all ? '--all' : task))
228
+ }
112
229
 
113
230
  const SHELL_USAGE = `wt shell <shell>
114
231
 
115
- Prints the shell function that lets \`wt cd\` change directory and \`wt rm\` leave the folder it removes: \`wt shell fish | source\` in config.fish.
232
+ Prints the shell function that lets \`wt cd\` change directory and \`wt rm\` leave the folder it removes, with wt's completion. Save it where the shell reads it at start, so no process runs then, and again after upgrading wt: \`wt shell fish > ~/.config/fish/conf.d/wt.fish\`; for bash or zsh, a file sourced from ~/.bashrc, or from ~/.zshrc after compinit.
116
233
 
117
- Exits 0 when it printed the wrapper, 1 for a shell it has none for, 2 on a usage error.`
234
+ Exits 0 when it printed the wrapper, 1 for a shell it has none for (fish, bash and zsh have one), 2 on a usage error.`
118
235
 
119
236
  const LS_USAGE = `wt ls [-a] [-p <name>] [--json]
120
237
 
@@ -134,6 +251,12 @@ Drafts a manifest for the project checked out in a directory, the current one by
134
251
 
135
252
  Exits 0 once it is written or printed, 1 when the folder holds no compose project, 2 on a usage error.`
136
253
 
254
+ const DOCTOR_USAGE = `wt doctor
255
+
256
+ Lists each program wt runs: where it is on PATH, or how to get it, and what wt needs it for.
257
+
258
+ Exits 0 when every program the projects need is there, 1 when one is missing, 2 on a usage error.`
259
+
137
260
  const RUN_USAGE = `wt run <task> [-p <name>] -- <command>...
138
261
 
139
262
  Runs a command of a worktree in a throwaway container of its run service, against a test database of its own, provisioned on first use.
@@ -146,6 +269,18 @@ Runs a heavy command (a full suite, e2e) once those queued before it, from any s
146
269
 
147
270
  Exits with the command's code, 2 on a usage error.`
148
271
 
272
+ const OPEN_USAGE = `wt open [task] [-p <name>]
273
+
274
+ Opens the worktree's main URL in the browser ($BROWSER, else the system's opener). The task defaults to the checkout the current directory is in, \`main\` naming main's.
275
+
276
+ Exits 0 once the browser is started, 1 when there is no such worktree, no URL or no browser, 2 on a usage error.`
277
+
278
+ const CD_USAGE = `wt cd [task] [-p <name>]
279
+
280
+ Goes to a worktree's directory, or main's when no task is given. A program cannot change its shell's directory, so this runs in the shell function \`wt shell\` prints; run as a program, it says so.
281
+
282
+ Exits 0 once there, 1 when there is no such worktree or the shell function is missing, 2 on a usage error.`
283
+
149
284
  const URL_USAGE = `wt url [task] [-p <name>]
150
285
 
151
286
  Prints the URLs of a worktree.
@@ -154,12 +289,12 @@ Exits 0 when it printed it, 1 when there is no such worktree or project, 2 on a
154
289
 
155
290
  const TASKS_USAGE = `wt tasks [-p <name>]
156
291
 
157
- Prints the worktree names of the project, one per line.
292
+ Prints the worktree names of the project, one per line, nothing when it has none.
158
293
 
159
- Exits 0 when it printed them, 1 when there is no such project, 2 on a usage error.`
294
+ Exits 0 when it printed them, 1 when there is no such project or its manifest does not load, 2 on a usage error.`
160
295
 
161
296
  process.exitCode = await runCli({
162
- name: 'wt',
297
+ name: APP,
163
298
  about: 'git worktrees of a compose project, each with its own stack, data and <task>.<host> subdomains beside main',
164
299
  replaces: 'a second clone, port juggling and a shared database to try a branch',
165
300
  examples: [['wt add planning', 'a worktree on wt/planning with its own stack'], ['wt up planning', 'start it'], ['wt rm planning', 'stop and delete it, keeping its branches']],
@@ -167,9 +302,18 @@ process.exitCode = await runCli({
167
302
  add: {
168
303
  summary: 'create a worktree, copy its files and data from main, and route its subdomains',
169
304
  run: async (args) => {
170
- const { values, positionals: [task, branch] } = parse(args, ADD_USAGE, { project: PROJECT_OPTION, bg: BG_OPTION }, { positionals: ['<task>', '[branch]'] })
305
+ const { values, positionals: [task, branch] } = parse(args, ADD_USAGE, {
306
+ project: PROJECT_OPTION,
307
+ up: { type: 'boolean', description: 'start its stack once it is ready, as `wt up` does' },
308
+ from: { type: 'string', description: 'the commit, branch or tag a new branch forks from, main\'s current commit by default' },
309
+ bg: BG_OPTION,
310
+ }, { positionals: ['<task>', '[branch]'] })
311
+
312
+ return guarded(async () => {
313
+ const worktree = worktreeOf((await locate(currentDir(), values.project)).project, task)
171
314
 
172
- return guarded(async () => asWorktreeJob('add', task, values, ['add', task, ...(branch === undefined ? [] : [branch])], async () => addTask(task, branch, values)))
315
+ return asWorktreeJob(values.up ? 'add --up' : 'add', worktree, values, ['add', task, ...(branch === undefined ? [] : [branch]), ...(values.from === undefined ? [] : ['--from', values.from]), ...(values.up ? ['--up'] : [])], async () => (await commands()).addTask(task, branch, values))
316
+ }, bgName(values, values.up ? 'add --up' : 'add', task))
173
317
  },
174
318
  },
175
319
  sync: {
@@ -178,9 +322,11 @@ process.exitCode = await runCli({
178
322
  const { values, positionals: [task] } = parse(args, SYNC_USAGE, { project: PROJECT_OPTION }, { positionals: ['[task]'], candidates: TASK })
179
323
 
180
324
  return guarded(async () => {
181
- const { task: name } = await locateWorktree(process.cwd(), task, values.project)
325
+ const worktree = await locateWorktree(currentDir(), task, values.project)
182
326
 
183
- return asWorktreeJob('sync', name, values, ['sync', name], async () => syncTask(name, values))
327
+ assertWorktreeExists(worktree)
328
+
329
+ return asWorktreeJob('sync', worktree, values, ['sync', worktree.task], async () => (await commands()).syncTask(worktree.task, values))
184
330
  })
185
331
  },
186
332
  },
@@ -194,34 +340,55 @@ process.exitCode = await runCli({
194
340
  }, { positionals: ['<task>'], candidates: TASK })
195
341
 
196
342
  return guarded(async () => {
197
- await assertRemovable(task, values)
343
+ const { assertRemovable, removeTask } = await commands()
344
+ const worktree = await assertRemovable(task, values)
198
345
 
199
- return asWorktreeJob('rm', task, values, ['rm', task, ...(values.force ? ['--force'] : [])], async () => removeTask(task, values))
200
- })
346
+ return asWorktreeJob('rm', worktree, values, ['rm', task, ...(values.force ? ['--force'] : [])], async () => removeTask(task, values))
347
+ }, bgName(values, 'rm', task))
201
348
  },
202
349
  },
203
350
  up: {
204
351
  summary: 'start a worktree stack',
352
+ run: async args => startOrStop('up', args),
353
+ },
354
+ down: {
355
+ summary: 'stop a worktree stack',
356
+ run: async args => startOrStop('down', args),
357
+ },
358
+ logs: {
359
+ summary: 'print the log of a worktree\'s last --bg job, or follow its stack\'s compose logs',
205
360
  run: async (args) => {
206
- const { values, positionals: [task] } = parse(args, UP_USAGE, { project: PROJECT_OPTION, bg: BG_OPTION }, { positionals: ['[task]'], candidates: TASK })
361
+ const { values, positionals: [task] } = parse(args, LOGS_USAGE, {
362
+ project: PROJECT_OPTION,
363
+ follow: { type: 'boolean', short: 'f', description: 'follow the stack\'s compose logs instead, from their last 100 lines' },
364
+ }, { positionals: ['[task]'], candidates: TASK })
207
365
 
208
366
  return guarded(async () => {
209
- const worktree = await locateWorktree(process.cwd(), task, values.project)
367
+ const worktree = await locateWorktree(currentDir(), task, values.project)
368
+
369
+ assertWorktreeExists(worktree)
370
+
371
+ if (values.follow)
372
+ return (await import('./stack')).followLogs(worktree)
210
373
 
211
- return asWorktreeJob('up', worktree.task, values, ['up', worktree.task], async () => upTask(worktree))
374
+ const { printJobLog } = await printing()
375
+
376
+ await printJobLog(worktree, await jobs())
377
+
378
+ return 0
212
379
  })
213
380
  },
214
381
  },
215
- down: {
216
- summary: 'stop a worktree stack',
382
+ prune: {
383
+ summary: 'remove what worktrees removed by hand left behind',
217
384
  run: async (args) => {
218
- const { values, positionals: [task] } = parse(args, DOWN_USAGE, { project: PROJECT_OPTION, bg: BG_OPTION }, { positionals: ['[task]'], candidates: TASK })
219
-
220
- return guarded(async () => {
221
- const worktree = await locateWorktree(process.cwd(), task, values.project)
385
+ const { values } = parse(args, PRUNE_USAGE, {
386
+ project: PROJECT_OPTION,
387
+ all: { type: 'boolean', short: 'a', description: 'every project, even from inside one' },
388
+ yes: { type: 'boolean', short: 'y', description: 'remove without asking' },
389
+ }, { positionals: [] })
222
390
 
223
- return asWorktreeJob('down', worktree.task, values, ['down', worktree.task], async () => downTask(worktree))
224
- })
391
+ return guarded(async () => prune(values))
225
392
  },
226
393
  },
227
394
  ls: {
@@ -230,10 +397,24 @@ process.exitCode = await runCli({
230
397
  const { values } = parse(args, LS_USAGE, {
231
398
  project: PROJECT_OPTION,
232
399
  all: { type: 'boolean', short: 'a', description: 'every project, even from inside one' },
233
- json: { type: 'boolean', description: 'print { project, checkouts }, project being the one the directory is in, null outside every one' },
400
+ json: jsonOption('{ project, checkouts }, project being the one the directory is in, null outside every one'),
234
401
  }, { positionals: [] })
235
402
 
236
- return guarded(async () => listWorktrees(values))
403
+ return guarded(async () => (await import('./list')).listWorktrees(values))
404
+ },
405
+ },
406
+ cd: {
407
+ summary: 'go to a worktree, or main (through the shell function of `wt shell`)',
408
+ run: async (args) => {
409
+ const { values, positionals: [task] } = parse(args, CD_USAGE, { project: PROJECT_OPTION }, { positionals: ['[task]'], candidates: { '[task]': async () => [...await taskNames(), 'main'] } })
410
+
411
+ return guarded(async () => {
412
+ const { printPath } = await printing()
413
+
414
+ // Checked first, so a task that does not exist says so rather than the wrapper's absence
415
+ await printPath(task, values)
416
+ fail('cd needs the shell function `wt shell <shell>` prints, as a program cannot change its shell\'s directory', 1, 'cd to the path above')
417
+ })
237
418
  },
238
419
  },
239
420
  path: {
@@ -241,7 +422,7 @@ process.exitCode = await runCli({
241
422
  run: async (args) => {
242
423
  const { values, positionals: [task] } = parse(args, PATH_USAGE, { project: PROJECT_OPTION }, { positionals: ['[task]'], candidates: TASK })
243
424
 
244
- return guarded(async () => printPath(task, values))
425
+ return guarded(async () => (await printing()).printPath(task, values))
245
426
  },
246
427
  },
247
428
  init: {
@@ -252,7 +433,15 @@ process.exitCode = await runCli({
252
433
  print: { type: 'boolean', description: 'print the draft instead of writing it' },
253
434
  }, { positionals: ['[dir]'] })
254
435
 
255
- return guarded(async () => initProject(dir ?? process.cwd(), values))
436
+ return guarded(async () => (await import('./init')).initProject(dir ?? currentDir(), values))
437
+ },
438
+ },
439
+ doctor: {
440
+ summary: 'list the programs wt runs, where each is or how to get it',
441
+ run: async (args) => {
442
+ parse(args, DOCTOR_USAGE, {})
443
+
444
+ return guarded(async () => (await import('./tools')).doctor(await loadProjects()))
256
445
  },
257
446
  },
258
447
  run: {
@@ -268,7 +457,7 @@ process.exitCode = await runCli({
268
457
  if (rest === undefined || rest.length === 0)
269
458
  badUsage('give the command after --')
270
459
 
271
- return guarded(async () => runTask(task, rest, values.project))
460
+ return guarded(async () => (await import('./run')).runTask(task, rest, values.project))
272
461
  },
273
462
  },
274
463
  heavy: {
@@ -283,36 +472,41 @@ process.exitCode = await runCli({
283
472
  if (rest === undefined || rest.length === 0)
284
473
  badUsage('give the command after --')
285
474
 
286
- return guarded(async () => runHeavy(rest))
475
+ return guarded(async () => (await import('./run')).runHeavy(rest))
287
476
  },
288
477
  },
289
478
  shell: {
290
479
  summary: 'print the shell function letting `wt cd` change directory',
291
480
  run: async (args) => {
292
- const { positionals: [shell] } = parse(args, SHELL_USAGE, {}, { positionals: ['<shell>'], candidates: { '<shell>': async () => Promise.resolve(['fish']) } })
481
+ const { positionals: [shell] } = parse(args, SHELL_USAGE, {}, { positionals: ['<shell>'], candidates: { '<shell>': async () => Promise.resolve(['fish', 'bash', 'zsh']) } })
293
482
 
294
483
  return guarded(async () => {
295
- console.log(shellWrapper(shell))
296
- await Promise.resolve()
484
+ console.log((await import('./shell')).shellWrapper(shell))
297
485
  })
298
486
  },
299
487
  },
488
+ open: {
489
+ summary: 'open a worktree\'s main URL in the browser',
490
+ run: async (args) => {
491
+ const { values, positionals: [task] } = parse(args, OPEN_USAGE, { project: PROJECT_OPTION }, { positionals: ['[task]'], candidates: TASK })
492
+
493
+ return guarded(async () => (await printing()).openTask(task, values))
494
+ },
495
+ },
300
496
  url: {
301
497
  summary: 'print the URLs of a worktree',
302
- hidden: true,
303
498
  run: async (args) => {
304
499
  const { values, positionals: [task] } = parse(args, URL_USAGE, { project: PROJECT_OPTION }, { positionals: ['[task]'], candidates: TASK })
305
500
 
306
- return guarded(async () => printUrls(task, values))
501
+ return guarded(async () => (await printing()).printUrls(task, values))
307
502
  },
308
503
  },
309
504
  tasks: {
310
505
  summary: 'print the worktree names of the project',
311
- hidden: true,
312
506
  run: async (args) => {
313
507
  const { values } = parse(args, TASKS_USAGE, { project: PROJECT_OPTION }, { positionals: [] })
314
508
 
315
- return guarded(async () => printTasks(values))
509
+ return guarded(async () => (await printing()).printTasks(values))
316
510
  },
317
511
  },
318
512
  },