fix: select files when revealing supported
This commit is contained in:
1 parent
f5965c2083
commit
be23cedeb9
2 files changed
+92
-5
No files matched your search
@@ -125,7 +125,7 @@ it.effect("reveals files with the linux file manager", () =>
|
||||
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
|
||||
);
|
||||
|
||||
it.effect("opens the containing folder with Finder", () =>
|
||||
it.effect("opens the containing folder when the Finder target is missing", () =>
|
||||
Effect.gen(function* () {
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
const path = yield* Path.Path;
|
||||
@@ -156,7 +156,41 @@ it.effect("opens the containing folder with Finder", () =>
|
||||
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
|
||||
);
|
||||
|
||||
it.effect("opens the containing folder with Windows Explorer", () =>
|
||||
it.effect("selects an existing file in 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");
|
||||
const targetPath = path.join(binDir, "project with spaces", "src", "index.ts");
|
||||
yield* fileSystem.writeFileString(commandPath, "#!/bin/sh\n");
|
||||
yield* fileSystem.chmod(commandPath, 0o755);
|
||||
yield* fileSystem.makeDirectory(path.dirname(targetPath), { recursive: true });
|
||||
yield* fileSystem.writeFileString(targetPath, "");
|
||||
|
||||
let spawned: ChildProcess.StandardCommand | undefined;
|
||||
yield* Effect.gen(function* () {
|
||||
const launcher = yield* ExternalLauncher.ExternalLauncher;
|
||||
yield* launcher.revealInFileManager({ path: targetPath });
|
||||
}).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", targetPath]);
|
||||
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
|
||||
);
|
||||
|
||||
it.effect("opens the containing folder when the Explorer target is missing", () =>
|
||||
Effect.gen(function* () {
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
const path = yield* Path.Path;
|
||||
@@ -186,6 +220,39 @@ it.effect("opens the containing folder with Windows Explorer", () =>
|
||||
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
|
||||
);
|
||||
|
||||
it.effect("selects an existing file in Windows Explorer", () =>
|
||||
Effect.gen(function* () {
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
const path = yield* Path.Path;
|
||||
const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-file-manager-" });
|
||||
const targetPath = path.join(binDir, "project files", "src", "index.ts");
|
||||
yield* fileSystem.writeFileString(path.join(binDir, "explorer.CMD"), "@echo off\r\n");
|
||||
yield* fileSystem.makeDirectory(path.dirname(targetPath), { recursive: true });
|
||||
yield* fileSystem.writeFileString(targetPath, "");
|
||||
|
||||
let spawned: ChildProcess.StandardCommand | undefined;
|
||||
yield* Effect.gen(function* () {
|
||||
const launcher = yield* ExternalLauncher.ExternalLauncher;
|
||||
yield* launcher.revealInFileManager({ path: targetPath });
|
||||
}).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,", targetPath.replaceAll("/", "\\")]);
|
||||
assert.equal(spawned.options.shell, false);
|
||||
}).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;
|
||||
|
||||
@@ -243,16 +243,32 @@ function hasGraphicalFileManagerSession(
|
||||
return true;
|
||||
}
|
||||
|
||||
function normalizeWindowsFileManagerPath(target: string): string {
|
||||
return target.replaceAll("/", "\\");
|
||||
}
|
||||
|
||||
function fileManagerFolderPath(platform: NodeJS.Platform, target: string, path: Path.Path): string {
|
||||
if (platform !== "win32") return path.dirname(target);
|
||||
|
||||
const normalized = target.replaceAll("/", "\\");
|
||||
const normalized = normalizeWindowsFileManagerPath(target);
|
||||
const separatorIndex = normalized.lastIndexOf("\\");
|
||||
if (separatorIndex < 0) return ".";
|
||||
if (separatorIndex === 2 && normalized[1] === ":") return normalized.slice(0, 3);
|
||||
return normalized.slice(0, separatorIndex) || "\\";
|
||||
}
|
||||
|
||||
function fileManagerRevealArgs(
|
||||
platform: NodeJS.Platform,
|
||||
target: string,
|
||||
targetExists: boolean,
|
||||
path: Path.Path,
|
||||
): ReadonlyArray<string> {
|
||||
if (!targetExists) return [fileManagerFolderPath(platform, target, path)];
|
||||
if (platform === "darwin") return ["-R", target];
|
||||
if (platform === "win32") return ["/select,", normalizeWindowsFileManagerPath(target)];
|
||||
return [fileManagerFolderPath(platform, target, path)];
|
||||
}
|
||||
|
||||
function fileManagerCommandForPlatform(platform: NodeJS.Platform): string {
|
||||
switch (platform) {
|
||||
case "darwin":
|
||||
@@ -422,14 +438,18 @@ const resolveEditorLaunch = Effect.fn("resolveEditorLaunch")(function* (
|
||||
const resolveFileManagerRevealLaunch = Effect.fn("externalLauncher.resolveFileManagerRevealLaunch")(
|
||||
function* (
|
||||
input: RevealInFileManagerInput,
|
||||
): Effect.fn.Return<EditorLaunch, ExternalLauncherError, Path.Path> {
|
||||
): Effect.fn.Return<EditorLaunch, ExternalLauncherError, FileSystem.FileSystem | Path.Path> {
|
||||
const platform = yield* HostProcessPlatform;
|
||||
const env = yield* readCommandLookupEnv;
|
||||
if (!hasGraphicalFileManagerSession(platform, env)) {
|
||||
return yield* new ExternalLauncherUnsupportedEditorError({ editor: "file-manager" });
|
||||
}
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
const path = yield* Path.Path;
|
||||
const args = [fileManagerFolderPath(platform, input.path, path)];
|
||||
const targetExists = yield* fileSystem
|
||||
.exists(input.path)
|
||||
.pipe(Effect.orElseSucceed(() => false));
|
||||
const args = fileManagerRevealArgs(platform, input.path, targetExists, path);
|
||||
|
||||
yield* Effect.annotateCurrentSpan({
|
||||
"externalLauncher.target": input.path,
|
||||
|
||||
Reference in new issue
Block a user