diff --git a/src/commands/new.ts b/src/commands/new.ts index 0d060ce..c4e9c59 100644 --- a/src/commands/new.ts +++ b/src/commands/new.ts @@ -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 @@ -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...`)); @@ -36,7 +34,6 @@ 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).`)); } @@ -44,12 +41,18 @@ export async function newWorktreeHandler( 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 " 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); diff --git a/src/index.ts b/src/index.ts index 13dd2cb..aee336a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -18,7 +18,9 @@ program .argument("[branchName]", "Name of the branch to base this worktree on") .option("-p, --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 ", "Package manager to use for installing dependencies (npm, pnpm, bun, etc.)") + .option("-e, --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