-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.ts
More file actions
274 lines (250 loc) · 9.33 KB
/
main.ts
File metadata and controls
274 lines (250 loc) · 9.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import { parseArgs, type ParseArgsConfig } from "node:util";
import { startCommand } from "./packages/core/src/commands/start.ts";
import { cleanCommand } from "./packages/core/src/commands/clean.ts";
import { homeCommand } from "./packages/core/src/commands/home.ts";
import { trustCommand } from "./packages/core/src/commands/trust.ts";
import { untrustCommand } from "./packages/core/src/commands/untrust.ts";
import { verifyCommand } from "./packages/core/src/commands/verify.ts";
import { configCommand } from "./packages/core/src/commands/config.ts";
import { jumpCommand } from "./packages/core/src/commands/jump.ts";
import { upgradeCommand } from "./packages/core/src/commands/upgrade.ts";
import { shellSetupCommand } from "./packages/core/src/commands/shell-setup.ts";
import { BUILD_INFO } from "./packages/core/src/version.ts";
import { initRuntime, runtime } from "./packages/core/src/runtime/index.ts";
import {
createAppContext,
getGlobalContext,
setGlobalContext,
} from "./packages/core/src/context/index.ts";
import { handleError } from "./packages/core/src/errors/index.ts";
/**
* CLI options configuration for node:util parseArgs
*/
const parseArgsOptions: ParseArgsConfig["options"] = {
help: { type: "boolean", short: "h" },
version: { type: "boolean", short: "v" },
verbose: { type: "boolean", short: "V" },
quiet: { type: "boolean", short: "q" },
reuse: { type: "boolean" },
"no-hooks": { type: "boolean" },
"no-copy": { type: "boolean" },
"dry-run": { type: "boolean", short: "n" },
force: { type: "boolean", short: "f" },
"delete-branch": { type: "boolean" },
"keep-branch": { type: "boolean" },
check: { type: "boolean" },
base: { type: "string" },
track: { type: "boolean" },
shell: { type: "string" },
"claude-code-worktree-hook": { type: "boolean" },
};
const HELP_TEXT = `vibe - git worktree helper
Installation:
# npx (Node.js)
npx @kexi/vibe
# Homebrew (macOS)
brew install kexi/tap/vibe
# Manual build
bun build --compile --minify --outfile vibe main.ts
Usage:
vibe start <branch-name> [options] Create a new worktree with the given branch
vibe jump <branch-name> [options] Jump to an existing worktree by branch name
vibe clean [options] Remove current worktree and return to main
vibe home Return to main worktree without removing current
vibe trust Trust .vibe.toml in current repository
vibe untrust Remove trust for .vibe.toml in current repository
vibe verify Verify trust status and hash history
vibe config Show current settings
vibe upgrade [options] Check for updates and show upgrade instructions
vibe shell-setup [options] Print shell wrapper function for eval
Global Options:
-h, --help Show this help message
-v, --version Show version information
-V, --verbose Show detailed output
-q, --quiet Suppress non-essential output
Command Options:
--reuse Use existing branch instead of creating a new one (start)
--base <ref> Base branch/commit for new branch (start)
--track Set upstream tracking when using --base (start)
--no-hooks Skip pre-start and post-start hooks (start)
--no-copy Skip copying files and directories (start)
-n, --dry-run Show what would be executed without making changes (start)
-f, --force Skip confirmation prompts (clean)
--delete-branch Delete the branch after removing the worktree (clean)
--keep-branch Keep the branch after removing the worktree (clean)
--check Check for updates without showing upgrade instructions (upgrade)
--shell <name> Specify shell type: bash, zsh, fish, nushell, powershell (shell-setup)
--claude-code-worktree-hook Claude Code worktree hook mode (start, clean)
Setup:
Add this to your .zshrc:
vibe() { eval "$(command vibe "$@")" }
Examples:
vibe trust
vibe untrust
vibe verify
vibe config
vibe upgrade
vibe upgrade --check
vibe start feat/new-feature
vibe start feat/existing --reuse
vibe start feat/new-feature --base main
vibe jump feat/new-feature
vibe clean
vibe home
`;
async function main(): Promise<void> {
// Initialize runtime before using any runtime APIs
const rt = await initRuntime();
// Set up global context for dependency injection
setGlobalContext(createAppContext(rt));
const rawArgs = [...runtime.control.args];
let parsedArgs: ReturnType<typeof parseArgs>;
try {
parsedArgs = parseArgs({
args: rawArgs,
options: parseArgsOptions,
allowPositionals: true,
strict: true,
});
} catch (error) {
if (error instanceof Error) {
// Extract option name from error message for better UX
const match = error.message.match(/Unknown option '(.+)'/);
if (match) {
console.error(`Error: Unknown option '${match[1]}'`);
} else {
console.error(`Error: ${error.message}`);
}
}
console.error("Run 'vibe --help' for usage.");
runtime.control.exit(1);
}
const { values: args, positionals } = parsedArgs;
// Warn when both --verbose and --quiet are specified.
// Note: Warnings and errors always display regardless of --quiet flag,
// as they indicate issues the user should be aware of.
const hasConflictingOutputOptions = args.verbose && args.quiet;
if (hasConflictingOutputOptions) {
console.error("Warning: Both --verbose and --quiet specified. Using --quiet.");
}
if (args.version) {
console.error(`vibe ${BUILD_INFO.version}`);
console.error(`Platform: ${BUILD_INFO.platform}-${BUILD_INFO.arch} (${BUILD_INFO.target})`);
console.error(`Distribution: ${BUILD_INFO.distribution}`);
console.error(`Built: ${BUILD_INFO.buildTime} (${BUILD_INFO.buildEnv})`);
console.error();
console.error(`${BUILD_INFO.repository}#readme`);
runtime.control.exit(0);
}
const showHelp = args.help || positionals.length === 0;
if (showHelp) {
console.error(HELP_TEXT);
console.error(`${BUILD_INFO.repository}#readme`);
runtime.control.exit(0);
}
const command = String(positionals[0]);
switch (command) {
case "start": {
const branchName = String(positionals[1] ?? "");
const reuse = args.reuse === true;
const noHooks = args["no-hooks"] === true;
const noCopy = args["no-copy"] === true;
const dryRun = args["dry-run"] === true;
const verbose = args.verbose === true;
const quiet = args.quiet === true;
const base =
typeof args.base === "string" ? args.base : args.base === undefined ? undefined : "";
const baseFromEquals = rawArgs.some((arg) => arg.startsWith("--base="));
const track = args.track === true;
const worktreeHook = args["claude-code-worktree-hook"] === true;
await startCommand(branchName, {
reuse,
noHooks,
noCopy,
dryRun,
verbose,
quiet,
base,
baseFromEquals,
track,
worktreeHook,
});
break;
}
case "jump": {
const branchName = String(positionals[1] ?? "");
const verbose = args.verbose === true;
const quiet = args.quiet === true;
await jumpCommand(branchName, { verbose, quiet });
break;
}
case "clean": {
const force = args.force === true;
const deleteBranch = args["delete-branch"] === true;
const keepBranch = args["keep-branch"] === true;
const verbose = args.verbose === true;
const quiet = args.quiet === true;
// Validate mutually exclusive options
const hasMutuallyExclusiveOptions = deleteBranch && keepBranch;
if (hasMutuallyExclusiveOptions) {
console.error("Error: --delete-branch and --keep-branch cannot be used together");
runtime.control.exit(1);
}
const worktreeHookClean = args["claude-code-worktree-hook"] === true;
await cleanCommand({
force,
deleteBranch,
keepBranch,
verbose,
quiet,
worktreeHook: worktreeHookClean,
});
break;
}
case "home": {
const verbose = args.verbose === true;
const quiet = args.quiet === true;
await homeCommand({ verbose, quiet });
break;
}
case "trust":
await trustCommand();
break;
case "untrust":
await untrustCommand();
break;
case "verify":
await verifyCommand();
break;
case "config":
await configCommand();
break;
case "upgrade": {
const check = args.check === true;
const verbose = args.verbose === true;
const quiet = args.quiet === true;
await upgradeCommand({ check, verbose, quiet });
break;
}
case "shell-setup": {
const verbose = args.verbose === true;
const quiet = args.quiet === true;
const shell = typeof args.shell === "string" ? args.shell : undefined;
await shellSetupCommand({ verbose, quiet, shell });
break;
}
default:
console.error(`Unknown command: ${command}`);
runtime.control.exit(1);
}
// Explicitly exit to ensure process terminates even if there are pending event listeners
runtime.control.exit(0);
}
main().catch((error) => {
const ctx = getGlobalContext();
const exitCode = handleError(error, {}, ctx);
const shouldExit = exitCode !== 0;
if (shouldExit) {
ctx.runtime.control.exit(exitCode);
}
});