@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/cli.ts CHANGED
@@ -1,42 +1,48 @@
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'
4
5
  import type { Worktree } from './project'
5
6
  import process from 'node:process'
6
- import { badUsage, parse, runCli } from '@kaynooo/cli-kit'
7
- import { addTask, assertRemovable, downTask, printPath, printTasks, printUrls, removeTask, syncTask, upTask } from './commands'
8
- import { wtConfig } from './config'
9
- import { failKnown } from './errors'
10
- import { initProject } from './init'
11
- import { Jobs } from './jobs'
12
- import { listWorktrees } from './list'
13
- import { assertWorktreeExists, listTasks, loadProjects, locate, locateWorktree, worktreeOf } from './project'
14
- import { runHeavy, runTask } from './run'
15
- import { shellWrapper } from './shell'
16
- import { slugOf } from './stack'
17
-
18
- async function jobs(): Promise<Jobs> {
19
- return new Jobs({ dir: (await wtConfig()).jobs, app: 'wt' })
20
- }
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
21
20
 
22
21
  // A config that does not load fails like any other known error; `bg` names a `--bg` command, whose refusal is notified too.
23
22
  async function guarded(action: () => Promise<number | void>, bg?: string): Promise<number> {
23
+ let built: Jobs
24
+
24
25
  try {
25
- return await (await jobs()).guarded(action, bg)
26
+ built = await jobs()
26
27
  }
27
28
  catch (error) {
29
+ if (bg !== undefined)
30
+ await notifyRefused(APP, bg, failureText(error))
31
+
28
32
  return failKnown(error)
29
33
  }
34
+
35
+ return built.guarded(action, bg)
30
36
  }
31
37
 
32
38
  // The name a `--bg` command is notified under, none without `--bg`.
33
39
  function bgName(values: { bg?: boolean }, ...words: (string | undefined)[]): string | undefined {
34
- return values.bg ? ['wt', ...words.filter(word => word !== undefined)].join(' ') : undefined
40
+ return values.bg ? [APP, ...words.filter(word => word !== undefined)].join(' ') : undefined
35
41
  }
36
42
 
37
43
  async function projectNames(): Promise<string[]> {
38
44
  try {
39
- return (await loadProjects()).map(project => project.name)
45
+ return (await loadProjects(() => undefined)).map(project => project.name)
40
46
  }
41
47
  catch {
42
48
  return []
@@ -46,7 +52,7 @@ async function projectNames(): Promise<string[]> {
46
52
  // The worktrees of the project the current directory belongs to, none outside one.
47
53
  async function taskNames(): Promise<string[]> {
48
54
  try {
49
- return listTasks((await locate(process.cwd())).project)
55
+ return listTasks((await locate(currentDir())).project)
50
56
  }
51
57
  catch {
52
58
  return []
@@ -61,7 +67,7 @@ const PROJECT_OPTION = { type: 'string', short: 'p', candidates: projectNames, d
61
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
62
68
 
63
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.
64
- async function asWorktreeJob(verb: 'add' | 'sync' | 'rm' | 'up' | 'down', { project, task }: Worktree, options: GlobalOptions & { bg?: boolean }, argv: string[], work: () => Promise<void>): Promise<number> {
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> {
65
71
  const ref = `${project.name}/${task}`
66
72
  const stack = { ref, slug: slugOf(ref) }
67
73
  const queue = await jobs()
@@ -78,16 +84,85 @@ async function asWorktreeJob(verb: 'add' | 'sync' | 'rm' | 'up' | 'down', { proj
78
84
 
79
85
  const job = queue.detach(stack, verb, [import.meta.path, ...argv, '-p', project.name])
80
86
 
81
- console.log(`wt: ${verb} ${ref} runs out of sight, its output in ${job.log}`)
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`}`)
82
88
 
83
89
  return 0
84
90
  }
85
91
 
86
- const ADD_USAGE = `wt add <task> [branch] [-p <name>] [--bg]
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
95
+
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
+ }
109
+ }
110
+
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
133
+ }
134
+
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
157
+ }
158
+
159
+ const ALL_OPTION = { type: 'boolean', description: 'every worktree of the project, one after the other' } as const
87
160
 
88
- 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>.
161
+ const ADD_USAGE = `wt add <task> [branch] [--from <ref>] [--up] [-p <name>] [--bg]
89
162
 
90
- Exits 0 once the worktree is ready, 1 when it cannot be made or provisioned, 2 on a usage error.`
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.
164
+
165
+ Exits 0 once the worktree is ready (and up), 1 when it cannot be made, provisioned or started, 2 on a usage error.`
91
166
 
92
167
  const SYNC_USAGE = `wt sync [task] [-p <name>]
93
168
 
@@ -97,27 +172,66 @@ Exits 0 once it is provisioned, 1 when a step fails, 2 on a usage error.`
97
172
 
98
173
  const RM_USAGE = `wt rm <task> [-f] [-p <name>] [--bg]
99
174
 
100
- 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.
101
176
 
102
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.`
103
178
 
104
- 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.`
105
184
 
106
- 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.
185
+ const DOWN_USAGE = `wt down [task | --all] [-p <name>] [--bg]
107
186
 
108
- Exits 0 once the stack is up, 1 when a command fails, 2 on a usage error.`
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.
109
188
 
110
- const DOWN_USAGE = `wt down [task] [-p <name>] [--bg]
189
+ Exits 0 once the stacks are down, 1 when a command fails, 2 on a usage error.`
111
190
 
112
- 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.
191
+ const LOGS_USAGE = `wt logs [task] [-f] [-p <name>]
113
192
 
114
- Exits 0 once the stack is down, 1 when a command fails, 2 on a usage error.`
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.
200
+
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.`
202
+
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()
208
+
209
+ return verb === 'up' ? upTask(worktree) : downTask(worktree)
210
+ })
211
+
212
+ if (values.all && task !== undefined)
213
+ badUsage(`wt ${verb} takes a task or --all, not both`)
214
+
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
+ }
115
229
 
116
230
  const SHELL_USAGE = `wt shell <shell>
117
231
 
118
- 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.
119
233
 
120
- 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.`
121
235
 
122
236
  const LS_USAGE = `wt ls [-a] [-p <name>] [--json]
123
237
 
@@ -137,6 +251,12 @@ Drafts a manifest for the project checked out in a directory, the current one by
137
251
 
138
252
  Exits 0 once it is written or printed, 1 when the folder holds no compose project, 2 on a usage error.`
139
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
+
140
260
  const RUN_USAGE = `wt run <task> [-p <name>] -- <command>...
141
261
 
142
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.
@@ -149,6 +269,18 @@ Runs a heavy command (a full suite, e2e) once those queued before it, from any s
149
269
 
150
270
  Exits with the command's code, 2 on a usage error.`
151
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
+
152
284
  const URL_USAGE = `wt url [task] [-p <name>]
153
285
 
154
286
  Prints the URLs of a worktree.
@@ -157,12 +289,12 @@ Exits 0 when it printed it, 1 when there is no such worktree or project, 2 on a
157
289
 
158
290
  const TASKS_USAGE = `wt tasks [-p <name>]
159
291
 
160
- 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.
161
293
 
162
- 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.`
163
295
 
164
296
  process.exitCode = await runCli({
165
- name: 'wt',
297
+ name: APP,
166
298
  about: 'git worktrees of a compose project, each with its own stack, data and <task>.<host> subdomains beside main',
167
299
  replaces: 'a second clone, port juggling and a shared database to try a branch',
168
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']],
@@ -170,13 +302,18 @@ process.exitCode = await runCli({
170
302
  add: {
171
303
  summary: 'create a worktree, copy its files and data from main, and route its subdomains',
172
304
  run: async (args) => {
173
- 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]'] })
174
311
 
175
312
  return guarded(async () => {
176
- const worktree = worktreeOf((await locate(process.cwd(), values.project)).project, task)
313
+ const worktree = worktreeOf((await locate(currentDir(), values.project)).project, task)
177
314
 
178
- return asWorktreeJob('add', worktree, values, ['add', task, ...(branch === undefined ? [] : [branch])], async () => addTask(task, branch, values))
179
- }, bgName(values, 'add', task))
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))
180
317
  },
181
318
  },
182
319
  sync: {
@@ -185,11 +322,11 @@ process.exitCode = await runCli({
185
322
  const { values, positionals: [task] } = parse(args, SYNC_USAGE, { project: PROJECT_OPTION }, { positionals: ['[task]'], candidates: TASK })
186
323
 
187
324
  return guarded(async () => {
188
- const worktree = await locateWorktree(process.cwd(), task, values.project)
325
+ const worktree = await locateWorktree(currentDir(), task, values.project)
189
326
 
190
327
  assertWorktreeExists(worktree)
191
328
 
192
- return asWorktreeJob('sync', worktree, values, ['sync', worktree.task], async () => syncTask(worktree.task, values))
329
+ return asWorktreeJob('sync', worktree, values, ['sync', worktree.task], async () => (await commands()).syncTask(worktree.task, values))
193
330
  })
194
331
  },
195
332
  },
@@ -203,6 +340,7 @@ process.exitCode = await runCli({
203
340
  }, { positionals: ['<task>'], candidates: TASK })
204
341
 
205
342
  return guarded(async () => {
343
+ const { assertRemovable, removeTask } = await commands()
206
344
  const worktree = await assertRemovable(task, values)
207
345
 
208
346
  return asWorktreeJob('rm', worktree, values, ['rm', task, ...(values.force ? ['--force'] : [])], async () => removeTask(task, values))
@@ -211,30 +349,46 @@ process.exitCode = await runCli({
211
349
  },
212
350
  up: {
213
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',
214
360
  run: async (args) => {
215
- 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 })
216
365
 
217
366
  return guarded(async () => {
218
- const worktree = await locateWorktree(process.cwd(), task, values.project)
367
+ const worktree = await locateWorktree(currentDir(), task, values.project)
219
368
 
220
369
  assertWorktreeExists(worktree)
221
370
 
222
- return asWorktreeJob('up', worktree, values, ['up', worktree.task], async () => upTask(worktree))
223
- }, bgName(values, 'up', task))
371
+ if (values.follow)
372
+ return (await import('./stack')).followLogs(worktree)
373
+
374
+ const { printJobLog } = await printing()
375
+
376
+ await printJobLog(worktree, await jobs())
377
+
378
+ return 0
379
+ })
224
380
  },
225
381
  },
226
- down: {
227
- summary: 'stop a worktree stack',
382
+ prune: {
383
+ summary: 'remove what worktrees removed by hand left behind',
228
384
  run: async (args) => {
229
- const { values, positionals: [task] } = parse(args, DOWN_USAGE, { project: PROJECT_OPTION, bg: BG_OPTION }, { positionals: ['[task]'], candidates: TASK })
230
-
231
- return guarded(async () => {
232
- const worktree = await locateWorktree(process.cwd(), task, values.project)
233
-
234
- assertWorktreeExists(worktree)
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: [] })
235
390
 
236
- return asWorktreeJob('down', worktree, values, ['down', worktree.task], async () => downTask(worktree))
237
- }, bgName(values, 'down', task))
391
+ return guarded(async () => prune(values))
238
392
  },
239
393
  },
240
394
  ls: {
@@ -243,10 +397,24 @@ process.exitCode = await runCli({
243
397
  const { values } = parse(args, LS_USAGE, {
244
398
  project: PROJECT_OPTION,
245
399
  all: { type: 'boolean', short: 'a', description: 'every project, even from inside one' },
246
- 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'),
247
401
  }, { positionals: [] })
248
402
 
249
- 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
+ })
250
418
  },
251
419
  },
252
420
  path: {
@@ -254,7 +422,7 @@ process.exitCode = await runCli({
254
422
  run: async (args) => {
255
423
  const { values, positionals: [task] } = parse(args, PATH_USAGE, { project: PROJECT_OPTION }, { positionals: ['[task]'], candidates: TASK })
256
424
 
257
- return guarded(async () => printPath(task, values))
425
+ return guarded(async () => (await printing()).printPath(task, values))
258
426
  },
259
427
  },
260
428
  init: {
@@ -265,7 +433,15 @@ process.exitCode = await runCli({
265
433
  print: { type: 'boolean', description: 'print the draft instead of writing it' },
266
434
  }, { positionals: ['[dir]'] })
267
435
 
268
- 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()))
269
445
  },
270
446
  },
271
447
  run: {
@@ -281,7 +457,7 @@ process.exitCode = await runCli({
281
457
  if (rest === undefined || rest.length === 0)
282
458
  badUsage('give the command after --')
283
459
 
284
- return guarded(async () => runTask(task, rest, values.project))
460
+ return guarded(async () => (await import('./run')).runTask(task, rest, values.project))
285
461
  },
286
462
  },
287
463
  heavy: {
@@ -296,36 +472,41 @@ process.exitCode = await runCli({
296
472
  if (rest === undefined || rest.length === 0)
297
473
  badUsage('give the command after --')
298
474
 
299
- return guarded(async () => runHeavy(rest))
475
+ return guarded(async () => (await import('./run')).runHeavy(rest))
300
476
  },
301
477
  },
302
478
  shell: {
303
479
  summary: 'print the shell function letting `wt cd` change directory',
304
480
  run: async (args) => {
305
- 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']) } })
306
482
 
307
483
  return guarded(async () => {
308
- console.log(shellWrapper(shell))
309
- await Promise.resolve()
484
+ console.log((await import('./shell')).shellWrapper(shell))
310
485
  })
311
486
  },
312
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
+ },
313
496
  url: {
314
497
  summary: 'print the URLs of a worktree',
315
- hidden: true,
316
498
  run: async (args) => {
317
499
  const { values, positionals: [task] } = parse(args, URL_USAGE, { project: PROJECT_OPTION }, { positionals: ['[task]'], candidates: TASK })
318
500
 
319
- return guarded(async () => printUrls(task, values))
501
+ return guarded(async () => (await printing()).printUrls(task, values))
320
502
  },
321
503
  },
322
504
  tasks: {
323
505
  summary: 'print the worktree names of the project',
324
- hidden: true,
325
506
  run: async (args) => {
326
507
  const { values } = parse(args, TASKS_USAGE, { project: PROJECT_OPTION }, { positionals: [] })
327
508
 
328
- return guarded(async () => printTasks(values))
509
+ return guarded(async () => (await printing()).printTasks(values))
329
510
  },
330
511
  },
331
512
  },