@aku11i/phantom 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/phantom.js DELETED
@@ -1,2544 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- // src/bin/phantom.ts
4
- import { argv, exit as exit2 } from "node:process";
5
-
6
- // src/cli/handlers/attach.ts
7
- import { parseArgs } from "node:util";
8
-
9
- // src/core/git/libs/get-git-root.ts
10
- import { dirname, resolve } from "node:path";
11
-
12
- // src/core/git/executor.ts
13
- import { execFile as execFileCallback } from "node:child_process";
14
- import { promisify } from "node:util";
15
- var execFile = promisify(execFileCallback);
16
- async function executeGitCommand(args2, options = {}) {
17
- try {
18
- const result = await execFile("git", args2, {
19
- cwd: options.cwd,
20
- env: options.env || process.env,
21
- encoding: "utf8"
22
- });
23
- return {
24
- stdout: result.stdout.trim(),
25
- stderr: result.stderr.trim()
26
- };
27
- } catch (error) {
28
- if (error && typeof error === "object" && "stdout" in error && "stderr" in error) {
29
- const execError = error;
30
- if (execError.stderr?.trim()) {
31
- throw new Error(execError.stderr.trim());
32
- }
33
- return {
34
- stdout: execError.stdout?.trim() || "",
35
- stderr: execError.stderr?.trim() || ""
36
- };
37
- }
38
- throw error;
39
- }
40
- }
41
- async function executeGitCommandInDirectory(directory, args2) {
42
- return executeGitCommand(["-C", directory, ...args2], {});
43
- }
44
-
45
- // src/core/git/libs/get-git-root.ts
46
- async function getGitRoot() {
47
- const { stdout: stdout2 } = await executeGitCommand(["rev-parse", "--git-common-dir"]);
48
- if (stdout2.endsWith("/.git") || stdout2 === ".git") {
49
- return resolve(process.cwd(), dirname(stdout2));
50
- }
51
- const { stdout: toplevel } = await executeGitCommand([
52
- "rev-parse",
53
- "--show-toplevel"
54
- ]);
55
- return toplevel;
56
- }
57
-
58
- // src/core/types/result.ts
59
- var ok = (value) => ({
60
- ok: true,
61
- value
62
- });
63
- var err = (error) => ({
64
- ok: false,
65
- error
66
- });
67
- var isOk = (result) => result.ok;
68
- var isErr = (result) => !result.ok;
69
-
70
- // src/core/worktree/validate.ts
71
- import fs from "node:fs/promises";
72
-
73
- // src/core/paths.ts
74
- import { join } from "node:path";
75
- function getPhantomDirectory(gitRoot) {
76
- return join(gitRoot, ".git", "phantom", "worktrees");
77
- }
78
- function getWorktreePath(gitRoot, name) {
79
- return join(getPhantomDirectory(gitRoot), name);
80
- }
81
-
82
- // src/core/worktree/errors.ts
83
- var WorktreeError = class extends Error {
84
- constructor(message) {
85
- super(message);
86
- this.name = "WorktreeError";
87
- }
88
- };
89
- var WorktreeNotFoundError = class extends WorktreeError {
90
- constructor(name) {
91
- super(`Worktree '${name}' not found`);
92
- this.name = "WorktreeNotFoundError";
93
- }
94
- };
95
- var WorktreeAlreadyExistsError = class extends WorktreeError {
96
- constructor(name) {
97
- super(`Worktree '${name}' already exists`);
98
- this.name = "WorktreeAlreadyExistsError";
99
- }
100
- };
101
- var GitOperationError = class extends WorktreeError {
102
- constructor(operation, details) {
103
- super(`Git ${operation} failed: ${details}`);
104
- this.name = "GitOperationError";
105
- }
106
- };
107
- var BranchNotFoundError = class extends WorktreeError {
108
- constructor(branchName) {
109
- super(`Branch '${branchName}' not found`);
110
- this.name = "BranchNotFoundError";
111
- }
112
- };
113
-
114
- // src/core/worktree/validate.ts
115
- async function validateWorktreeExists(gitRoot, name) {
116
- const worktreePath = getWorktreePath(gitRoot, name);
117
- try {
118
- await fs.access(worktreePath);
119
- return ok({ path: worktreePath });
120
- } catch {
121
- return err(new WorktreeNotFoundError(name));
122
- }
123
- }
124
- async function validateWorktreeDoesNotExist(gitRoot, name) {
125
- const worktreePath = getWorktreePath(gitRoot, name);
126
- try {
127
- await fs.access(worktreePath);
128
- return err(new WorktreeAlreadyExistsError(name));
129
- } catch {
130
- return ok({ path: worktreePath });
131
- }
132
- }
133
- function validateWorktreeName(name) {
134
- if (!name || name.trim() === "") {
135
- return err(new Error("Phantom name cannot be empty"));
136
- }
137
- const validNamePattern = /^[a-zA-Z0-9\-_.\/]+$/;
138
- if (!validNamePattern.test(name)) {
139
- return err(
140
- new Error(
141
- "Phantom name can only contain letters, numbers, hyphens, underscores, dots, and slashes"
142
- )
143
- );
144
- }
145
- if (name.includes("..")) {
146
- return err(new Error("Phantom name cannot contain consecutive dots"));
147
- }
148
- return ok(void 0);
149
- }
150
-
151
- // src/core/process/spawn.ts
152
- import {
153
- spawn as nodeSpawn
154
- } from "node:child_process";
155
-
156
- // src/core/process/errors.ts
157
- var ProcessError = class extends Error {
158
- exitCode;
159
- constructor(message, exitCode) {
160
- super(message);
161
- this.name = "ProcessError";
162
- this.exitCode = exitCode;
163
- }
164
- };
165
- var ProcessExecutionError = class extends ProcessError {
166
- constructor(command2, exitCode) {
167
- super(`Command '${command2}' failed with exit code ${exitCode}`, exitCode);
168
- this.name = "ProcessExecutionError";
169
- }
170
- };
171
- var ProcessSignalError = class extends ProcessError {
172
- constructor(signal) {
173
- const exitCode = 128 + (signal === "SIGTERM" ? 15 : 1);
174
- super(`Command terminated by signal: ${signal}`, exitCode);
175
- this.name = "ProcessSignalError";
176
- }
177
- };
178
- var ProcessSpawnError = class extends ProcessError {
179
- constructor(command2, details) {
180
- super(`Error executing command '${command2}': ${details}`);
181
- this.name = "ProcessSpawnError";
182
- }
183
- };
184
-
185
- // src/core/process/spawn.ts
186
- async function spawnProcess(config) {
187
- return new Promise((resolve2) => {
188
- const { command: command2, args: args2 = [], options = {} } = config;
189
- const childProcess = nodeSpawn(command2, args2, {
190
- stdio: "inherit",
191
- ...options
192
- });
193
- childProcess.on("error", (error) => {
194
- resolve2(err(new ProcessSpawnError(command2, error.message)));
195
- });
196
- childProcess.on("exit", (code, signal) => {
197
- if (signal) {
198
- resolve2(err(new ProcessSignalError(signal)));
199
- } else {
200
- const exitCode = code ?? 0;
201
- if (exitCode === 0) {
202
- resolve2(ok({ exitCode }));
203
- } else {
204
- resolve2(err(new ProcessExecutionError(command2, exitCode)));
205
- }
206
- }
207
- });
208
- });
209
- }
210
-
211
- // src/core/process/exec.ts
212
- async function execInWorktree(gitRoot, worktreeName, command2, options = {}) {
213
- const validation = await validateWorktreeExists(gitRoot, worktreeName);
214
- if (isErr(validation)) {
215
- return err(validation.error);
216
- }
217
- const worktreePath = validation.value.path;
218
- const [cmd, ...args2] = command2;
219
- const stdio = options.interactive ? "inherit" : ["ignore", "inherit", "inherit"];
220
- return spawnProcess({
221
- command: cmd,
222
- args: args2,
223
- options: {
224
- cwd: worktreePath,
225
- stdio
226
- }
227
- });
228
- }
229
-
230
- // src/core/process/env.ts
231
- function getPhantomEnv(worktreeName, worktreePath) {
232
- return {
233
- PHANTOM: "1",
234
- PHANTOM_NAME: worktreeName,
235
- PHANTOM_PATH: worktreePath
236
- };
237
- }
238
-
239
- // src/core/process/shell.ts
240
- async function shellInWorktree(gitRoot, worktreeName) {
241
- const validation = await validateWorktreeExists(gitRoot, worktreeName);
242
- if (isErr(validation)) {
243
- return err(validation.error);
244
- }
245
- const worktreePath = validation.value.path;
246
- const shell = process.env.SHELL || "/bin/sh";
247
- return spawnProcess({
248
- command: shell,
249
- args: [],
250
- options: {
251
- cwd: worktreePath,
252
- env: {
253
- ...process.env,
254
- ...getPhantomEnv(worktreeName, worktreePath)
255
- }
256
- }
257
- });
258
- }
259
-
260
- // src/core/worktree/attach.ts
261
- import { existsSync } from "node:fs";
262
-
263
- // src/core/git/libs/attach-worktree.ts
264
- async function attachWorktree(gitRoot, worktreePath, branchName) {
265
- try {
266
- await executeGitCommand(["worktree", "add", worktreePath, branchName], {
267
- cwd: gitRoot
268
- });
269
- return ok(void 0);
270
- } catch (error) {
271
- return err(
272
- error instanceof Error ? error : new Error(`Failed to attach worktree: ${String(error)}`)
273
- );
274
- }
275
- }
276
-
277
- // src/core/git/libs/branch-exists.ts
278
- async function branchExists(gitRoot, branchName) {
279
- try {
280
- await executeGitCommand(
281
- ["show-ref", "--verify", "--quiet", `refs/heads/${branchName}`],
282
- { cwd: gitRoot }
283
- );
284
- return ok(true);
285
- } catch (error) {
286
- if (error && typeof error === "object" && "code" in error) {
287
- const execError = error;
288
- if (execError.code === 1) {
289
- return ok(false);
290
- }
291
- }
292
- return err(
293
- new Error(
294
- `Failed to check branch existence: ${error instanceof Error ? error.message : String(error)}`
295
- )
296
- );
297
- }
298
- }
299
-
300
- // src/core/worktree/attach.ts
301
- async function attachWorktreeCore(gitRoot, name) {
302
- const validation = validateWorktreeName(name);
303
- if (isErr(validation)) {
304
- return validation;
305
- }
306
- const worktreePath = getWorktreePath(gitRoot, name);
307
- if (existsSync(worktreePath)) {
308
- return err(new WorktreeAlreadyExistsError(name));
309
- }
310
- const branchCheckResult = await branchExists(gitRoot, name);
311
- if (isErr(branchCheckResult)) {
312
- return err(branchCheckResult.error);
313
- }
314
- if (!branchCheckResult.value) {
315
- return err(new BranchNotFoundError(name));
316
- }
317
- const attachResult = await attachWorktree(gitRoot, worktreePath, name);
318
- if (isErr(attachResult)) {
319
- return err(attachResult.error);
320
- }
321
- return ok(worktreePath);
322
- }
323
-
324
- // src/cli/output.ts
325
- var output = {
326
- log: (message) => {
327
- console.log(message);
328
- },
329
- error: (message) => {
330
- console.error(message);
331
- },
332
- warn: (message) => {
333
- console.warn(message);
334
- },
335
- table: (data) => {
336
- console.table(data);
337
- },
338
- processOutput: (proc) => {
339
- proc.stdout?.pipe(process.stdout);
340
- proc.stderr?.pipe(process.stderr);
341
- }
342
- };
343
-
344
- // src/cli/errors.ts
345
- var exitCodes = {
346
- success: 0,
347
- generalError: 1,
348
- notFound: 2,
349
- validationError: 3
350
- };
351
- function exitWithSuccess() {
352
- process.exit(exitCodes.success);
353
- }
354
- function exitWithError(message, exitCode = exitCodes.generalError) {
355
- output.error(message);
356
- process.exit(exitCode);
357
- }
358
-
359
- // src/cli/handlers/attach.ts
360
- async function attachHandler(args2) {
361
- const { positionals, values } = parseArgs({
362
- args: args2,
363
- strict: true,
364
- allowPositionals: true,
365
- options: {
366
- shell: {
367
- type: "boolean",
368
- short: "s"
369
- },
370
- exec: {
371
- type: "string",
372
- short: "e"
373
- }
374
- }
375
- });
376
- if (positionals.length === 0) {
377
- exitWithError(
378
- "Missing required argument: branch name",
379
- exitCodes.validationError
380
- );
381
- }
382
- const [branchName] = positionals;
383
- if (values.shell && values.exec) {
384
- exitWithError(
385
- "Cannot use both --shell and --exec options",
386
- exitCodes.validationError
387
- );
388
- }
389
- const gitRoot = await getGitRoot();
390
- const result = await attachWorktreeCore(gitRoot, branchName);
391
- if (isErr(result)) {
392
- const error = result.error;
393
- if (error instanceof WorktreeAlreadyExistsError) {
394
- exitWithError(error.message, exitCodes.validationError);
395
- }
396
- if (error instanceof BranchNotFoundError) {
397
- exitWithError(error.message, exitCodes.notFound);
398
- }
399
- exitWithError(error.message, exitCodes.generalError);
400
- }
401
- const worktreePath = result.value;
402
- output.log(`Attached phantom: ${branchName}`);
403
- if (values.shell) {
404
- const shellResult = await shellInWorktree(gitRoot, branchName);
405
- if (isErr(shellResult)) {
406
- exitWithError(shellResult.error.message, exitCodes.generalError);
407
- }
408
- } else if (values.exec) {
409
- const shell = process.env.SHELL || "/bin/sh";
410
- const execResult = await execInWorktree(
411
- gitRoot,
412
- branchName,
413
- [shell, "-c", values.exec],
414
- { interactive: true }
415
- );
416
- if (isErr(execResult)) {
417
- exitWithError(execResult.error.message, exitCodes.generalError);
418
- }
419
- }
420
- }
421
-
422
- // src/cli/handlers/completion.ts
423
- import { exit } from "node:process";
424
- var FISH_COMPLETION_SCRIPT = `# Fish completion for phantom
425
- # Place this in ~/.config/fish/completions/phantom.fish
426
-
427
- function __phantom_list_worktrees
428
- phantom list --names 2>/dev/null
429
- end
430
-
431
- function __phantom_using_command
432
- set -l cmd (commandline -opc)
433
- set -l cmd_count (count $cmd)
434
- if test $cmd_count -eq 1
435
- # No subcommand yet, so any command can be used
436
- if test (count $argv) -eq 0
437
- return 0
438
- else
439
- return 1
440
- end
441
- else if test $cmd_count -ge 2
442
- # Check if we're in the context of a specific command
443
- if test (count $argv) -gt 0 -a "$argv[1]" = "$cmd[2]"
444
- return 0
445
- end
446
- end
447
- return 1
448
- end
449
-
450
- # Disable file completion for phantom
451
- complete -c phantom -f
452
-
453
- # Main commands
454
- complete -c phantom -n "__phantom_using_command" -a "create" -d "Create a new Git worktree (phantom)"
455
- complete -c phantom -n "__phantom_using_command" -a "attach" -d "Attach to an existing branch by creating a new worktree"
456
- complete -c phantom -n "__phantom_using_command" -a "list" -d "List all Git worktrees (phantoms)"
457
- complete -c phantom -n "__phantom_using_command" -a "where" -d "Output the filesystem path of a specific worktree"
458
- complete -c phantom -n "__phantom_using_command" -a "delete" -d "Delete a Git worktree (phantom)"
459
- complete -c phantom -n "__phantom_using_command" -a "exec" -d "Execute a command in a worktree directory"
460
- complete -c phantom -n "__phantom_using_command" -a "shell" -d "Open an interactive shell in a worktree directory"
461
- complete -c phantom -n "__phantom_using_command" -a "version" -d "Display phantom version information"
462
- complete -c phantom -n "__phantom_using_command" -a "completion" -d "Generate shell completion scripts"
463
-
464
- # Global options
465
- complete -c phantom -l help -d "Show help (-h)"
466
- complete -c phantom -l version -d "Show version (-v)"
467
-
468
- # create command options
469
- complete -c phantom -n "__phantom_using_command create" -l shell -d "Open an interactive shell in the new worktree after creation (-s)"
470
- complete -c phantom -n "__phantom_using_command create" -l exec -d "Execute a command in the new worktree after creation (-x)" -x
471
- complete -c phantom -n "__phantom_using_command create" -l tmux -d "Open the worktree in a new tmux window (-t)"
472
- complete -c phantom -n "__phantom_using_command create" -l tmux-vertical -d "Open the worktree in a vertical tmux pane"
473
- complete -c phantom -n "__phantom_using_command create" -l tmux-horizontal -d "Open the worktree in a horizontal tmux pane"
474
- complete -c phantom -n "__phantom_using_command create" -l copy-file -d "Copy specified files from the current worktree" -r
475
-
476
- # attach command options
477
- complete -c phantom -n "__phantom_using_command attach" -l shell -d "Open an interactive shell in the worktree after attaching (-s)"
478
- complete -c phantom -n "__phantom_using_command attach" -l exec -d "Execute a command in the worktree after attaching (-x)" -x
479
-
480
- # list command options
481
- complete -c phantom -n "__phantom_using_command list" -l fzf -d "Use fzf for interactive selection"
482
- complete -c phantom -n "__phantom_using_command list" -l names -d "Output only phantom names (for scripts and completion)"
483
-
484
- # where command options
485
- complete -c phantom -n "__phantom_using_command where" -l fzf -d "Use fzf for interactive selection"
486
- complete -c phantom -n "__phantom_using_command where" -a "(__phantom_list_worktrees)"
487
-
488
- # delete command options
489
- complete -c phantom -n "__phantom_using_command delete" -l force -d "Force deletion even if worktree has uncommitted changes (-f)"
490
- complete -c phantom -n "__phantom_using_command delete" -l current -d "Delete the current worktree"
491
- complete -c phantom -n "__phantom_using_command delete" -l fzf -d "Use fzf for interactive selection"
492
- complete -c phantom -n "__phantom_using_command delete" -a "(__phantom_list_worktrees)"
493
-
494
- # exec command - accept worktree names and then any command
495
- complete -c phantom -n "__phantom_using_command exec" -a "(__phantom_list_worktrees)"
496
-
497
- # shell command options
498
- complete -c phantom -n "__phantom_using_command shell" -l fzf -d "Use fzf for interactive selection"
499
- complete -c phantom -n "__phantom_using_command shell" -a "(__phantom_list_worktrees)"
500
-
501
- # completion command - shell names
502
- complete -c phantom -n "__phantom_using_command completion" -a "fish zsh" -d "Shell type"`;
503
- var ZSH_COMPLETION_SCRIPT = `#compdef phantom
504
- # Zsh completion for phantom
505
- # Place this in a directory in your $fpath (e.g., ~/.zsh/completions/)
506
- # Or load dynamically with: eval "$(phantom completion zsh)"
507
-
508
- # Only define the function, don't execute it
509
- _phantom() {
510
- local -a commands
511
- commands=(
512
- 'create:Create a new Git worktree (phantom)'
513
- 'attach:Attach to an existing branch by creating a new worktree'
514
- 'list:List all Git worktrees (phantoms)'
515
- 'where:Output the filesystem path of a specific worktree'
516
- 'delete:Delete a Git worktree (phantom)'
517
- 'exec:Execute a command in a worktree directory'
518
- 'shell:Open an interactive shell in a worktree directory'
519
- 'version:Display phantom version information'
520
- 'completion:Generate shell completion scripts'
521
- )
522
-
523
- _arguments -C \\
524
- '--help[Show help (-h)]' \\
525
- '--version[Show version (-v)]' \\
526
- '1:command:->command' \\
527
- '*::arg:->args'
528
-
529
- case \${state} in
530
- command)
531
- _describe 'phantom command' commands
532
- ;;
533
- args)
534
- case \${line[1]} in
535
- create)
536
- _arguments \\
537
- '--shell[Open an interactive shell in the new worktree after creation (-s)]' \\
538
- '--exec[Execute a command in the new worktree after creation (-x)]:command:' \\
539
- '--tmux[Open the worktree in a new tmux window (-t)]' \\
540
- '--tmux-vertical[Open the worktree in a vertical tmux pane]' \\
541
- '--tmux-horizontal[Open the worktree in a horizontal tmux pane]' \\
542
- '*--copy-file[Copy specified files from the current worktree]:file:_files' \\
543
- '1:name:'
544
- ;;
545
- attach)
546
- _arguments \\
547
- '--shell[Open an interactive shell in the worktree after attaching (-s)]' \\
548
- '--exec[Execute a command in the worktree after attaching (-x)]:command:' \\
549
- '1:worktree-name:' \\
550
- '2:branch-name:'
551
- ;;
552
- list)
553
- _arguments \\
554
- '--fzf[Use fzf for interactive selection]' \\
555
- '--names[Output only phantom names (for scripts and completion)]'
556
- ;;
557
- where|delete|shell)
558
- local worktrees
559
- worktrees=(\${(f)"$(phantom list --names 2>/dev/null)"})
560
- if [[ \${line[1]} == "where" || \${line[1]} == "shell" ]]; then
561
- _arguments \\
562
- '--fzf[Use fzf for interactive selection]' \\
563
- '1:worktree:(\${(q)worktrees[@]})'
564
- elif [[ \${line[1]} == "delete" ]]; then
565
- _arguments \\
566
- '--force[Force deletion even if worktree has uncommitted changes (-f)]' \\
567
- '--current[Delete the current worktree]' \\
568
- '--fzf[Use fzf for interactive selection]' \\
569
- '1:worktree:(\${(q)worktrees[@]})'
570
- fi
571
- ;;
572
- exec)
573
- local worktrees
574
- worktrees=(\${(f)"$(phantom list --names 2>/dev/null)"})
575
- _arguments \\
576
- '1:worktree:(\${(q)worktrees[@]})' \\
577
- '*:command:_command_names'
578
- ;;
579
- completion)
580
- _arguments \\
581
- '1:shell:(fish zsh)'
582
- ;;
583
- esac
584
- ;;
585
- esac
586
- }
587
-
588
- # Register the completion function if loading dynamically
589
- if [[ -n \${ZSH_VERSION} ]]; then
590
- autoload -Uz compinit && compinit -C
591
- compdef _phantom phantom
592
- fi`;
593
- function completionHandler(args2) {
594
- const shell = args2[0];
595
- if (!shell) {
596
- output.error("Usage: phantom completion <shell>");
597
- output.error("Supported shells: fish, zsh");
598
- exit(1);
599
- }
600
- switch (shell.toLowerCase()) {
601
- case "fish":
602
- console.log(FISH_COMPLETION_SCRIPT);
603
- break;
604
- case "zsh":
605
- console.log(ZSH_COMPLETION_SCRIPT);
606
- break;
607
- default:
608
- output.error(`Unsupported shell: ${shell}`);
609
- output.error("Supported shells: fish, zsh");
610
- exit(1);
611
- }
612
- }
613
-
614
- // src/cli/handlers/create.ts
615
- import { parseArgs as parseArgs2 } from "node:util";
616
-
617
- // src/core/config/loader.ts
618
- import fs2 from "node:fs/promises";
619
- import path from "node:path";
620
-
621
- // src/core/utils/type-guards.ts
622
- function isObject(value) {
623
- return typeof value === "object" && value !== null && !Array.isArray(value);
624
- }
625
-
626
- // src/core/config/validate.ts
627
- var ConfigValidationError = class extends Error {
628
- constructor(message) {
629
- super(`Invalid phantom.config.json: ${message}`);
630
- this.name = "ConfigValidationError";
631
- }
632
- };
633
- function validateConfig(config) {
634
- if (!isObject(config)) {
635
- return err(new ConfigValidationError("Configuration must be an object"));
636
- }
637
- const cfg = config;
638
- if (cfg.postCreate !== void 0) {
639
- if (!isObject(cfg.postCreate)) {
640
- return err(new ConfigValidationError("postCreate must be an object"));
641
- }
642
- const postCreate = cfg.postCreate;
643
- if (postCreate.copyFiles !== void 0) {
644
- if (!Array.isArray(postCreate.copyFiles)) {
645
- return err(
646
- new ConfigValidationError("postCreate.copyFiles must be an array")
647
- );
648
- }
649
- if (!postCreate.copyFiles.every((f) => typeof f === "string")) {
650
- return err(
651
- new ConfigValidationError(
652
- "postCreate.copyFiles must contain only strings"
653
- )
654
- );
655
- }
656
- }
657
- if (postCreate.commands !== void 0) {
658
- if (!Array.isArray(postCreate.commands)) {
659
- return err(
660
- new ConfigValidationError("postCreate.commands must be an array")
661
- );
662
- }
663
- if (!postCreate.commands.every((c) => typeof c === "string")) {
664
- return err(
665
- new ConfigValidationError(
666
- "postCreate.commands must contain only strings"
667
- )
668
- );
669
- }
670
- }
671
- }
672
- return ok(config);
673
- }
674
-
675
- // src/core/config/loader.ts
676
- var ConfigNotFoundError = class extends Error {
677
- constructor() {
678
- super("phantom.config.json not found");
679
- this.name = "ConfigNotFoundError";
680
- }
681
- };
682
- var ConfigParseError = class extends Error {
683
- constructor(message) {
684
- super(`Failed to parse phantom.config.json: ${message}`);
685
- this.name = "ConfigParseError";
686
- }
687
- };
688
- async function loadConfig(gitRoot) {
689
- const configPath = path.join(gitRoot, "phantom.config.json");
690
- try {
691
- const content = await fs2.readFile(configPath, "utf-8");
692
- try {
693
- const parsed = JSON.parse(content);
694
- const validationResult = validateConfig(parsed);
695
- if (!validationResult.ok) {
696
- return err(validationResult.error);
697
- }
698
- return ok(validationResult.value);
699
- } catch (error) {
700
- return err(
701
- new ConfigParseError(
702
- error instanceof Error ? error.message : String(error)
703
- )
704
- );
705
- }
706
- } catch (error) {
707
- if (error instanceof Error && "code" in error && error.code === "ENOENT") {
708
- return err(new ConfigNotFoundError());
709
- }
710
- throw error;
711
- }
712
- }
713
-
714
- // src/core/process/tmux.ts
715
- async function isInsideTmux() {
716
- return process.env.TMUX !== void 0;
717
- }
718
- async function executeTmuxCommand(options) {
719
- const { direction, command: command2, args: args2, cwd, env, windowName } = options;
720
- const tmuxArgs = [];
721
- switch (direction) {
722
- case "new":
723
- tmuxArgs.push("new-window");
724
- if (windowName) {
725
- tmuxArgs.push("-n", windowName);
726
- }
727
- break;
728
- case "vertical":
729
- tmuxArgs.push("split-window", "-v");
730
- break;
731
- case "horizontal":
732
- tmuxArgs.push("split-window", "-h");
733
- break;
734
- }
735
- if (cwd) {
736
- tmuxArgs.push("-c", cwd);
737
- }
738
- if (env) {
739
- for (const [key, value] of Object.entries(env)) {
740
- tmuxArgs.push("-e", `${key}=${value}`);
741
- }
742
- }
743
- tmuxArgs.push(command2);
744
- if (args2 && args2.length > 0) {
745
- tmuxArgs.push(...args2);
746
- }
747
- const result = await spawnProcess({
748
- command: "tmux",
749
- args: tmuxArgs
750
- });
751
- return result;
752
- }
753
-
754
- // src/core/worktree/create.ts
755
- import fs3 from "node:fs/promises";
756
-
757
- // src/core/git/libs/add-worktree.ts
758
- async function addWorktree(options) {
759
- const { path: path3, branch, commitish = "HEAD" } = options;
760
- await executeGitCommand(["worktree", "add", path3, "-b", branch, commitish]);
761
- }
762
-
763
- // src/core/worktree/file-copier.ts
764
- import { copyFile, mkdir, stat } from "node:fs/promises";
765
- import path2 from "node:path";
766
- var FileCopyError = class extends Error {
767
- file;
768
- constructor(file, message) {
769
- super(`Failed to copy ${file}: ${message}`);
770
- this.name = "FileCopyError";
771
- this.file = file;
772
- }
773
- };
774
- async function copyFiles(sourceDir, targetDir, files) {
775
- const copiedFiles = [];
776
- const skippedFiles = [];
777
- for (const file of files) {
778
- const sourcePath = path2.join(sourceDir, file);
779
- const targetPath = path2.join(targetDir, file);
780
- try {
781
- const stats = await stat(sourcePath);
782
- if (!stats.isFile()) {
783
- skippedFiles.push(file);
784
- continue;
785
- }
786
- const targetDirPath = path2.dirname(targetPath);
787
- await mkdir(targetDirPath, { recursive: true });
788
- await copyFile(sourcePath, targetPath);
789
- copiedFiles.push(file);
790
- } catch (error) {
791
- if (error instanceof Error && "code" in error && error.code === "ENOENT") {
792
- skippedFiles.push(file);
793
- } else {
794
- return err(
795
- new FileCopyError(
796
- file,
797
- error instanceof Error ? error.message : String(error)
798
- )
799
- );
800
- }
801
- }
802
- }
803
- return ok({ copiedFiles, skippedFiles });
804
- }
805
-
806
- // src/core/worktree/create.ts
807
- async function createWorktree(gitRoot, name, options = {}) {
808
- const nameValidation = validateWorktreeName(name);
809
- if (isErr(nameValidation)) {
810
- return nameValidation;
811
- }
812
- const { branch = name, commitish = "HEAD" } = options;
813
- const worktreesPath = getPhantomDirectory(gitRoot);
814
- const worktreePath = getWorktreePath(gitRoot, name);
815
- try {
816
- await fs3.access(worktreesPath);
817
- } catch {
818
- await fs3.mkdir(worktreesPath, { recursive: true });
819
- }
820
- const validation = await validateWorktreeDoesNotExist(gitRoot, name);
821
- if (isErr(validation)) {
822
- return err(validation.error);
823
- }
824
- try {
825
- await addWorktree({
826
- path: worktreePath,
827
- branch,
828
- commitish
829
- });
830
- let copiedFiles;
831
- let skippedFiles;
832
- let copyError;
833
- if (options.copyFiles && options.copyFiles.length > 0) {
834
- const copyResult = await copyFiles(
835
- gitRoot,
836
- worktreePath,
837
- options.copyFiles
838
- );
839
- if (isOk(copyResult)) {
840
- copiedFiles = copyResult.value.copiedFiles;
841
- skippedFiles = copyResult.value.skippedFiles;
842
- } else {
843
- copyError = copyResult.error.message;
844
- }
845
- }
846
- return ok({
847
- message: `Created worktree '${name}' at ${worktreePath}`,
848
- path: worktreePath,
849
- copiedFiles,
850
- skippedFiles,
851
- copyError
852
- });
853
- } catch (error) {
854
- const errorMessage = error instanceof Error ? error.message : String(error);
855
- return err(new GitOperationError("worktree add", errorMessage));
856
- }
857
- }
858
-
859
- // src/cli/handlers/create.ts
860
- async function createHandler(args2) {
861
- const { values, positionals } = parseArgs2({
862
- args: args2,
863
- options: {
864
- shell: {
865
- type: "boolean",
866
- short: "s"
867
- },
868
- exec: {
869
- type: "string",
870
- short: "x"
871
- },
872
- tmux: {
873
- type: "boolean",
874
- short: "t"
875
- },
876
- "tmux-vertical": {
877
- type: "boolean"
878
- },
879
- "tmux-v": {
880
- type: "boolean"
881
- },
882
- "tmux-horizontal": {
883
- type: "boolean"
884
- },
885
- "tmux-h": {
886
- type: "boolean"
887
- },
888
- "copy-file": {
889
- type: "string",
890
- multiple: true
891
- }
892
- },
893
- strict: true,
894
- allowPositionals: true
895
- });
896
- if (positionals.length === 0) {
897
- exitWithError(
898
- "Please provide a name for the new worktree",
899
- exitCodes.validationError
900
- );
901
- }
902
- const worktreeName = positionals[0];
903
- const openShell = values.shell ?? false;
904
- const execCommand = values.exec;
905
- const copyFileOptions = values["copy-file"];
906
- const tmuxOption = values.tmux || values["tmux-vertical"] || values["tmux-v"] || values["tmux-horizontal"] || values["tmux-h"];
907
- let tmuxDirection;
908
- if (values.tmux) {
909
- tmuxDirection = "new";
910
- } else if (values["tmux-vertical"] || values["tmux-v"]) {
911
- tmuxDirection = "vertical";
912
- } else if (values["tmux-horizontal"] || values["tmux-h"]) {
913
- tmuxDirection = "horizontal";
914
- }
915
- if ([openShell, execCommand !== void 0, tmuxOption].filter(Boolean).length > 1) {
916
- exitWithError(
917
- "Cannot use --shell, --exec, and --tmux options together",
918
- exitCodes.validationError
919
- );
920
- }
921
- if (tmuxOption && !await isInsideTmux()) {
922
- exitWithError(
923
- "The --tmux option can only be used inside a tmux session",
924
- exitCodes.validationError
925
- );
926
- }
927
- try {
928
- const gitRoot = await getGitRoot();
929
- let filesToCopy = [];
930
- const configResult = await loadConfig(gitRoot);
931
- if (isOk(configResult)) {
932
- if (configResult.value.postCreate?.copyFiles) {
933
- filesToCopy = [...configResult.value.postCreate.copyFiles];
934
- }
935
- } else {
936
- if (configResult.error instanceof ConfigValidationError) {
937
- output.warn(`Configuration warning: ${configResult.error.message}`);
938
- } else if (configResult.error instanceof ConfigParseError) {
939
- output.warn(`Configuration warning: ${configResult.error.message}`);
940
- }
941
- }
942
- if (copyFileOptions && copyFileOptions.length > 0) {
943
- const cliFiles = Array.isArray(copyFileOptions) ? copyFileOptions : [copyFileOptions];
944
- filesToCopy = [.../* @__PURE__ */ new Set([...filesToCopy, ...cliFiles])];
945
- }
946
- const result = await createWorktree(gitRoot, worktreeName, {
947
- copyFiles: filesToCopy.length > 0 ? filesToCopy : void 0
948
- });
949
- if (isErr(result)) {
950
- const exitCode = result.error instanceof WorktreeAlreadyExistsError ? exitCodes.validationError : exitCodes.generalError;
951
- exitWithError(result.error.message, exitCode);
952
- }
953
- output.log(result.value.message);
954
- if (result.value.copyError) {
955
- output.error(
956
- `
957
- Warning: Failed to copy some files: ${result.value.copyError}`
958
- );
959
- }
960
- if (isOk(configResult) && configResult.value.postCreate?.commands) {
961
- const commands2 = configResult.value.postCreate.commands;
962
- output.log("\nRunning post-create commands...");
963
- for (const command2 of commands2) {
964
- output.log(`Executing: ${command2}`);
965
- const shell = process.env.SHELL || "/bin/sh";
966
- const cmdResult = await execInWorktree(gitRoot, worktreeName, [
967
- shell,
968
- "-c",
969
- command2
970
- ]);
971
- if (isErr(cmdResult)) {
972
- output.error(`Failed to execute command: ${cmdResult.error.message}`);
973
- const exitCode = "exitCode" in cmdResult.error ? cmdResult.error.exitCode ?? exitCodes.generalError : exitCodes.generalError;
974
- exitWithError(`Post-create command failed: ${command2}`, exitCode);
975
- }
976
- if (cmdResult.value.exitCode !== 0) {
977
- exitWithError(
978
- `Post-create command failed: ${command2}`,
979
- cmdResult.value.exitCode
980
- );
981
- }
982
- }
983
- }
984
- if (execCommand && isOk(result)) {
985
- output.log(
986
- `
987
- Executing command in worktree '${worktreeName}': ${execCommand}`
988
- );
989
- const shell = process.env.SHELL || "/bin/sh";
990
- const execResult = await execInWorktree(
991
- gitRoot,
992
- worktreeName,
993
- [shell, "-c", execCommand],
994
- { interactive: true }
995
- );
996
- if (isErr(execResult)) {
997
- output.error(execResult.error.message);
998
- const exitCode = "exitCode" in execResult.error ? execResult.error.exitCode ?? exitCodes.generalError : exitCodes.generalError;
999
- exitWithError("", exitCode);
1000
- }
1001
- process.exit(execResult.value.exitCode ?? 0);
1002
- }
1003
- if (openShell && isOk(result)) {
1004
- output.log(
1005
- `
1006
- Entering worktree '${worktreeName}' at ${result.value.path}`
1007
- );
1008
- output.log("Type 'exit' to return to your original directory\n");
1009
- const shellResult = await shellInWorktree(gitRoot, worktreeName);
1010
- if (isErr(shellResult)) {
1011
- output.error(shellResult.error.message);
1012
- const exitCode = "exitCode" in shellResult.error ? shellResult.error.exitCode ?? exitCodes.generalError : exitCodes.generalError;
1013
- exitWithError("", exitCode);
1014
- }
1015
- process.exit(shellResult.value.exitCode ?? 0);
1016
- }
1017
- if (tmuxDirection && isOk(result)) {
1018
- output.log(
1019
- `
1020
- Opening worktree '${worktreeName}' in tmux ${tmuxDirection === "new" ? "window" : "pane"}...`
1021
- );
1022
- const shell = process.env.SHELL || "/bin/sh";
1023
- const tmuxResult = await executeTmuxCommand({
1024
- direction: tmuxDirection,
1025
- command: shell,
1026
- cwd: result.value.path,
1027
- env: getPhantomEnv(worktreeName, result.value.path),
1028
- windowName: tmuxDirection === "new" ? worktreeName : void 0
1029
- });
1030
- if (isErr(tmuxResult)) {
1031
- output.error(tmuxResult.error.message);
1032
- const exitCode = "exitCode" in tmuxResult.error ? tmuxResult.error.exitCode ?? exitCodes.generalError : exitCodes.generalError;
1033
- exitWithError("", exitCode);
1034
- }
1035
- }
1036
- exitWithSuccess();
1037
- } catch (error) {
1038
- exitWithError(
1039
- error instanceof Error ? error.message : String(error),
1040
- exitCodes.generalError
1041
- );
1042
- }
1043
- }
1044
-
1045
- // src/cli/handlers/delete.ts
1046
- import { parseArgs as parseArgs3 } from "node:util";
1047
-
1048
- // src/core/git/libs/list-worktrees.ts
1049
- async function listWorktrees(gitRoot) {
1050
- const { stdout: stdout2 } = await executeGitCommand([
1051
- "worktree",
1052
- "list",
1053
- "--porcelain"
1054
- ]);
1055
- const worktrees = [];
1056
- let currentWorktree = {};
1057
- const lines = stdout2.split("\n").filter((line) => line.length > 0);
1058
- for (const line of lines) {
1059
- if (line.startsWith("worktree ")) {
1060
- if (currentWorktree.path) {
1061
- worktrees.push(currentWorktree);
1062
- }
1063
- currentWorktree = {
1064
- path: line.substring("worktree ".length),
1065
- isLocked: false,
1066
- isPrunable: false
1067
- };
1068
- } else if (line.startsWith("HEAD ")) {
1069
- currentWorktree.head = line.substring("HEAD ".length);
1070
- } else if (line.startsWith("branch ")) {
1071
- const fullBranch = line.substring("branch ".length);
1072
- currentWorktree.branch = fullBranch.startsWith("refs/heads/") ? fullBranch.substring("refs/heads/".length) : fullBranch;
1073
- } else if (line === "detached") {
1074
- currentWorktree.branch = "(detached HEAD)";
1075
- } else if (line === "locked") {
1076
- currentWorktree.isLocked = true;
1077
- } else if (line === "prunable") {
1078
- currentWorktree.isPrunable = true;
1079
- }
1080
- }
1081
- if (currentWorktree.path) {
1082
- worktrees.push(currentWorktree);
1083
- }
1084
- return worktrees;
1085
- }
1086
-
1087
- // src/core/git/libs/get-current-worktree.ts
1088
- async function getCurrentWorktree(gitRoot) {
1089
- try {
1090
- const { stdout: currentPath } = await executeGitCommand([
1091
- "rev-parse",
1092
- "--show-toplevel"
1093
- ]);
1094
- const currentPathTrimmed = currentPath.trim();
1095
- const worktrees = await listWorktrees(gitRoot);
1096
- const currentWorktree = worktrees.find(
1097
- (wt) => wt.path === currentPathTrimmed
1098
- );
1099
- if (!currentWorktree || currentWorktree.path === gitRoot) {
1100
- return null;
1101
- }
1102
- return currentWorktree.branch;
1103
- } catch {
1104
- return null;
1105
- }
1106
- }
1107
-
1108
- // src/core/worktree/delete.ts
1109
- async function getWorktreeStatus(worktreePath) {
1110
- try {
1111
- const { stdout: stdout2 } = await executeGitCommandInDirectory(worktreePath, [
1112
- "status",
1113
- "--porcelain"
1114
- ]);
1115
- if (stdout2) {
1116
- return {
1117
- hasUncommittedChanges: true,
1118
- changedFiles: stdout2.split("\n").length
1119
- };
1120
- }
1121
- } catch {
1122
- }
1123
- return {
1124
- hasUncommittedChanges: false,
1125
- changedFiles: 0
1126
- };
1127
- }
1128
- async function removeWorktree(gitRoot, worktreePath, force = false) {
1129
- try {
1130
- await executeGitCommand(["worktree", "remove", worktreePath], {
1131
- cwd: gitRoot
1132
- });
1133
- } catch (error) {
1134
- try {
1135
- await executeGitCommand(["worktree", "remove", "--force", worktreePath], {
1136
- cwd: gitRoot
1137
- });
1138
- } catch {
1139
- throw new Error("Failed to remove worktree");
1140
- }
1141
- }
1142
- }
1143
- async function deleteBranch(gitRoot, branchName) {
1144
- try {
1145
- await executeGitCommand(["branch", "-D", branchName], { cwd: gitRoot });
1146
- return ok(true);
1147
- } catch (error) {
1148
- const errorMessage = error instanceof Error ? error.message : String(error);
1149
- return err(new GitOperationError("branch delete", errorMessage));
1150
- }
1151
- }
1152
- async function deleteWorktree(gitRoot, name, options = {}) {
1153
- const { force = false } = options;
1154
- const validation = await validateWorktreeExists(gitRoot, name);
1155
- if (isErr(validation)) {
1156
- return err(validation.error);
1157
- }
1158
- const worktreePath = validation.value.path;
1159
- const status = await getWorktreeStatus(worktreePath);
1160
- if (status.hasUncommittedChanges && !force) {
1161
- return err(
1162
- new WorktreeError(
1163
- `Worktree '${name}' has uncommitted changes (${status.changedFiles} files). Use --force to delete anyway.`
1164
- )
1165
- );
1166
- }
1167
- try {
1168
- await removeWorktree(gitRoot, worktreePath, force);
1169
- const branchName = name;
1170
- const branchResult = await deleteBranch(gitRoot, branchName);
1171
- let message;
1172
- if (isOk(branchResult)) {
1173
- message = `Deleted worktree '${name}' and its branch '${branchName}'`;
1174
- } else {
1175
- message = `Deleted worktree '${name}'`;
1176
- message += `
1177
- Note: Branch '${branchName}' could not be deleted: ${branchResult.error.message}`;
1178
- }
1179
- if (status.hasUncommittedChanges) {
1180
- message = `Warning: Worktree '${name}' had uncommitted changes (${status.changedFiles} files)
1181
- ${message}`;
1182
- }
1183
- return ok({
1184
- message,
1185
- hasUncommittedChanges: status.hasUncommittedChanges,
1186
- changedFiles: status.hasUncommittedChanges ? status.changedFiles : void 0
1187
- });
1188
- } catch (error) {
1189
- const errorMessage = error instanceof Error ? error.message : String(error);
1190
- return err(new GitOperationError("worktree remove", errorMessage));
1191
- }
1192
- }
1193
-
1194
- // src/core/utils/fzf.ts
1195
- import { spawn } from "node:child_process";
1196
- async function selectWithFzf(items, options = {}) {
1197
- return new Promise((resolve2) => {
1198
- const args2 = [];
1199
- if (options.prompt) {
1200
- args2.push("--prompt", options.prompt);
1201
- }
1202
- if (options.header) {
1203
- args2.push("--header", options.header);
1204
- }
1205
- if (options.previewCommand) {
1206
- args2.push("--preview", options.previewCommand);
1207
- }
1208
- const fzf = spawn("fzf", args2, {
1209
- stdio: ["pipe", "pipe", "pipe"]
1210
- });
1211
- let result = "";
1212
- let errorOutput = "";
1213
- fzf.stdout.on("data", (data) => {
1214
- result += data.toString();
1215
- });
1216
- if (fzf.stderr) {
1217
- fzf.stderr.on("data", (data) => {
1218
- errorOutput += data.toString();
1219
- });
1220
- }
1221
- fzf.on("error", (error) => {
1222
- if (error.message.includes("ENOENT")) {
1223
- resolve2(
1224
- err(new Error("fzf command not found. Please install fzf first."))
1225
- );
1226
- } else {
1227
- resolve2(err(error));
1228
- }
1229
- });
1230
- fzf.on("close", (code) => {
1231
- if (code === 0) {
1232
- const selected = result.trim();
1233
- resolve2(ok(selected || null));
1234
- } else if (code === 1) {
1235
- resolve2(ok(null));
1236
- } else if (code === 130) {
1237
- resolve2(ok(null));
1238
- } else {
1239
- resolve2(err(new Error(`fzf exited with code ${code}: ${errorOutput}`)));
1240
- }
1241
- });
1242
- fzf.stdin.write(items.join("\n"));
1243
- fzf.stdin.end();
1244
- });
1245
- }
1246
-
1247
- // src/core/worktree/list.ts
1248
- async function getWorktreeStatus2(worktreePath) {
1249
- try {
1250
- const { stdout: stdout2 } = await executeGitCommandInDirectory(worktreePath, [
1251
- "status",
1252
- "--porcelain"
1253
- ]);
1254
- return !stdout2;
1255
- } catch {
1256
- return true;
1257
- }
1258
- }
1259
- async function listWorktrees2(gitRoot) {
1260
- try {
1261
- const gitWorktrees = await listWorktrees(gitRoot);
1262
- const phantomDir = getPhantomDirectory(gitRoot);
1263
- const phantomWorktrees = gitWorktrees.filter(
1264
- (worktree) => worktree.path.startsWith(phantomDir)
1265
- );
1266
- if (phantomWorktrees.length === 0) {
1267
- return ok({
1268
- worktrees: [],
1269
- message: "No worktrees found"
1270
- });
1271
- }
1272
- const worktrees = await Promise.all(
1273
- phantomWorktrees.map(async (gitWorktree) => {
1274
- const name = gitWorktree.path.substring(phantomDir.length + 1);
1275
- const isClean = await getWorktreeStatus2(gitWorktree.path);
1276
- return {
1277
- name,
1278
- path: gitWorktree.path,
1279
- branch: gitWorktree.branch || "(detached HEAD)",
1280
- isClean
1281
- };
1282
- })
1283
- );
1284
- return ok({
1285
- worktrees
1286
- });
1287
- } catch (error) {
1288
- const errorMessage = error instanceof Error ? error.message : String(error);
1289
- throw new Error(`Failed to list worktrees: ${errorMessage}`);
1290
- }
1291
- }
1292
-
1293
- // src/core/worktree/select.ts
1294
- async function selectWorktreeWithFzf(gitRoot) {
1295
- const listResult = await listWorktrees2(gitRoot);
1296
- if (isErr(listResult)) {
1297
- return listResult;
1298
- }
1299
- const { worktrees } = listResult.value;
1300
- if (worktrees.length === 0) {
1301
- return {
1302
- ok: true,
1303
- value: null
1304
- };
1305
- }
1306
- const list = worktrees.map((wt) => {
1307
- const branchInfo = wt.branch ? `(${wt.branch})` : "";
1308
- const status = !wt.isClean ? " [dirty]" : "";
1309
- return `${wt.name} ${branchInfo}${status}`;
1310
- });
1311
- const fzfResult = await selectWithFzf(list, {
1312
- prompt: "Select worktree> ",
1313
- header: "Git Worktrees"
1314
- });
1315
- if (isErr(fzfResult)) {
1316
- return fzfResult;
1317
- }
1318
- if (!fzfResult.value) {
1319
- return {
1320
- ok: true,
1321
- value: null
1322
- };
1323
- }
1324
- const selectedName = fzfResult.value.split(" ")[0];
1325
- const selectedWorktree = worktrees.find((wt) => wt.name === selectedName);
1326
- if (!selectedWorktree) {
1327
- return {
1328
- ok: false,
1329
- error: new Error("Selected worktree not found")
1330
- };
1331
- }
1332
- return {
1333
- ok: true,
1334
- value: {
1335
- name: selectedWorktree.name,
1336
- branch: selectedWorktree.branch,
1337
- isClean: selectedWorktree.isClean
1338
- }
1339
- };
1340
- }
1341
-
1342
- // src/cli/handlers/delete.ts
1343
- async function deleteHandler(args2) {
1344
- const { values, positionals } = parseArgs3({
1345
- args: args2,
1346
- options: {
1347
- force: {
1348
- type: "boolean",
1349
- short: "f"
1350
- },
1351
- current: {
1352
- type: "boolean"
1353
- },
1354
- fzf: {
1355
- type: "boolean",
1356
- default: false
1357
- }
1358
- },
1359
- strict: true,
1360
- allowPositionals: true
1361
- });
1362
- const deleteCurrent = values.current ?? false;
1363
- const useFzf = values.fzf ?? false;
1364
- if (positionals.length === 0 && !deleteCurrent && !useFzf) {
1365
- exitWithError(
1366
- "Please provide a worktree name to delete, use --current to delete the current worktree, or use --fzf for interactive selection",
1367
- exitCodes.validationError
1368
- );
1369
- }
1370
- if ((positionals.length > 0 || useFzf) && deleteCurrent) {
1371
- exitWithError(
1372
- "Cannot specify --current with a worktree name or --fzf option",
1373
- exitCodes.validationError
1374
- );
1375
- }
1376
- if (positionals.length > 0 && useFzf) {
1377
- exitWithError(
1378
- "Cannot specify both a worktree name and --fzf option",
1379
- exitCodes.validationError
1380
- );
1381
- }
1382
- const forceDelete = values.force ?? false;
1383
- try {
1384
- const gitRoot = await getGitRoot();
1385
- let worktreeName;
1386
- if (deleteCurrent) {
1387
- const currentWorktree = await getCurrentWorktree(gitRoot);
1388
- if (!currentWorktree) {
1389
- exitWithError(
1390
- "Not in a worktree directory. The --current option can only be used from within a worktree.",
1391
- exitCodes.validationError
1392
- );
1393
- }
1394
- worktreeName = currentWorktree;
1395
- } else if (useFzf) {
1396
- const selectResult = await selectWorktreeWithFzf(gitRoot);
1397
- if (isErr(selectResult)) {
1398
- exitWithError(selectResult.error.message, exitCodes.generalError);
1399
- }
1400
- if (!selectResult.value) {
1401
- exitWithSuccess();
1402
- }
1403
- worktreeName = selectResult.value.name;
1404
- } else {
1405
- worktreeName = positionals[0];
1406
- }
1407
- const result = await deleteWorktree(gitRoot, worktreeName, {
1408
- force: forceDelete
1409
- });
1410
- if (isErr(result)) {
1411
- const exitCode = result.error instanceof WorktreeNotFoundError ? exitCodes.validationError : result.error instanceof WorktreeError && result.error.message.includes("uncommitted changes") ? exitCodes.validationError : exitCodes.generalError;
1412
- exitWithError(result.error.message, exitCode);
1413
- }
1414
- output.log(result.value.message);
1415
- exitWithSuccess();
1416
- } catch (error) {
1417
- exitWithError(
1418
- error instanceof Error ? error.message : String(error),
1419
- exitCodes.generalError
1420
- );
1421
- }
1422
- }
1423
-
1424
- // src/cli/handlers/exec.ts
1425
- import { parseArgs as parseArgs4 } from "node:util";
1426
- async function execHandler(args2) {
1427
- const { positionals, values } = parseArgs4({
1428
- args: args2,
1429
- options: {
1430
- fzf: {
1431
- type: "boolean",
1432
- default: false
1433
- },
1434
- tmux: {
1435
- type: "boolean",
1436
- short: "t"
1437
- },
1438
- "tmux-vertical": {
1439
- type: "boolean"
1440
- },
1441
- "tmux-v": {
1442
- type: "boolean"
1443
- },
1444
- "tmux-horizontal": {
1445
- type: "boolean"
1446
- },
1447
- "tmux-h": {
1448
- type: "boolean"
1449
- }
1450
- },
1451
- strict: true,
1452
- allowPositionals: true
1453
- });
1454
- const useFzf = values.fzf ?? false;
1455
- const tmuxOption = values.tmux || values["tmux-vertical"] || values["tmux-v"] || values["tmux-horizontal"] || values["tmux-h"];
1456
- let tmuxDirection;
1457
- if (values.tmux) {
1458
- tmuxDirection = "new";
1459
- } else if (values["tmux-vertical"] || values["tmux-v"]) {
1460
- tmuxDirection = "vertical";
1461
- } else if (values["tmux-horizontal"] || values["tmux-h"]) {
1462
- tmuxDirection = "horizontal";
1463
- }
1464
- let commandArgs;
1465
- if (useFzf) {
1466
- if (positionals.length < 1) {
1467
- exitWithError(
1468
- "Usage: phantom exec --fzf <command> [args...]",
1469
- exitCodes.validationError
1470
- );
1471
- }
1472
- commandArgs = positionals;
1473
- } else {
1474
- if (positionals.length < 2) {
1475
- exitWithError(
1476
- "Usage: phantom exec <worktree-name> <command> [args...]",
1477
- exitCodes.validationError
1478
- );
1479
- }
1480
- commandArgs = positionals.slice(1);
1481
- }
1482
- try {
1483
- const gitRoot = await getGitRoot();
1484
- if (tmuxOption && !await isInsideTmux()) {
1485
- exitWithError(
1486
- "The --tmux option can only be used inside a tmux session",
1487
- exitCodes.validationError
1488
- );
1489
- }
1490
- let worktreeName;
1491
- if (useFzf) {
1492
- const selectResult = await selectWorktreeWithFzf(gitRoot);
1493
- if (isErr(selectResult)) {
1494
- exitWithError(selectResult.error.message, exitCodes.generalError);
1495
- }
1496
- if (!selectResult.value) {
1497
- exitWithSuccess();
1498
- }
1499
- worktreeName = selectResult.value.name;
1500
- } else {
1501
- worktreeName = positionals[0];
1502
- }
1503
- const validation = await validateWorktreeExists(gitRoot, worktreeName);
1504
- if (isErr(validation)) {
1505
- exitWithError(validation.error.message, exitCodes.generalError);
1506
- }
1507
- if (tmuxDirection) {
1508
- output.log(
1509
- `Executing command in worktree '${worktreeName}' in tmux ${tmuxDirection === "new" ? "window" : "pane"}...`
1510
- );
1511
- const [command2, ...args3] = commandArgs;
1512
- const tmuxResult = await executeTmuxCommand({
1513
- direction: tmuxDirection,
1514
- command: command2,
1515
- args: args3,
1516
- cwd: validation.value.path,
1517
- env: getPhantomEnv(worktreeName, validation.value.path),
1518
- windowName: tmuxDirection === "new" ? worktreeName : void 0
1519
- });
1520
- if (isErr(tmuxResult)) {
1521
- output.error(tmuxResult.error.message);
1522
- const exitCode = "exitCode" in tmuxResult.error ? tmuxResult.error.exitCode ?? exitCodes.generalError : exitCodes.generalError;
1523
- exitWithError("", exitCode);
1524
- }
1525
- exitWithSuccess();
1526
- }
1527
- const result = await execInWorktree(
1528
- gitRoot,
1529
- worktreeName,
1530
- commandArgs,
1531
- { interactive: true }
1532
- );
1533
- if (isErr(result)) {
1534
- const exitCode = result.error instanceof WorktreeNotFoundError ? exitCodes.notFound : result.error.exitCode || exitCodes.generalError;
1535
- exitWithError(result.error.message, exitCode);
1536
- }
1537
- process.exit(result.value.exitCode);
1538
- } catch (error) {
1539
- exitWithError(
1540
- error instanceof Error ? error.message : String(error),
1541
- exitCodes.generalError
1542
- );
1543
- }
1544
- }
1545
-
1546
- // src/cli/handlers/list.ts
1547
- import { parseArgs as parseArgs5 } from "node:util";
1548
- async function listHandler(args2 = []) {
1549
- const { values } = parseArgs5({
1550
- args: args2,
1551
- options: {
1552
- fzf: {
1553
- type: "boolean",
1554
- default: false
1555
- },
1556
- names: {
1557
- type: "boolean",
1558
- default: false
1559
- }
1560
- },
1561
- strict: true,
1562
- allowPositionals: false
1563
- });
1564
- try {
1565
- const gitRoot = await getGitRoot();
1566
- if (values.fzf) {
1567
- const selectResult = await selectWorktreeWithFzf(gitRoot);
1568
- if (isErr(selectResult)) {
1569
- exitWithError(selectResult.error.message, exitCodes.generalError);
1570
- }
1571
- if (selectResult.value) {
1572
- output.log(selectResult.value.name);
1573
- }
1574
- } else {
1575
- const result = await listWorktrees2(gitRoot);
1576
- if (isErr(result)) {
1577
- exitWithError("Failed to list worktrees", exitCodes.generalError);
1578
- }
1579
- const { worktrees, message } = result.value;
1580
- if (worktrees.length === 0) {
1581
- if (!values.names) {
1582
- output.log(message || "No worktrees found.");
1583
- }
1584
- process.exit(exitCodes.success);
1585
- }
1586
- if (values.names) {
1587
- for (const worktree of worktrees) {
1588
- output.log(worktree.name);
1589
- }
1590
- } else {
1591
- const maxNameLength = Math.max(
1592
- ...worktrees.map((wt) => wt.name.length)
1593
- );
1594
- for (const worktree of worktrees) {
1595
- const paddedName = worktree.name.padEnd(maxNameLength + 2);
1596
- const branchInfo = worktree.branch ? `(${worktree.branch})` : "";
1597
- const status = !worktree.isClean ? " [dirty]" : "";
1598
- output.log(`${paddedName} ${branchInfo}${status}`);
1599
- }
1600
- }
1601
- }
1602
- process.exit(exitCodes.success);
1603
- } catch (error) {
1604
- exitWithError(
1605
- error instanceof Error ? error.message : String(error),
1606
- exitCodes.generalError
1607
- );
1608
- }
1609
- }
1610
-
1611
- // src/cli/handlers/shell.ts
1612
- import { parseArgs as parseArgs6 } from "node:util";
1613
- async function shellHandler(args2) {
1614
- const { positionals, values } = parseArgs6({
1615
- args: args2,
1616
- options: {
1617
- fzf: {
1618
- type: "boolean",
1619
- default: false
1620
- },
1621
- tmux: {
1622
- type: "boolean",
1623
- short: "t"
1624
- },
1625
- "tmux-vertical": {
1626
- type: "boolean"
1627
- },
1628
- "tmux-v": {
1629
- type: "boolean"
1630
- },
1631
- "tmux-horizontal": {
1632
- type: "boolean"
1633
- },
1634
- "tmux-h": {
1635
- type: "boolean"
1636
- }
1637
- },
1638
- strict: true,
1639
- allowPositionals: true
1640
- });
1641
- const useFzf = values.fzf ?? false;
1642
- const tmuxOption = values.tmux || values["tmux-vertical"] || values["tmux-v"] || values["tmux-horizontal"] || values["tmux-h"];
1643
- let tmuxDirection;
1644
- if (values.tmux) {
1645
- tmuxDirection = "new";
1646
- } else if (values["tmux-vertical"] || values["tmux-v"]) {
1647
- tmuxDirection = "vertical";
1648
- } else if (values["tmux-horizontal"] || values["tmux-h"]) {
1649
- tmuxDirection = "horizontal";
1650
- }
1651
- if (positionals.length === 0 && !useFzf) {
1652
- exitWithError(
1653
- "Usage: phantom shell <worktree-name> or phantom shell --fzf",
1654
- exitCodes.validationError
1655
- );
1656
- }
1657
- if (positionals.length > 0 && useFzf) {
1658
- exitWithError(
1659
- "Cannot specify both a worktree name and --fzf option",
1660
- exitCodes.validationError
1661
- );
1662
- }
1663
- let worktreeName;
1664
- try {
1665
- const gitRoot = await getGitRoot();
1666
- if (tmuxOption && !await isInsideTmux()) {
1667
- exitWithError(
1668
- "The --tmux option can only be used inside a tmux session",
1669
- exitCodes.validationError
1670
- );
1671
- }
1672
- if (useFzf) {
1673
- const selectResult = await selectWorktreeWithFzf(gitRoot);
1674
- if (isErr(selectResult)) {
1675
- exitWithError(selectResult.error.message, exitCodes.generalError);
1676
- }
1677
- if (!selectResult.value) {
1678
- exitWithSuccess();
1679
- }
1680
- worktreeName = selectResult.value.name;
1681
- } else {
1682
- worktreeName = positionals[0];
1683
- }
1684
- const validation = await validateWorktreeExists(gitRoot, worktreeName);
1685
- if (isErr(validation)) {
1686
- exitWithError(validation.error.message, exitCodes.generalError);
1687
- }
1688
- if (tmuxDirection) {
1689
- output.log(
1690
- `Opening worktree '${worktreeName}' in tmux ${tmuxDirection === "new" ? "window" : "pane"}...`
1691
- );
1692
- const shell = process.env.SHELL || "/bin/sh";
1693
- const tmuxResult = await executeTmuxCommand({
1694
- direction: tmuxDirection,
1695
- command: shell,
1696
- cwd: validation.value.path,
1697
- env: getPhantomEnv(worktreeName, validation.value.path),
1698
- windowName: tmuxDirection === "new" ? worktreeName : void 0
1699
- });
1700
- if (isErr(tmuxResult)) {
1701
- output.error(tmuxResult.error.message);
1702
- const exitCode = "exitCode" in tmuxResult.error ? tmuxResult.error.exitCode ?? exitCodes.generalError : exitCodes.generalError;
1703
- exitWithError("", exitCode);
1704
- }
1705
- exitWithSuccess();
1706
- }
1707
- output.log(
1708
- `Entering worktree '${worktreeName}' at ${validation.value.path}`
1709
- );
1710
- output.log("Type 'exit' to return to your original directory\n");
1711
- const result = await shellInWorktree(gitRoot, worktreeName);
1712
- if (isErr(result)) {
1713
- const exitCode = result.error instanceof WorktreeNotFoundError ? exitCodes.notFound : result.error.exitCode || exitCodes.generalError;
1714
- exitWithError(result.error.message, exitCode);
1715
- }
1716
- process.exit(result.value.exitCode);
1717
- } catch (error) {
1718
- exitWithError(
1719
- error instanceof Error ? error.message : String(error),
1720
- exitCodes.generalError
1721
- );
1722
- }
1723
- }
1724
-
1725
- // src/cli/handlers/version.ts
1726
- import { parseArgs as parseArgs7 } from "node:util";
1727
-
1728
- // package.json
1729
- var package_default = {
1730
- name: "@aku11i/phantom",
1731
- packageManager: "pnpm@10.8.1",
1732
- version: "1.0.0",
1733
- description: "A powerful CLI tool for managing Git worktrees for parallel development",
1734
- keywords: [
1735
- "git",
1736
- "worktree",
1737
- "cli",
1738
- "phantom",
1739
- "workspace",
1740
- "development",
1741
- "parallel"
1742
- ],
1743
- homepage: "https://github.com/aku11i/phantom#readme",
1744
- bugs: {
1745
- url: "https://github.com/aku11i/phantom/issues"
1746
- },
1747
- repository: {
1748
- type: "git",
1749
- url: "git+https://github.com/aku11i/phantom.git"
1750
- },
1751
- license: "MIT",
1752
- author: "aku11i",
1753
- type: "module",
1754
- bin: {
1755
- phantom: "./dist/phantom.js"
1756
- },
1757
- scripts: {
1758
- start: "node ./src/bin/phantom.ts",
1759
- phantom: "node ./src/bin/phantom.ts",
1760
- build: "node build.ts",
1761
- typecheck: "tsgo --noEmit",
1762
- test: 'node --test --experimental-strip-types --experimental-test-module-mocks "src/**/*.test.js"',
1763
- "test:coverage": 'node --experimental-test-coverage --test --experimental-strip-types --experimental-test-module-mocks "src/**/*.test.js"',
1764
- "test:file": "node --test --experimental-strip-types --experimental-test-module-mocks",
1765
- lint: "biome check .",
1766
- fix: "biome check --write .",
1767
- ready: "pnpm fix && pnpm typecheck && pnpm test",
1768
- "ready:check": "pnpm lint && pnpm typecheck && pnpm test",
1769
- prepublishOnly: "pnpm ready:check && pnpm build"
1770
- },
1771
- engines: {
1772
- node: ">=22.0.0"
1773
- },
1774
- files: [
1775
- "dist/",
1776
- "README.md",
1777
- "LICENSE"
1778
- ],
1779
- devDependencies: {
1780
- "@biomejs/biome": "^1.9.4",
1781
- "@types/node": "^22.15.29",
1782
- "@typescript/native-preview": "7.0.0-dev.20250602.1",
1783
- esbuild: "^0.25.5",
1784
- typescript: "^5.8.3"
1785
- }
1786
- };
1787
-
1788
- // src/core/version.ts
1789
- function getVersion() {
1790
- return package_default.version;
1791
- }
1792
-
1793
- // src/cli/handlers/version.ts
1794
- function versionHandler(args2 = []) {
1795
- parseArgs7({
1796
- args: args2,
1797
- options: {},
1798
- strict: true,
1799
- allowPositionals: false
1800
- });
1801
- const version = getVersion();
1802
- output.log(`Phantom v${version}`);
1803
- exitWithSuccess();
1804
- }
1805
-
1806
- // src/cli/handlers/where.ts
1807
- import { parseArgs as parseArgs8 } from "node:util";
1808
-
1809
- // src/core/worktree/where.ts
1810
- async function whereWorktree(gitRoot, name) {
1811
- const validation = await validateWorktreeExists(gitRoot, name);
1812
- if (isErr(validation)) {
1813
- return err(validation.error);
1814
- }
1815
- return ok({
1816
- path: validation.value.path
1817
- });
1818
- }
1819
-
1820
- // src/cli/handlers/where.ts
1821
- async function whereHandler(args2) {
1822
- const { positionals, values } = parseArgs8({
1823
- args: args2,
1824
- options: {
1825
- fzf: {
1826
- type: "boolean",
1827
- default: false
1828
- }
1829
- },
1830
- strict: true,
1831
- allowPositionals: true
1832
- });
1833
- const useFzf = values.fzf ?? false;
1834
- if (positionals.length === 0 && !useFzf) {
1835
- exitWithError(
1836
- "Usage: phantom where <worktree-name> or phantom where --fzf",
1837
- exitCodes.validationError
1838
- );
1839
- }
1840
- if (positionals.length > 0 && useFzf) {
1841
- exitWithError(
1842
- "Cannot specify both a worktree name and --fzf option",
1843
- exitCodes.validationError
1844
- );
1845
- }
1846
- let worktreeName;
1847
- let gitRoot;
1848
- try {
1849
- gitRoot = await getGitRoot();
1850
- } catch (error) {
1851
- exitWithError(
1852
- error instanceof Error ? error.message : String(error),
1853
- exitCodes.generalError
1854
- );
1855
- }
1856
- if (useFzf) {
1857
- const selectResult = await selectWorktreeWithFzf(gitRoot);
1858
- if (isErr(selectResult)) {
1859
- exitWithError(selectResult.error.message, exitCodes.generalError);
1860
- }
1861
- if (!selectResult.value) {
1862
- exitWithSuccess();
1863
- }
1864
- worktreeName = selectResult.value.name;
1865
- } else {
1866
- worktreeName = positionals[0];
1867
- }
1868
- const result = await whereWorktree(gitRoot, worktreeName);
1869
- if (isErr(result)) {
1870
- exitWithError(result.error.message, exitCodes.notFound);
1871
- }
1872
- output.log(result.value.path);
1873
- exitWithSuccess();
1874
- }
1875
-
1876
- // src/cli/help.ts
1877
- import { stdout } from "node:process";
1878
- var HelpFormatter = class {
1879
- width;
1880
- indent = " ";
1881
- constructor() {
1882
- this.width = stdout.columns || 80;
1883
- }
1884
- formatMainHelp(commands2) {
1885
- const lines = [];
1886
- lines.push(this.bold("Phantom - Git Worktree Manager"));
1887
- lines.push("");
1888
- lines.push(
1889
- this.dim(
1890
- "A CLI tool for managing Git worktrees with enhanced functionality"
1891
- )
1892
- );
1893
- lines.push("");
1894
- lines.push(this.section("USAGE"));
1895
- lines.push(`${this.indent}phantom <command> [options]`);
1896
- lines.push("");
1897
- lines.push(this.section("COMMANDS"));
1898
- const maxNameLength = Math.max(...commands2.map((cmd) => cmd.name.length));
1899
- for (const cmd of commands2) {
1900
- const paddedName = cmd.name.padEnd(maxNameLength + 2);
1901
- lines.push(`${this.indent}${this.cyan(paddedName)}${cmd.description}`);
1902
- }
1903
- lines.push("");
1904
- lines.push(this.section("GLOBAL OPTIONS"));
1905
- const helpOption = "-h, --help";
1906
- const versionOption = "-v, --version";
1907
- const globalOptionWidth = Math.max(helpOption.length, versionOption.length) + 2;
1908
- lines.push(
1909
- `${this.indent}${this.cyan(helpOption.padEnd(globalOptionWidth))}Show help`
1910
- );
1911
- lines.push(
1912
- `${this.indent}${this.cyan(versionOption.padEnd(globalOptionWidth))}Show version`
1913
- );
1914
- lines.push("");
1915
- lines.push(
1916
- this.dim(
1917
- "Run 'phantom <command> --help' for more information on a command."
1918
- )
1919
- );
1920
- return lines.join("\n");
1921
- }
1922
- formatCommandHelp(help) {
1923
- const lines = [];
1924
- lines.push(this.bold(`phantom ${help.name}`));
1925
- lines.push(this.dim(help.description));
1926
- lines.push("");
1927
- lines.push(this.section("USAGE"));
1928
- lines.push(`${this.indent}${help.usage}`);
1929
- lines.push("");
1930
- if (help.options && help.options.length > 0) {
1931
- lines.push(this.section("OPTIONS"));
1932
- const maxOptionLength = Math.max(
1933
- ...help.options.map((opt) => this.formatOptionName(opt).length)
1934
- );
1935
- for (const option of help.options) {
1936
- const optionName = this.formatOptionName(option);
1937
- const paddedName = optionName.padEnd(maxOptionLength + 2);
1938
- const description = this.wrapText(
1939
- option.description,
1940
- maxOptionLength + 4
1941
- );
1942
- lines.push(`${this.indent}${this.cyan(paddedName)}${description[0]}`);
1943
- for (let i = 1; i < description.length; i++) {
1944
- lines.push(
1945
- `${this.indent}${" ".repeat(maxOptionLength + 2)}${description[i]}`
1946
- );
1947
- }
1948
- if (option.example) {
1949
- const exampleIndent = " ".repeat(maxOptionLength + 4);
1950
- lines.push(
1951
- `${this.indent}${exampleIndent}${this.dim(`Example: ${option.example}`)}`
1952
- );
1953
- }
1954
- }
1955
- lines.push("");
1956
- }
1957
- if (help.examples && help.examples.length > 0) {
1958
- lines.push(this.section("EXAMPLES"));
1959
- for (const example of help.examples) {
1960
- lines.push(`${this.indent}${this.dim(example.description)}`);
1961
- lines.push(`${this.indent}${this.indent}$ ${example.command}`);
1962
- lines.push("");
1963
- }
1964
- }
1965
- if (help.notes && help.notes.length > 0) {
1966
- lines.push(this.section("NOTES"));
1967
- for (const note of help.notes) {
1968
- const wrappedNote = this.wrapText(note, 2);
1969
- for (const line of wrappedNote) {
1970
- lines.push(`${this.indent}${line}`);
1971
- }
1972
- }
1973
- lines.push("");
1974
- }
1975
- return lines.join("\n");
1976
- }
1977
- formatOptionName(option) {
1978
- const parts = [];
1979
- if (option.short) {
1980
- parts.push(`-${option.short},`);
1981
- }
1982
- parts.push(`--${option.name}`);
1983
- if (option.type === "string") {
1984
- parts.push(option.multiple ? "<value>..." : "<value>");
1985
- }
1986
- return parts.join(" ");
1987
- }
1988
- wrapText(text, indent) {
1989
- const maxWidth = this.width - indent - 2;
1990
- const words = text.split(" ");
1991
- const lines = [];
1992
- let currentLine = "";
1993
- for (const word of words) {
1994
- if (currentLine.length + word.length + 1 > maxWidth) {
1995
- lines.push(currentLine);
1996
- currentLine = word;
1997
- } else {
1998
- currentLine = currentLine ? `${currentLine} ${word}` : word;
1999
- }
2000
- }
2001
- if (currentLine) {
2002
- lines.push(currentLine);
2003
- }
2004
- return lines;
2005
- }
2006
- section(text) {
2007
- return this.bold(text);
2008
- }
2009
- bold(text) {
2010
- return `\x1B[1m${text}\x1B[0m`;
2011
- }
2012
- dim(text) {
2013
- return `\x1B[2m${text}\x1B[0m`;
2014
- }
2015
- cyan(text) {
2016
- return `\x1B[36m${text}\x1B[0m`;
2017
- }
2018
- };
2019
- var helpFormatter = new HelpFormatter();
2020
-
2021
- // src/cli/help/attach.ts
2022
- var attachHelp = {
2023
- name: "attach",
2024
- description: "Attach to an existing branch by creating a new worktree",
2025
- usage: "phantom attach <worktree-name> <branch-name> [options]",
2026
- options: [
2027
- {
2028
- name: "shell",
2029
- short: "s",
2030
- type: "boolean",
2031
- description: "Open an interactive shell in the worktree after attaching"
2032
- },
2033
- {
2034
- name: "exec",
2035
- short: "x",
2036
- type: "string",
2037
- description: "Execute a command in the worktree after attaching",
2038
- example: "--exec 'git pull'"
2039
- }
2040
- ],
2041
- examples: [
2042
- {
2043
- description: "Attach to an existing branch",
2044
- command: "phantom attach review-pr main"
2045
- },
2046
- {
2047
- description: "Attach to a remote branch and open a shell",
2048
- command: "phantom attach hotfix origin/hotfix-v1.2 --shell"
2049
- },
2050
- {
2051
- description: "Attach to a branch and pull latest changes",
2052
- command: "phantom attach staging origin/staging --exec 'git pull'"
2053
- }
2054
- ],
2055
- notes: [
2056
- "The branch must already exist (locally or remotely)",
2057
- "If attaching to a remote branch, it will be checked out locally",
2058
- "Only one of --shell or --exec options can be used at a time"
2059
- ]
2060
- };
2061
-
2062
- // src/cli/help/completion.ts
2063
- var completionHelp = {
2064
- name: "completion",
2065
- usage: "phantom completion <shell>",
2066
- description: "Generate shell completion scripts for fish or zsh",
2067
- examples: [
2068
- {
2069
- command: "phantom completion fish > ~/.config/fish/completions/phantom.fish",
2070
- description: "Generate and install Fish completion"
2071
- },
2072
- {
2073
- command: "phantom completion fish | source",
2074
- description: "Load Fish completion in current session"
2075
- },
2076
- {
2077
- command: "phantom completion zsh > ~/.zsh/completions/_phantom",
2078
- description: "Generate and install Zsh completion"
2079
- },
2080
- {
2081
- command: 'eval "$(phantom completion zsh)"',
2082
- description: "Load Zsh completion in current session"
2083
- }
2084
- ],
2085
- notes: [
2086
- "Supported shells: fish, zsh",
2087
- "After installing completions, you may need to restart your shell or source the completion file",
2088
- "For Fish: completions are loaded automatically from ~/.config/fish/completions/",
2089
- "For Zsh: ensure the completion file is in a directory in your $fpath"
2090
- ]
2091
- };
2092
-
2093
- // src/cli/help/create.ts
2094
- var createHelp = {
2095
- name: "create",
2096
- description: "Create a new Git worktree",
2097
- usage: "phantom create <name> [options]",
2098
- options: [
2099
- {
2100
- name: "shell",
2101
- short: "s",
2102
- type: "boolean",
2103
- description: "Open an interactive shell in the new worktree after creation"
2104
- },
2105
- {
2106
- name: "exec",
2107
- short: "x",
2108
- type: "string",
2109
- description: "Execute a command in the new worktree after creation",
2110
- example: "--exec 'npm install'"
2111
- },
2112
- {
2113
- name: "tmux",
2114
- short: "t",
2115
- type: "boolean",
2116
- description: "Open the worktree in a new tmux window (requires being inside tmux)"
2117
- },
2118
- {
2119
- name: "tmux-vertical",
2120
- type: "boolean",
2121
- description: "Open the worktree in a vertical tmux pane (requires being inside tmux)"
2122
- },
2123
- {
2124
- name: "tmux-horizontal",
2125
- type: "boolean",
2126
- description: "Open the worktree in a horizontal tmux pane (requires being inside tmux)"
2127
- },
2128
- {
2129
- name: "copy-file",
2130
- type: "string",
2131
- multiple: true,
2132
- description: "Copy specified files from the current worktree to the new one. Can be used multiple times",
2133
- example: "--copy-file .env --copy-file config.local.json"
2134
- }
2135
- ],
2136
- examples: [
2137
- {
2138
- description: "Create a new worktree named 'feature-auth'",
2139
- command: "phantom create feature-auth"
2140
- },
2141
- {
2142
- description: "Create a worktree and open a shell in it",
2143
- command: "phantom create bugfix-123 --shell"
2144
- },
2145
- {
2146
- description: "Create a worktree and run npm install",
2147
- command: "phantom create new-feature --exec 'npm install'"
2148
- },
2149
- {
2150
- description: "Create a worktree in a new tmux window",
2151
- command: "phantom create experiment --tmux"
2152
- },
2153
- {
2154
- description: "Create a worktree and copy environment files",
2155
- command: "phantom create staging --copy-file .env --copy-file database.yml"
2156
- }
2157
- ],
2158
- notes: [
2159
- "The worktree name will be used as the branch name",
2160
- "Only one of --shell, --exec, or --tmux options can be used at a time",
2161
- "File copying can also be configured in phantom.config.json"
2162
- ]
2163
- };
2164
-
2165
- // src/cli/help/delete.ts
2166
- var deleteHelp = {
2167
- name: "delete",
2168
- description: "Delete a Git worktree",
2169
- usage: "phantom delete <name> [options]",
2170
- options: [
2171
- {
2172
- name: "force",
2173
- short: "f",
2174
- type: "boolean",
2175
- description: "Force deletion even if the worktree has uncommitted or unpushed changes"
2176
- },
2177
- {
2178
- name: "--current",
2179
- type: "boolean",
2180
- description: "Delete the current worktree"
2181
- },
2182
- {
2183
- name: "--fzf",
2184
- type: "boolean",
2185
- description: "Use fzf for interactive selection"
2186
- }
2187
- ],
2188
- examples: [
2189
- {
2190
- description: "Delete a worktree",
2191
- command: "phantom delete feature-auth"
2192
- },
2193
- {
2194
- description: "Force delete a worktree with uncommitted changes",
2195
- command: "phantom delete experimental --force"
2196
- },
2197
- {
2198
- description: "Delete the current worktree",
2199
- command: "phantom delete --current"
2200
- },
2201
- {
2202
- description: "Delete a worktree with interactive fzf selection",
2203
- command: "phantom delete --fzf"
2204
- }
2205
- ],
2206
- notes: [
2207
- "By default, deletion will fail if the worktree has uncommitted changes",
2208
- "The associated branch will also be deleted if it's not checked out elsewhere",
2209
- "With --fzf, you can interactively select the worktree to delete"
2210
- ]
2211
- };
2212
-
2213
- // src/cli/help/exec.ts
2214
- var execHelp = {
2215
- name: "exec",
2216
- description: "Execute a command in a worktree directory",
2217
- usage: "phantom exec [options] <worktree-name> <command> [args...]",
2218
- options: [
2219
- {
2220
- name: "--fzf",
2221
- type: "boolean",
2222
- description: "Use fzf for interactive worktree selection"
2223
- },
2224
- {
2225
- name: "--tmux, -t",
2226
- type: "boolean",
2227
- description: "Execute command in new tmux window"
2228
- },
2229
- {
2230
- name: "--tmux-vertical, --tmux-v",
2231
- type: "boolean",
2232
- description: "Execute command in vertical split pane"
2233
- },
2234
- {
2235
- name: "--tmux-horizontal, --tmux-h",
2236
- type: "boolean",
2237
- description: "Execute command in horizontal split pane"
2238
- }
2239
- ],
2240
- examples: [
2241
- {
2242
- description: "Run npm test in a worktree",
2243
- command: "phantom exec feature-auth npm test"
2244
- },
2245
- {
2246
- description: "Check git status in a worktree",
2247
- command: "phantom exec bugfix-123 git status"
2248
- },
2249
- {
2250
- description: "Run a complex command with arguments",
2251
- command: "phantom exec staging npm run build -- --production"
2252
- },
2253
- {
2254
- description: "Execute with interactive selection",
2255
- command: "phantom exec --fzf npm run dev"
2256
- },
2257
- {
2258
- description: "Run dev server in new tmux window",
2259
- command: "phantom exec --tmux feature-auth npm run dev"
2260
- },
2261
- {
2262
- description: "Run tests in vertical split pane",
2263
- command: "phantom exec --tmux-v feature-auth npm test"
2264
- },
2265
- {
2266
- description: "Interactive selection with tmux",
2267
- command: "phantom exec --fzf --tmux npm run dev"
2268
- }
2269
- ],
2270
- notes: [
2271
- "The command is executed with the worktree directory as the working directory",
2272
- "All arguments after the worktree name are passed to the command",
2273
- "The exit code of the executed command is preserved",
2274
- "With --fzf, select the worktree interactively before executing the command",
2275
- "Tmux options require being inside a tmux session"
2276
- ]
2277
- };
2278
-
2279
- // src/cli/help/list.ts
2280
- var listHelp = {
2281
- name: "list",
2282
- description: "List all Git worktrees",
2283
- usage: "phantom list [options]",
2284
- options: [
2285
- {
2286
- name: "--fzf",
2287
- type: "boolean",
2288
- description: "Use fzf for interactive selection"
2289
- },
2290
- {
2291
- name: "--names",
2292
- type: "boolean",
2293
- description: "Output only worktree names (for scripts and completion)"
2294
- }
2295
- ],
2296
- examples: [
2297
- {
2298
- description: "List all worktrees",
2299
- command: "phantom list"
2300
- },
2301
- {
2302
- description: "List worktrees with interactive fzf selection",
2303
- command: "phantom list --fzf"
2304
- },
2305
- {
2306
- description: "List only worktree names",
2307
- command: "phantom list --names"
2308
- }
2309
- ],
2310
- notes: [
2311
- "Shows all worktrees with their paths and associated branches",
2312
- "The main worktree is marked as '(bare)' if using a bare repository",
2313
- "With --fzf, outputs only the selected worktree name",
2314
- "Use --names for shell completion scripts and automation"
2315
- ]
2316
- };
2317
-
2318
- // src/cli/help/shell.ts
2319
- var shellHelp = {
2320
- name: "shell",
2321
- description: "Open an interactive shell in a worktree directory",
2322
- usage: "phantom shell <worktree-name> [options]",
2323
- options: [
2324
- {
2325
- name: "--fzf",
2326
- type: "boolean",
2327
- description: "Use fzf for interactive selection"
2328
- },
2329
- {
2330
- name: "--tmux, -t",
2331
- type: "boolean",
2332
- description: "Open shell in new tmux window"
2333
- },
2334
- {
2335
- name: "--tmux-vertical, --tmux-v",
2336
- type: "boolean",
2337
- description: "Open shell in vertical split pane"
2338
- },
2339
- {
2340
- name: "--tmux-horizontal, --tmux-h",
2341
- type: "boolean",
2342
- description: "Open shell in horizontal split pane"
2343
- }
2344
- ],
2345
- examples: [
2346
- {
2347
- description: "Open a shell in a worktree",
2348
- command: "phantom shell feature-auth"
2349
- },
2350
- {
2351
- description: "Open a shell with interactive fzf selection",
2352
- command: "phantom shell --fzf"
2353
- },
2354
- {
2355
- description: "Open a shell in a new tmux window",
2356
- command: "phantom shell feature-auth --tmux"
2357
- },
2358
- {
2359
- description: "Open a shell in a vertical tmux pane",
2360
- command: "phantom shell feature-auth --tmux-v"
2361
- },
2362
- {
2363
- description: "Interactive selection with tmux",
2364
- command: "phantom shell --fzf --tmux"
2365
- }
2366
- ],
2367
- notes: [
2368
- "Uses your default shell from the SHELL environment variable",
2369
- "The shell starts with the worktree directory as the working directory",
2370
- "Type 'exit' to return to your original directory",
2371
- "With --fzf, you can interactively select the worktree to enter",
2372
- "Tmux options require being inside a tmux session"
2373
- ]
2374
- };
2375
-
2376
- // src/cli/help/version.ts
2377
- var versionHelp = {
2378
- name: "version",
2379
- description: "Display phantom version information",
2380
- usage: "phantom version",
2381
- examples: [
2382
- {
2383
- description: "Show version",
2384
- command: "phantom version"
2385
- }
2386
- ],
2387
- notes: ["Also accessible via 'phantom --version' or 'phantom -v'"]
2388
- };
2389
-
2390
- // src/cli/help/where.ts
2391
- var whereHelp = {
2392
- name: "where",
2393
- description: "Output the filesystem path of a specific worktree",
2394
- usage: "phantom where <worktree-name> [options]",
2395
- options: [
2396
- {
2397
- name: "--fzf",
2398
- type: "boolean",
2399
- description: "Use fzf for interactive selection"
2400
- }
2401
- ],
2402
- examples: [
2403
- {
2404
- description: "Get the path of a worktree",
2405
- command: "phantom where feature-auth"
2406
- },
2407
- {
2408
- description: "Change directory to a worktree",
2409
- command: "cd $(phantom where staging)"
2410
- },
2411
- {
2412
- description: "Get path with interactive fzf selection",
2413
- command: "phantom where --fzf"
2414
- },
2415
- {
2416
- description: "Change directory using fzf selection",
2417
- command: "cd $(phantom where --fzf)"
2418
- }
2419
- ],
2420
- notes: [
2421
- "Outputs only the path, making it suitable for use in scripts",
2422
- "Exits with an error code if the worktree doesn't exist",
2423
- "With --fzf, you can interactively select the worktree"
2424
- ]
2425
- };
2426
-
2427
- // src/bin/phantom.ts
2428
- var commands = [
2429
- {
2430
- name: "create",
2431
- description: "Create a new Git worktree (phantom)",
2432
- handler: createHandler,
2433
- help: createHelp
2434
- },
2435
- {
2436
- name: "attach",
2437
- description: "Attach to an existing branch by creating a new worktree",
2438
- handler: attachHandler,
2439
- help: attachHelp
2440
- },
2441
- {
2442
- name: "list",
2443
- description: "List all Git worktrees (phantoms)",
2444
- handler: listHandler,
2445
- help: listHelp
2446
- },
2447
- {
2448
- name: "where",
2449
- description: "Output the filesystem path of a specific worktree",
2450
- handler: whereHandler,
2451
- help: whereHelp
2452
- },
2453
- {
2454
- name: "delete",
2455
- description: "Delete a Git worktree (phantom)",
2456
- handler: deleteHandler,
2457
- help: deleteHelp
2458
- },
2459
- {
2460
- name: "exec",
2461
- description: "Execute a command in a worktree directory",
2462
- handler: execHandler,
2463
- help: execHelp
2464
- },
2465
- {
2466
- name: "shell",
2467
- description: "Open an interactive shell in a worktree directory",
2468
- handler: shellHandler,
2469
- help: shellHelp
2470
- },
2471
- {
2472
- name: "version",
2473
- description: "Display phantom version information",
2474
- handler: versionHandler,
2475
- help: versionHelp
2476
- },
2477
- {
2478
- name: "completion",
2479
- description: "Generate shell completion scripts",
2480
- handler: completionHandler,
2481
- help: completionHelp
2482
- }
2483
- ];
2484
- function printHelp(commands2) {
2485
- const simpleCommands = commands2.map((cmd) => ({
2486
- name: cmd.name,
2487
- description: cmd.description
2488
- }));
2489
- console.log(helpFormatter.formatMainHelp(simpleCommands));
2490
- }
2491
- function findCommand(args2, commands2) {
2492
- if (args2.length === 0) {
2493
- return { command: null, remainingArgs: [] };
2494
- }
2495
- const [cmdName, ...rest] = args2;
2496
- const command2 = commands2.find((cmd) => cmd.name === cmdName);
2497
- if (!command2) {
2498
- return { command: null, remainingArgs: args2 };
2499
- }
2500
- if (command2.subcommands && rest.length > 0) {
2501
- const { command: subcommand, remainingArgs: remainingArgs2 } = findCommand(
2502
- rest,
2503
- command2.subcommands
2504
- );
2505
- if (subcommand) {
2506
- return { command: subcommand, remainingArgs: remainingArgs2 };
2507
- }
2508
- }
2509
- return { command: command2, remainingArgs: rest };
2510
- }
2511
- var args = argv.slice(2);
2512
- if (args.length === 0 || args[0] === "-h" || args[0] === "--help") {
2513
- printHelp(commands);
2514
- exit2(0);
2515
- }
2516
- if (args[0] === "--version" || args[0] === "-v") {
2517
- versionHandler();
2518
- exit2(0);
2519
- }
2520
- var { command, remainingArgs } = findCommand(args, commands);
2521
- if (!command || !command.handler) {
2522
- console.error(`Error: Unknown command '${args.join(" ")}'
2523
- `);
2524
- printHelp(commands);
2525
- exit2(1);
2526
- }
2527
- if (remainingArgs.includes("--help") || remainingArgs.includes("-h")) {
2528
- if (command.help) {
2529
- console.log(helpFormatter.formatCommandHelp(command.help));
2530
- } else {
2531
- console.log(`Help not available for command '${command.name}'`);
2532
- }
2533
- exit2(0);
2534
- }
2535
- try {
2536
- await command.handler(remainingArgs);
2537
- } catch (error) {
2538
- console.error(
2539
- "Error:",
2540
- error instanceof Error ? error.message : String(error)
2541
- );
2542
- exit2(1);
2543
- }
2544
- //# sourceMappingURL=phantom.js.map