feat(web): open files in containing folder
This commit is contained in:
1 parent
8c628f1499
commit
0e69aea035
9 files changed
+261
-3
No files matched your search
@@ -81,6 +81,7 @@ export const RPC_REQUIRED_SCOPES = {
|
||||
[WS_METHODS.projectsSearchEntries]: AuthOrchestrationReadScope,
|
||||
[WS_METHODS.projectsWriteFile]: AuthOrchestrationOperateScope,
|
||||
[WS_METHODS.shellOpenInEditor]: AuthOrchestrationOperateScope,
|
||||
[WS_METHODS.shellRevealInFileManager]: AuthOrchestrationOperateScope,
|
||||
[WS_METHODS.filesystemBrowse]: AuthOrchestrationReadScope,
|
||||
[WS_METHODS.assetsCreateUrl]: AuthOrchestrationReadScope,
|
||||
[WS_METHODS.subscribeVcsStatus]: AuthOrchestrationReadScope,
|
||||
|
||||
@@ -94,6 +94,97 @@ it.effect("launches the default browser through the platform command", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it.effect("reveals files with the linux file manager", () =>
|
||||
Effect.gen(function* () {
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
const path = yield* Path.Path;
|
||||
const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-file-manager-" });
|
||||
const commandPath = path.join(binDir, "xdg-open");
|
||||
yield* fileSystem.writeFileString(commandPath, "#!/bin/sh\n");
|
||||
yield* fileSystem.chmod(commandPath, 0o755);
|
||||
|
||||
let spawned: ChildProcess.StandardCommand | undefined;
|
||||
yield* Effect.gen(function* () {
|
||||
const launcher = yield* ExternalLauncher.ExternalLauncher;
|
||||
yield* launcher.revealInFileManager({ path: "/tmp/project/src/index.ts" });
|
||||
}).pipe(
|
||||
Effect.provide(
|
||||
testLayer({
|
||||
platform: "linux",
|
||||
env: { PATH: binDir },
|
||||
onSpawn: (command) => {
|
||||
spawned = command;
|
||||
},
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
assert.ok(spawned);
|
||||
assert.equal(spawned.command, "xdg-open");
|
||||
assert.deepEqual(spawned.args, ["/tmp/project/src"]);
|
||||
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
|
||||
);
|
||||
|
||||
it.effect("reveals files with Finder", () =>
|
||||
Effect.gen(function* () {
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
const path = yield* Path.Path;
|
||||
const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-file-manager-" });
|
||||
const commandPath = path.join(binDir, "open");
|
||||
yield* fileSystem.writeFileString(commandPath, "#!/bin/sh\n");
|
||||
yield* fileSystem.chmod(commandPath, 0o755);
|
||||
|
||||
let spawned: ChildProcess.StandardCommand | undefined;
|
||||
yield* Effect.gen(function* () {
|
||||
const launcher = yield* ExternalLauncher.ExternalLauncher;
|
||||
yield* launcher.revealInFileManager({ path: "/tmp/project/src/index.ts" });
|
||||
}).pipe(
|
||||
Effect.provide(
|
||||
testLayer({
|
||||
platform: "darwin",
|
||||
env: { PATH: binDir },
|
||||
onSpawn: (command) => {
|
||||
spawned = command;
|
||||
},
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
assert.ok(spawned);
|
||||
assert.equal(spawned.command, "open");
|
||||
assert.deepEqual(spawned.args, ["-R", "/tmp/project/src/index.ts"]);
|
||||
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
|
||||
);
|
||||
|
||||
it.effect("reveals files with Windows Explorer", () =>
|
||||
Effect.gen(function* () {
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
const path = yield* Path.Path;
|
||||
const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-file-manager-" });
|
||||
yield* fileSystem.writeFileString(path.join(binDir, "explorer.CMD"), "@echo off\r\n");
|
||||
|
||||
let spawned: ChildProcess.StandardCommand | undefined;
|
||||
yield* Effect.gen(function* () {
|
||||
const launcher = yield* ExternalLauncher.ExternalLauncher;
|
||||
yield* launcher.revealInFileManager({ path: "C:\\project\\src\\index.ts" });
|
||||
}).pipe(
|
||||
Effect.provide(
|
||||
testLayer({
|
||||
platform: "win32",
|
||||
env: { PATH: binDir, PATHEXT: ".COM;.EXE;.BAT;.CMD" },
|
||||
onSpawn: (command) => {
|
||||
spawned = command;
|
||||
},
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
assert.ok(spawned);
|
||||
assert.equal(spawned.command, "explorer");
|
||||
assert.deepEqual(spawned.args, ["/select,C:\\project\\src\\index.ts"]);
|
||||
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
|
||||
);
|
||||
|
||||
it.effect("launches an installed editor with platform-safe arguments", () =>
|
||||
Effect.gen(function* () {
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
|
||||
@@ -16,6 +16,7 @@ import {
|
||||
ExternalLauncherUnsupportedEditorError,
|
||||
type EditorId,
|
||||
type LaunchEditorInput,
|
||||
type RevealInFileManagerInput,
|
||||
} from "@t3tools/contracts";
|
||||
import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
|
||||
import { isCommandAvailable, resolveSpawnCommand } from "@t3tools/shared/shell";
|
||||
@@ -45,7 +46,7 @@ export {
|
||||
ExternalLauncherUnsupportedEditorError,
|
||||
isExternalLauncherError,
|
||||
} from "@t3tools/contracts";
|
||||
export type { LaunchEditorInput };
|
||||
export type { LaunchEditorInput, RevealInFileManagerInput };
|
||||
interface EditorLaunch {
|
||||
readonly editor: EditorId;
|
||||
readonly target: string;
|
||||
@@ -337,6 +338,10 @@ export class ExternalLauncher extends Context.Service<
|
||||
* Launches the editor as a detached process so server startup is not blocked.
|
||||
*/
|
||||
readonly launchEditor: (input: LaunchEditorInput) => Effect.Effect<void, ExternalLauncherError>;
|
||||
/** Reveal a workspace file in the host file manager. */
|
||||
readonly revealInFileManager: (
|
||||
input: RevealInFileManagerInput,
|
||||
) => Effect.Effect<void, ExternalLauncherError>;
|
||||
}
|
||||
>()("t3/process/externalLauncher") {}
|
||||
|
||||
@@ -384,6 +389,33 @@ const resolveEditorLaunch = Effect.fn("resolveEditorLaunch")(function* (
|
||||
};
|
||||
});
|
||||
|
||||
const resolveFileManagerRevealLaunch = Effect.fn(
|
||||
"externalLauncher.resolveFileManagerRevealLaunch",
|
||||
)(function* (
|
||||
input: RevealInFileManagerInput,
|
||||
): Effect.fn.Return<EditorLaunch, never, Path.Path> {
|
||||
const platform = yield* HostProcessPlatform;
|
||||
const path = yield* Path.Path;
|
||||
const args =
|
||||
platform === "darwin"
|
||||
? ["-R", input.path]
|
||||
: platform === "win32"
|
||||
? [`/select,${input.path}`]
|
||||
: [path.dirname(input.path)];
|
||||
|
||||
yield* Effect.annotateCurrentSpan({
|
||||
"externalLauncher.target": input.path,
|
||||
"externalLauncher.platform": platform,
|
||||
});
|
||||
|
||||
return {
|
||||
editor: "file-manager",
|
||||
target: input.path,
|
||||
command: fileManagerCommandForPlatform(platform),
|
||||
args,
|
||||
};
|
||||
});
|
||||
|
||||
const launchAndUnref = Effect.fn("externalLauncher.launchAndUnref")(function* (
|
||||
launch: ProcessLaunch,
|
||||
onError: (cause: unknown) => ExternalLauncherError,
|
||||
@@ -501,6 +533,14 @@ export const make = Effect.gen(function* () {
|
||||
),
|
||||
),
|
||||
),
|
||||
revealInFileManager: (input) =>
|
||||
provideCommandResolutionServices(
|
||||
Effect.flatMap(resolveFileManagerRevealLaunch(input), (launch) =>
|
||||
launchEditorProcess(launch).pipe(
|
||||
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
|
||||
),
|
||||
),
|
||||
),
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -5097,6 +5097,33 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
|
||||
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
|
||||
);
|
||||
|
||||
it.effect("routes websocket rpc shell.revealInFileManager", () =>
|
||||
Effect.gen(function* () {
|
||||
let revealedInput: { path: string } | null = null;
|
||||
yield* buildAppUnderTest({
|
||||
layers: {
|
||||
externalLauncher: {
|
||||
revealInFileManager: (input) =>
|
||||
Effect.sync(() => {
|
||||
revealedInput = input;
|
||||
}),
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const wsUrl = yield* getWsServerUrl("/ws");
|
||||
yield* Effect.scoped(
|
||||
withWsRpcClient(wsUrl, (client) =>
|
||||
client[WS_METHODS.shellRevealInFileManager]({
|
||||
path: "/tmp/project/src/index.ts",
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
assert.deepEqual(revealedInput, { path: "/tmp/project/src/index.ts" });
|
||||
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
|
||||
);
|
||||
|
||||
it.effect("routes websocket rpc shell.openInEditor errors", () =>
|
||||
Effect.gen(function* () {
|
||||
const externalLauncherError = new ExternalLauncherCommandNotFoundError({
|
||||
|
||||
@@ -1831,6 +1831,12 @@ const makeWsRpcLayer = (
|
||||
observeRpcEffect(WS_METHODS.shellOpenInEditor, externalLauncher.launchEditor(input), {
|
||||
"rpc.aggregate": "workspace",
|
||||
}),
|
||||
[WS_METHODS.shellRevealInFileManager]: (input) =>
|
||||
observeRpcEffect(
|
||||
WS_METHODS.shellRevealInFileManager,
|
||||
externalLauncher.revealInFileManager(input),
|
||||
{ "rpc.aggregate": "workspace" },
|
||||
),
|
||||
[WS_METHODS.filesystemBrowse]: (input) =>
|
||||
observeRpcEffect(
|
||||
WS_METHODS.filesystemBrowse,
|
||||
|
||||
@@ -88,6 +88,7 @@ import { serverEnvironment } from "../state/server";
|
||||
import { assetEnvironment } from "../state/assets";
|
||||
import { usePreparedConnection } from "../state/session";
|
||||
import { previewEnvironment } from "../state/preview";
|
||||
import { shellEnvironment } from "../state/shell";
|
||||
import { useAtomCommand } from "../state/use-atom-command";
|
||||
import { useAtomQueryRunner } from "../state/use-atom-query-runner";
|
||||
import { useOpenChangeRequestLink } from "~/lib/openPullRequestLink";
|
||||
@@ -781,6 +782,7 @@ function UncachedShikiCodeBlock({
|
||||
|
||||
interface MarkdownFileLinkProps {
|
||||
href: string;
|
||||
filePath: string;
|
||||
targetPath: string;
|
||||
iconPath: string;
|
||||
displayPath: string;
|
||||
@@ -791,6 +793,7 @@ interface MarkdownFileLinkProps {
|
||||
theme: "light" | "dark";
|
||||
threadRef?: ScopedThreadRef | undefined;
|
||||
onOpen: (targetPath: string) => Promise<AtomCommandResult<unknown, unknown>>;
|
||||
onRevealInFileManager: (filePath: string) => Promise<AtomCommandResult<unknown, unknown>>;
|
||||
onOpenInBrowser?: (() => Promise<AtomCommandResult<unknown, unknown>>) | undefined;
|
||||
className?: string | undefined;
|
||||
}
|
||||
@@ -1086,6 +1089,7 @@ function MarkdownExternalLinkContent({
|
||||
|
||||
const MarkdownFileLink = memo(function MarkdownFileLink({
|
||||
href,
|
||||
filePath,
|
||||
targetPath,
|
||||
iconPath,
|
||||
displayPath,
|
||||
@@ -1096,6 +1100,7 @@ const MarkdownFileLink = memo(function MarkdownFileLink({
|
||||
theme,
|
||||
threadRef,
|
||||
onOpen,
|
||||
onRevealInFileManager,
|
||||
onOpenInBrowser,
|
||||
className,
|
||||
}: MarkdownFileLinkProps) {
|
||||
@@ -1134,6 +1139,38 @@ const MarkdownFileLink = memo(function MarkdownFileLink({
|
||||
})();
|
||||
}, [onOpen, targetPath]);
|
||||
|
||||
const handleRevealInFileManager = useCallback(() => {
|
||||
void (async () => {
|
||||
try {
|
||||
const result = await onRevealInFileManager(filePath);
|
||||
if (result._tag === "Success" || isAtomCommandInterrupted(result)) {
|
||||
return;
|
||||
}
|
||||
reportMarkdownActionFailure(
|
||||
{ operation: "open-file-in-folder", target: filePath },
|
||||
result.cause,
|
||||
);
|
||||
const error = squashAtomCommandFailure(result);
|
||||
toastManager.add(
|
||||
stackedThreadToast({
|
||||
type: "error",
|
||||
title: "Unable to open folder",
|
||||
description: error instanceof Error ? error.message : "An error occurred.",
|
||||
}),
|
||||
);
|
||||
} catch (cause) {
|
||||
reportMarkdownActionFailure({ operation: "open-file-in-folder", target: filePath }, cause);
|
||||
toastManager.add(
|
||||
stackedThreadToast({
|
||||
type: "error",
|
||||
title: "Unable to open folder",
|
||||
description: cause instanceof Error ? cause.message : "An error occurred.",
|
||||
}),
|
||||
);
|
||||
}
|
||||
})();
|
||||
}, [filePath, onRevealInFileManager]);
|
||||
|
||||
const handleOpenInFilePreview = useCallback(() => {
|
||||
if (!threadRef || !workspaceRelativePath) {
|
||||
handleOpenInEditor();
|
||||
@@ -1231,6 +1268,7 @@ const MarkdownFileLink = memo(function MarkdownFileLink({
|
||||
const clicked = await api.contextMenu.show(
|
||||
[
|
||||
{ id: "open", label: "Open in editor" },
|
||||
{ id: "open-in-folder", label: "Open in folder" },
|
||||
...(onOpenInBrowser
|
||||
? ([{ id: "open-in-browser", label: "Open in integrated browser" }] as const)
|
||||
: []),
|
||||
@@ -1244,6 +1282,10 @@ const MarkdownFileLink = memo(function MarkdownFileLink({
|
||||
handleOpenInEditor();
|
||||
return;
|
||||
}
|
||||
if (clicked === "open-in-folder") {
|
||||
handleRevealInFileManager();
|
||||
return;
|
||||
}
|
||||
if (clicked === "open-in-browser") {
|
||||
handleOpenInBrowser();
|
||||
return;
|
||||
@@ -1262,7 +1304,15 @@ const MarkdownFileLink = memo(function MarkdownFileLink({
|
||||
);
|
||||
}
|
||||
},
|
||||
[displayPath, handleCopy, handleOpenInBrowser, handleOpenInEditor, onOpenInBrowser, targetPath],
|
||||
[
|
||||
displayPath,
|
||||
handleCopy,
|
||||
handleOpenInBrowser,
|
||||
handleOpenInEditor,
|
||||
handleRevealInFileManager,
|
||||
onOpenInBrowser,
|
||||
targetPath,
|
||||
],
|
||||
);
|
||||
|
||||
return (
|
||||
@@ -1306,6 +1356,7 @@ function areMarkdownFileLinkPropsEqual(
|
||||
): boolean {
|
||||
return (
|
||||
previous.href === next.href &&
|
||||
previous.filePath === next.filePath &&
|
||||
previous.targetPath === next.targetPath &&
|
||||
previous.iconPath === next.iconPath &&
|
||||
previous.displayPath === next.displayPath &&
|
||||
@@ -1316,6 +1367,7 @@ function areMarkdownFileLinkPropsEqual(
|
||||
previous.theme === next.theme &&
|
||||
previous.threadRef === next.threadRef &&
|
||||
previous.onOpen === next.onOpen &&
|
||||
previous.onRevealInFileManager === next.onRevealInFileManager &&
|
||||
previous.onOpenInBrowser === next.onOpenInBrowser &&
|
||||
previous.className === next.className
|
||||
);
|
||||
@@ -1338,6 +1390,9 @@ function ChatMarkdown({
|
||||
const openPreview = useAtomCommand(previewEnvironment.open, {
|
||||
reportFailure: false,
|
||||
});
|
||||
const revealInFileManager = useAtomCommand(shellEnvironment.revealInFileManager, {
|
||||
reportFailure: false,
|
||||
});
|
||||
const preparedConnection = usePreparedConnection(threadRef?.environmentId ?? null);
|
||||
const environmentId = useActiveEnvironmentId();
|
||||
const serverConfig = useAtomValue(serverEnvironment.configValueAtom(environmentId));
|
||||
@@ -1414,6 +1469,21 @@ function ChatMarkdown({
|
||||
},
|
||||
[openPreview, threadRef],
|
||||
);
|
||||
const revealMarkdownFileInFileManager = useCallback(
|
||||
(filePath: string): Promise<AtomCommandResult<unknown, unknown>> => {
|
||||
const targetEnvironmentId = threadRef?.environmentId ?? environmentId;
|
||||
if (targetEnvironmentId === null) {
|
||||
return Promise.resolve(
|
||||
AsyncResult.failure(Cause.fail(new Error("No environment is selected."))),
|
||||
);
|
||||
}
|
||||
return revealInFileManager({
|
||||
environmentId: targetEnvironmentId,
|
||||
input: { path: filePath },
|
||||
});
|
||||
},
|
||||
[environmentId, revealInFileManager, threadRef?.environmentId],
|
||||
);
|
||||
const openMarkdownFileInPreview = useCallback(
|
||||
(path: string) => {
|
||||
if (!threadRef || preparedConnection._tag === "None") {
|
||||
@@ -1460,6 +1530,7 @@ function ChatMarkdown({
|
||||
return (
|
||||
<MarkdownFileLink
|
||||
href={fileLinkMeta.targetPath}
|
||||
filePath={fileLinkMeta.filePath}
|
||||
targetPath={fileLinkMeta.targetPath}
|
||||
iconPath={fileLinkMeta.filePath}
|
||||
displayPath={fileLinkMeta.displayPath}
|
||||
@@ -1470,6 +1541,7 @@ function ChatMarkdown({
|
||||
theme={resolvedTheme}
|
||||
threadRef={threadRef}
|
||||
onOpen={openInPreferredEditor}
|
||||
onRevealInFileManager={revealMarkdownFileInFileManager}
|
||||
onOpenInBrowser={
|
||||
threadRef &&
|
||||
isPreviewSupportedInRuntime() &&
|
||||
@@ -1690,6 +1762,7 @@ function ChatMarkdown({
|
||||
markdownFileLinkMetaByHref,
|
||||
onTaskListChange,
|
||||
openInPreferredEditor,
|
||||
revealMarkdownFileInFileManager,
|
||||
openExternalLinkInPreview,
|
||||
openMarkdownFileInPreview,
|
||||
resolvedTheme,
|
||||
|
||||
@@ -12,5 +12,9 @@ export function createShellEnvironmentAtoms<R, E>(
|
||||
label: "environment-data:shell:open-in-editor",
|
||||
tag: WS_METHODS.shellOpenInEditor,
|
||||
}),
|
||||
revealInFileManager: createEnvironmentRpcCommand(runtime, {
|
||||
label: "environment-data:shell:reveal-in-file-manager",
|
||||
tag: WS_METHODS.shellRevealInFileManager,
|
||||
}),
|
||||
};
|
||||
}
|
||||
@@ -123,6 +123,11 @@ export const RemoteOpenTarget = Schema.Struct({
|
||||
});
|
||||
export type RemoteOpenTarget = typeof RemoteOpenTarget.Type;
|
||||
|
||||
export const RevealInFileManagerInput = Schema.Struct({
|
||||
path: TrimmedNonEmptyString,
|
||||
});
|
||||
export type RevealInFileManagerInput = typeof RevealInFileManagerInput.Type;
|
||||
|
||||
export class ExternalLauncherUnknownEditorError extends Schema.TaggedErrorClass<ExternalLauncherUnknownEditorError>()(
|
||||
"ExternalLauncherUnknownEditorError",
|
||||
{
|
||||
|
||||
@@ -2,7 +2,11 @@ import * as Schema from "effect/Schema";
|
||||
import * as Rpc from "effect/unstable/rpc/Rpc";
|
||||
import * as RpcGroup from "effect/unstable/rpc/RpcGroup";
|
||||
|
||||
import { ExternalLauncherError, LaunchEditorInput } from "./editor.ts";
|
||||
import {
|
||||
ExternalLauncherError,
|
||||
LaunchEditorInput,
|
||||
RevealInFileManagerInput,
|
||||
} from "./editor.ts";
|
||||
import {
|
||||
AuthAccessStreamError,
|
||||
AuthAccessStreamEvent,
|
||||
@@ -204,6 +208,7 @@ export const WS_METHODS = {
|
||||
|
||||
// Shell methods
|
||||
shellOpenInEditor: "shell.openInEditor",
|
||||
shellRevealInFileManager: "shell.revealInFileManager",
|
||||
|
||||
// Filesystem methods
|
||||
filesystemBrowse: "filesystem.browse",
|
||||
@@ -644,6 +649,11 @@ export const WsShellOpenInEditorRpc = Rpc.make(WS_METHODS.shellOpenInEditor, {
|
||||
error: Schema.Union([ExternalLauncherError, EnvironmentAuthorizationError]),
|
||||
});
|
||||
|
||||
export const WsShellRevealInFileManagerRpc = Rpc.make(WS_METHODS.shellRevealInFileManager, {
|
||||
payload: RevealInFileManagerInput,
|
||||
error: Schema.Union([ExternalLauncherError, EnvironmentAuthorizationError]),
|
||||
});
|
||||
|
||||
export const WsFilesystemBrowseRpc = Rpc.make(WS_METHODS.filesystemBrowse, {
|
||||
payload: FilesystemBrowseInput,
|
||||
success: FilesystemBrowseResult,
|
||||
@@ -1022,6 +1032,7 @@ export const WsRpcGroup = RpcGroup.make(
|
||||
WsProjectsSearchEntriesRpc,
|
||||
WsProjectsWriteFileRpc,
|
||||
WsShellOpenInEditorRpc,
|
||||
WsShellRevealInFileManagerRpc,
|
||||
WsFilesystemBrowseRpc,
|
||||
WsAssetsCreateUrlRpc,
|
||||
WsSubscribeVcsStatusRpc,
|
||||
|
||||
Reference in new issue
Block a user