Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions src/commands/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { resolve, join, dirname, basename } from "node:path";

export async function newWorktreeHandler(
branchName: string = "main",
options: { path?: string; checkout?: boolean }
options: { path?: string; checkout?: boolean; install?: string; editor?: string }
) {
try {
// 1. Validate we're in a git repo
Expand All @@ -25,9 +25,7 @@ export async function newWorktreeHandler(
const resolvedPath = resolve(folderName);

// 3. (Optional) checkout new local branch if it doesn't exist yet
// This step is only run if user passes `--checkout`
if (options.checkout) {
// Check if branch already exists
const { stdout } = await execa("git", ["branch", "--list", branchName]);
if (!stdout) {
console.log(chalk.yellow(`Branch "${branchName}" doesn't exist locally. Creating...`));
Expand All @@ -36,20 +34,25 @@ export async function newWorktreeHandler(
console.log(chalk.green(`Branch "${branchName}" found locally.`));
}
} else {
// Ensure the branch is present, or you might want to skip this check
console.log(chalk.gray(`Using branch "${branchName}". Make sure it exists (local or remote).`));
}

// 4. Create the new worktree
console.log(chalk.blue(`Creating new worktree for branch "${branchName}" at: ${resolvedPath}`));
await execa("git", ["worktree", "add", resolvedPath, branchName]);

// 5. Open in Cursor editor
// (Assuming "cursor <path>" is how you open a folder in Cursor)
console.log(chalk.blue(`Opening ${resolvedPath} in Cursor...`));
await execa("cursor", [resolvedPath], { stdio: "inherit" });
// 5. (Optional) Install dependencies if --install flag is provided
if (options.install) {
console.log(chalk.blue(`Installing dependencies using ${options.install} in ${resolvedPath}...`));
await execa(options.install, ["install"], { cwd: resolvedPath, stdio: "inherit" });
}

// 6. Open in the specified editor (or default to "cursor")
const editorCommand = options.editor || "cursor";
console.log(chalk.blue(`Opening ${resolvedPath} in ${editorCommand}...`));
await execa(editorCommand, [resolvedPath], { stdio: "inherit" });

console.log(chalk.green(`Worktree created and opened in Cursor successfully!`));
console.log(chalk.green(`Worktree created, dependencies installed (if specified), and opened in ${editorCommand} successfully!`));
} catch (error) {
if (error instanceof Error) {
console.error(chalk.red("Failed to create new worktree:"), error.message);
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ program
.argument("[branchName]", "Name of the branch to base this worktree on")
.option("-p, --path <path>", "Relative path/folder name for new worktree")
.option("-c, --checkout", "Create new branch if it doesn't exist and checkout automatically", false)
.description("Create a new worktree for the specified branch and open it in Cursor.")
.option("-i, --install <packageManager>", "Package manager to use for installing dependencies (npm, pnpm, bun, etc.)")
.option("-e, --editor <editor>", "Editor to use for opening the worktree (e.g., code, webstorm, windsurf, etc.)")
.description("Create a new worktree for the specified branch, install dependencies if specified, and open in editor.")
.action(newWorktreeHandler);

program
Expand Down