default editor fixes, fix bug with worktree panel not showing

This commit is contained in:
Cody Seibert
2025-12-16 12:35:36 -05:00
parent 9509c8ea00
commit d103d0aa45
6 changed files with 160 additions and 41 deletions

View File

@@ -19,7 +19,7 @@ import { createPullHandler } from "./routes/pull.js";
import { createCheckoutBranchHandler } from "./routes/checkout-branch.js";
import { createListBranchesHandler } from "./routes/list-branches.js";
import { createSwitchBranchHandler } from "./routes/switch-branch.js";
import { createOpenInEditorHandler } from "./routes/open-in-editor.js";
import { createOpenInEditorHandler, createGetDefaultEditorHandler } from "./routes/open-in-editor.js";
import { createInitGitHandler } from "./routes/init-git.js";
import { createActivateHandler } from "./routes/activate.js";
import { createMigrateHandler } from "./routes/migrate.js";
@@ -47,6 +47,7 @@ export function createWorktreeRoutes(): Router {
router.post("/list-branches", createListBranchesHandler());
router.post("/switch-branch", createSwitchBranchHandler());
router.post("/open-in-editor", createOpenInEditorHandler());
router.get("/default-editor", createGetDefaultEditorHandler());
router.post("/init-git", createInitGitHandler());
router.post("/activate", createActivateHandler());
router.post("/migrate", createMigrateHandler());

View File

@@ -1,5 +1,6 @@
/**
* POST /open-in-editor endpoint - Open a worktree directory in VS Code
* POST /open-in-editor endpoint - Open a worktree directory in the default code editor
* GET /default-editor endpoint - Get the name of the default code editor
*/
import type { Request, Response } from "express";
@@ -9,6 +10,89 @@ import { getErrorMessage, logError } from "../common.js";
const execAsync = promisify(exec);
// Editor detection with caching
interface EditorInfo {
name: string;
command: string;
}
let cachedEditor: EditorInfo | null = null;
/**
* Detect which code editor is available on the system
*/
async function detectDefaultEditor(): Promise<EditorInfo> {
// Return cached result if available
if (cachedEditor) {
return cachedEditor;
}
// Try Cursor first (if user has Cursor, they probably prefer it)
try {
await execAsync("which cursor || where cursor");
cachedEditor = { name: "Cursor", command: "cursor" };
return cachedEditor;
} catch {
// Cursor not found
}
// Try VS Code
try {
await execAsync("which code || where code");
cachedEditor = { name: "VS Code", command: "code" };
return cachedEditor;
} catch {
// VS Code not found
}
// Try Zed
try {
await execAsync("which zed || where zed");
cachedEditor = { name: "Zed", command: "zed" };
return cachedEditor;
} catch {
// Zed not found
}
// Try Sublime Text
try {
await execAsync("which subl || where subl");
cachedEditor = { name: "Sublime Text", command: "subl" };
return cachedEditor;
} catch {
// Sublime not found
}
// Fallback to file manager
const platform = process.platform;
if (platform === "darwin") {
cachedEditor = { name: "Finder", command: "open" };
} else if (platform === "win32") {
cachedEditor = { name: "Explorer", command: "explorer" };
} else {
cachedEditor = { name: "File Manager", command: "xdg-open" };
}
return cachedEditor;
}
export function createGetDefaultEditorHandler() {
return async (_req: Request, res: Response): Promise<void> => {
try {
const editor = await detectDefaultEditor();
res.json({
success: true,
result: {
editorName: editor.name,
editorCommand: editor.command,
},
});
} catch (error) {
logError(error, "Get default editor failed");
res.status(500).json({ success: false, error: getErrorMessage(error) });
}
};
}
export function createOpenInEditorHandler() {
return async (req: Request, res: Response): Promise<void> => {
try {
@@ -24,46 +108,42 @@ export function createOpenInEditorHandler() {
return;
}
// Try to open in VS Code
const editor = await detectDefaultEditor();
try {
await execAsync(`code "${worktreePath}"`);
await execAsync(`${editor.command} "${worktreePath}"`);
res.json({
success: true,
result: {
message: `Opened ${worktreePath} in VS Code`,
message: `Opened ${worktreePath} in ${editor.name}`,
editorName: editor.name,
},
});
} catch {
// If 'code' command fails, try 'cursor' (for Cursor editor)
try {
await execAsync(`cursor "${worktreePath}"`);
res.json({
success: true,
result: {
message: `Opened ${worktreePath} in Cursor`,
},
});
} catch {
// If both fail, try opening in default file manager
const platform = process.platform;
let openCommand: string;
} catch (editorError) {
// If the detected editor fails, try opening in default file manager as fallback
const platform = process.platform;
let openCommand: string;
let fallbackName: string;
if (platform === "darwin") {
openCommand = `open "${worktreePath}"`;
} else if (platform === "win32") {
openCommand = `explorer "${worktreePath}"`;
} else {
openCommand = `xdg-open "${worktreePath}"`;
}
await execAsync(openCommand);
res.json({
success: true,
result: {
message: `Opened ${worktreePath} in file manager`,
},
});
if (platform === "darwin") {
openCommand = `open "${worktreePath}"`;
fallbackName = "Finder";
} else if (platform === "win32") {
openCommand = `explorer "${worktreePath}"`;
fallbackName = "Explorer";
} else {
openCommand = `xdg-open "${worktreePath}"`;
fallbackName = "File Manager";
}
await execAsync(openCommand);
res.json({
success: true,
result: {
message: `Opened ${worktreePath} in ${fallbackName}`,
editorName: fallbackName,
},
});
}
} catch (error) {
logError(error, "Open in editor failed");