-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathlauncher-service.ts
More file actions
50 lines (44 loc) · 1.37 KB
/
launcher-service.ts
File metadata and controls
50 lines (44 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { FilePath } from "../types/file/filePath.js";
import { execa } from "execa";
import os from "os";
import { spawn } from "child_process";
export class LauncherService {
public async openInEditor(filePath: FilePath): Promise<void> {
try {
await execa("code", ["--wait", filePath.toString()]);
} catch {
// TODO: check for fallback (start)
if (process.platform === "win32") {
await execa("cmd", ["/c", "start", "/wait", "notepad", filePath.toString()], { stdio: "ignore" });
} else if (process.platform === "darwin") {
await execa("vim", [filePath.toString()], { stdio: "inherit"});
}
}
}
public async openFile(filePath: FilePath): Promise<void> {
const targetPath = filePath.toString();
// Determine the command and args without using the shell
let command: string;
let args: string[];
switch (os.platform()) {
case "win32":
command = "cmd";
args = ["/c", "start", "", targetPath];
break;
case "darwin":
command = "open";
args = [targetPath];
break;
default:
command = "xdg-open";
args = [targetPath];
break;
}
try {
const child = spawn(command, args, { stdio: "ignore", detached: true });
child.unref(); // Let it run without blocking
} catch {
// Silently ignore errors
}
}
}