mirror of
https://github.com/AutoMaker-Org/automaker.git
synced 2026-02-04 09:13:08 +00:00
default editor fixes, fix bug with worktree panel not showing
This commit is contained in:
@@ -101,6 +101,7 @@ export function WorktreeSelector({
|
|||||||
const [isLoadingBranches, setIsLoadingBranches] = useState(false);
|
const [isLoadingBranches, setIsLoadingBranches] = useState(false);
|
||||||
const [branchFilter, setBranchFilter] = useState("");
|
const [branchFilter, setBranchFilter] = useState("");
|
||||||
const [runningDevServers, setRunningDevServers] = useState<Map<string, DevServerInfo>>(new Map());
|
const [runningDevServers, setRunningDevServers] = useState<Map<string, DevServerInfo>>(new Map());
|
||||||
|
const [defaultEditorName, setDefaultEditorName] = useState<string>("Editor");
|
||||||
const currentWorktree = useAppStore((s) => s.getCurrentWorktree(projectPath));
|
const currentWorktree = useAppStore((s) => s.getCurrentWorktree(projectPath));
|
||||||
const setCurrentWorktree = useAppStore((s) => s.setCurrentWorktree);
|
const setCurrentWorktree = useAppStore((s) => s.setCurrentWorktree);
|
||||||
const setWorktreesInStore = useAppStore((s) => s.setWorktrees);
|
const setWorktreesInStore = useAppStore((s) => s.setWorktrees);
|
||||||
@@ -145,6 +146,21 @@ export function WorktreeSelector({
|
|||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
const fetchDefaultEditor = useCallback(async () => {
|
||||||
|
try {
|
||||||
|
const api = getElectronAPI();
|
||||||
|
if (!api?.worktree?.getDefaultEditor) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const result = await api.worktree.getDefaultEditor();
|
||||||
|
if (result.success && result.result?.editorName) {
|
||||||
|
setDefaultEditorName(result.result.editorName);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Failed to fetch default editor:", error);
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
const fetchBranches = useCallback(async (worktreePath: string) => {
|
const fetchBranches = useCallback(async (worktreePath: string) => {
|
||||||
setIsLoadingBranches(true);
|
setIsLoadingBranches(true);
|
||||||
try {
|
try {
|
||||||
@@ -169,7 +185,8 @@ export function WorktreeSelector({
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
fetchWorktrees();
|
fetchWorktrees();
|
||||||
fetchDevServers();
|
fetchDevServers();
|
||||||
}, [fetchWorktrees, fetchDevServers]);
|
fetchDefaultEditor();
|
||||||
|
}, [fetchWorktrees, fetchDevServers, fetchDefaultEditor]);
|
||||||
|
|
||||||
// Refresh when refreshTrigger changes (but skip the initial render)
|
// Refresh when refreshTrigger changes (but skip the initial render)
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -442,10 +459,6 @@ export function WorktreeSelector({
|
|||||||
? worktrees.find((w) => w.path === currentWorktree)
|
? worktrees.find((w) => w.path === currentWorktree)
|
||||||
: worktrees.find((w) => w.isMain);
|
: worktrees.find((w) => w.isMain);
|
||||||
|
|
||||||
if (worktrees.length === 0 && !isLoading) {
|
|
||||||
// No git repo or loading
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Render a worktree tab with branch selector (for main) and actions dropdown
|
// Render a worktree tab with branch selector (for main) and actions dropdown
|
||||||
const renderWorktreeTab = (worktree: WorktreeInfo) => {
|
const renderWorktreeTab = (worktree: WorktreeInfo) => {
|
||||||
@@ -707,7 +720,7 @@ export function WorktreeSelector({
|
|||||||
className="text-xs"
|
className="text-xs"
|
||||||
>
|
>
|
||||||
<ExternalLink className="w-3.5 h-3.5 mr-2" />
|
<ExternalLink className="w-3.5 h-3.5 mr-2" />
|
||||||
Open in Editor
|
Open in {defaultEditorName}
|
||||||
</DropdownMenuItem>
|
</DropdownMenuItem>
|
||||||
<DropdownMenuSeparator />
|
<DropdownMenuSeparator />
|
||||||
{/* Commit changes */}
|
{/* Commit changes */}
|
||||||
|
|||||||
@@ -1222,7 +1222,19 @@ function createMockWorktreeAPI(): WorktreeAPI {
|
|||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
result: {
|
result: {
|
||||||
message: `Opened ${worktreePath} in editor`,
|
message: `Opened ${worktreePath} in VS Code`,
|
||||||
|
editorName: "VS Code",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
|
getDefaultEditor: async () => {
|
||||||
|
console.log("[Mock] Getting default editor");
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
result: {
|
||||||
|
editorName: "VS Code",
|
||||||
|
editorCommand: "code",
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -612,6 +612,8 @@ export class HttpApiClient implements ElectronAPI {
|
|||||||
this.post("/api/worktree/switch-branch", { worktreePath, branchName }),
|
this.post("/api/worktree/switch-branch", { worktreePath, branchName }),
|
||||||
openInEditor: (worktreePath: string) =>
|
openInEditor: (worktreePath: string) =>
|
||||||
this.post("/api/worktree/open-in-editor", { worktreePath }),
|
this.post("/api/worktree/open-in-editor", { worktreePath }),
|
||||||
|
getDefaultEditor: () =>
|
||||||
|
this.get("/api/worktree/default-editor"),
|
||||||
initGit: (projectPath: string) =>
|
initGit: (projectPath: string) =>
|
||||||
this.post("/api/worktree/init-git", { projectPath }),
|
this.post("/api/worktree/init-git", { projectPath }),
|
||||||
activate: (projectPath: string, worktreePath: string | null) =>
|
activate: (projectPath: string, worktreePath: string | null) =>
|
||||||
|
|||||||
11
apps/app/src/types/electron.d.ts
vendored
11
apps/app/src/types/electron.d.ts
vendored
@@ -796,6 +796,17 @@ export interface WorktreeAPI {
|
|||||||
success: boolean;
|
success: boolean;
|
||||||
result?: {
|
result?: {
|
||||||
message: string;
|
message: string;
|
||||||
|
editorName?: string;
|
||||||
|
};
|
||||||
|
error?: string;
|
||||||
|
}>;
|
||||||
|
|
||||||
|
// Get the default code editor name
|
||||||
|
getDefaultEditor: () => Promise<{
|
||||||
|
success: boolean;
|
||||||
|
result?: {
|
||||||
|
editorName: string;
|
||||||
|
editorCommand: string;
|
||||||
};
|
};
|
||||||
error?: string;
|
error?: string;
|
||||||
}>;
|
}>;
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ import { createPullHandler } from "./routes/pull.js";
|
|||||||
import { createCheckoutBranchHandler } from "./routes/checkout-branch.js";
|
import { createCheckoutBranchHandler } from "./routes/checkout-branch.js";
|
||||||
import { createListBranchesHandler } from "./routes/list-branches.js";
|
import { createListBranchesHandler } from "./routes/list-branches.js";
|
||||||
import { createSwitchBranchHandler } from "./routes/switch-branch.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 { createInitGitHandler } from "./routes/init-git.js";
|
||||||
import { createActivateHandler } from "./routes/activate.js";
|
import { createActivateHandler } from "./routes/activate.js";
|
||||||
import { createMigrateHandler } from "./routes/migrate.js";
|
import { createMigrateHandler } from "./routes/migrate.js";
|
||||||
@@ -47,6 +47,7 @@ export function createWorktreeRoutes(): Router {
|
|||||||
router.post("/list-branches", createListBranchesHandler());
|
router.post("/list-branches", createListBranchesHandler());
|
||||||
router.post("/switch-branch", createSwitchBranchHandler());
|
router.post("/switch-branch", createSwitchBranchHandler());
|
||||||
router.post("/open-in-editor", createOpenInEditorHandler());
|
router.post("/open-in-editor", createOpenInEditorHandler());
|
||||||
|
router.get("/default-editor", createGetDefaultEditorHandler());
|
||||||
router.post("/init-git", createInitGitHandler());
|
router.post("/init-git", createInitGitHandler());
|
||||||
router.post("/activate", createActivateHandler());
|
router.post("/activate", createActivateHandler());
|
||||||
router.post("/migrate", createMigrateHandler());
|
router.post("/migrate", createMigrateHandler());
|
||||||
|
|||||||
@@ -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";
|
import type { Request, Response } from "express";
|
||||||
@@ -9,6 +10,89 @@ import { getErrorMessage, logError } from "../common.js";
|
|||||||
|
|
||||||
const execAsync = promisify(exec);
|
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() {
|
export function createOpenInEditorHandler() {
|
||||||
return async (req: Request, res: Response): Promise<void> => {
|
return async (req: Request, res: Response): Promise<void> => {
|
||||||
try {
|
try {
|
||||||
@@ -24,47 +108,43 @@ export function createOpenInEditorHandler() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to open in VS Code
|
const editor = await detectDefaultEditor();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await execAsync(`code "${worktreePath}"`);
|
await execAsync(`${editor.command} "${worktreePath}"`);
|
||||||
res.json({
|
res.json({
|
||||||
success: true,
|
success: true,
|
||||||
result: {
|
result: {
|
||||||
message: `Opened ${worktreePath} in VS Code`,
|
message: `Opened ${worktreePath} in ${editor.name}`,
|
||||||
|
editorName: editor.name,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
} catch {
|
} catch (editorError) {
|
||||||
// If 'code' command fails, try 'cursor' (for Cursor editor)
|
// If the detected editor fails, try opening in default file manager as fallback
|
||||||
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;
|
const platform = process.platform;
|
||||||
let openCommand: string;
|
let openCommand: string;
|
||||||
|
let fallbackName: string;
|
||||||
|
|
||||||
if (platform === "darwin") {
|
if (platform === "darwin") {
|
||||||
openCommand = `open "${worktreePath}"`;
|
openCommand = `open "${worktreePath}"`;
|
||||||
|
fallbackName = "Finder";
|
||||||
} else if (platform === "win32") {
|
} else if (platform === "win32") {
|
||||||
openCommand = `explorer "${worktreePath}"`;
|
openCommand = `explorer "${worktreePath}"`;
|
||||||
|
fallbackName = "Explorer";
|
||||||
} else {
|
} else {
|
||||||
openCommand = `xdg-open "${worktreePath}"`;
|
openCommand = `xdg-open "${worktreePath}"`;
|
||||||
|
fallbackName = "File Manager";
|
||||||
}
|
}
|
||||||
|
|
||||||
await execAsync(openCommand);
|
await execAsync(openCommand);
|
||||||
res.json({
|
res.json({
|
||||||
success: true,
|
success: true,
|
||||||
result: {
|
result: {
|
||||||
message: `Opened ${worktreePath} in file manager`,
|
message: `Opened ${worktreePath} in ${fallbackName}`,
|
||||||
|
editorName: fallbackName,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logError(error, "Open in editor failed");
|
logError(error, "Open in editor failed");
|
||||||
res.status(500).json({ success: false, error: getErrorMessage(error) });
|
res.status(500).json({ success: false, error: getErrorMessage(error) });
|
||||||
|
|||||||
Reference in New Issue
Block a user