-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnativeFilePicker.js
More file actions
79 lines (73 loc) · 2.77 KB
/
Copy pathnativeFilePicker.js
File metadata and controls
79 lines (73 loc) · 2.77 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { execFile } from "node:child_process";
function run(file, args) {
return new Promise((resolve, reject) => {
execFile(file, args, { encoding: "utf-8" }, (error, stdout, stderr) => {
if (error) {
error.stderr = stderr;
reject(error);
return;
}
resolve(stdout.trim());
});
});
}
function isCancellation(error) {
const message = `${error?.message || ""}\n${error?.stderr || ""}`;
return /cancel|canceled|cancelled|user cancelled/i.test(message);
}
async function chooseLocalPath({ kind, platform = process.platform, execute = run }) {
let command;
if (platform === "darwin") {
command = kind === "directory"
? {
file: "osascript",
args: [
"-e",
'set selectedFolder to choose folder with prompt "Choose a folder containing HTML files"',
"-e",
"POSIX path of selectedFolder",
],
}
: {
file: "osascript",
args: [
"-e",
'set selectedFile to choose file with prompt "Choose an HTML file" of type {"public.html"}',
"-e",
"POSIX path of selectedFile",
],
};
} else if (platform === "win32") {
const dialogScript = kind === "directory"
? "Add-Type -AssemblyName System.Windows.Forms; $d=New-Object System.Windows.Forms.FolderBrowserDialog; $d.Description='Choose a folder containing HTML files'; if($d.ShowDialog() -eq 'OK'){[Console]::Write($d.SelectedPath)}"
: "Add-Type -AssemblyName System.Windows.Forms; $d=New-Object System.Windows.Forms.OpenFileDialog; $d.Filter='HTML files (*.html)|*.html'; if($d.ShowDialog() -eq 'OK'){[Console]::Write($d.FileName)}";
command = {
file: "powershell.exe",
args: ["-NoProfile", "-Command", dialogScript],
};
} else {
command = {
file: "zenity",
args: kind === "directory"
? ["--file-selection", "--directory", "--title=Choose a folder containing HTML files"]
: ["--file-selection", "--title=Choose an HTML file", "--file-filter=HTML files | *.html"],
};
}
try {
return (await execute(command.file, command.args)).trim() || null;
} catch (error) {
if (isCancellation(error)) return null;
const unavailable = new Error("Native file picker is unavailable");
unavailable.code = "PICKER_UNAVAILABLE";
unavailable.cause = error;
throw unavailable;
}
}
/** Open the operating system's native single-file dialog from the local CLI. */
export function chooseLocalHtmlFile(options = {}) {
return chooseLocalPath({ ...options, kind: "file" });
}
/** Open the operating system's native directory dialog from the local CLI. */
export function chooseLocalHtmlDirectory(options = {}) {
return chooseLocalPath({ ...options, kind: "directory" });
}