forked from skoshx/deno-open
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
180 lines (150 loc) · 4.9 KB
/
index.ts
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { join } from "https://deno.land/std@0.215.0/path/posix/join.ts";
import { isWsl } from "https://deno.land/x/is_wsl@v1.1.0/mod.ts";
const { os } = Deno.build;
/**
* Returns the directory where this file exists.
* @param url Value returned from import.meta.url
*/
export function getDir(url: string) {
const u = new URL(url);
const file: string = u.protocol === "file:" ? u.pathname : url;
const directory = file.replace(/[/][^/]*$/, "");
return directory;
}
export interface OpenOptions {
/**
* Wait for the opened app to exit before fulfilling the promise. If `false` it's fulfilled immediately when opening the app.
* Note that it waits for the app to exit, not just for the window to close.
* On Windows, you have to explicitly specify an app for it to be able to wait.
* @default false
*/
readonly wait?: boolean;
/**
* __macOS only__
* Do not bring the app to the foreground.
* @default false
*/
readonly background?: boolean;
/**
* Specify the app to open the `target` with, or an array with the app and app arguments.
* The app name is platform dependent. Don't hard code it in reusable modules. For example, Chrome is `google chrome` on macOS, `google-chrome` on Linux and `chrome` on Windows.
* You may also pass in the app's full path. For example on WSL, this can be `/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe` for the Windows installation of Chrome.
*/
app?: string | string[];
/**
* Uses `encodeURI` to encode the `target` before executing it.
* The use with targets that are not URLs is not recommended.
* Especially useful when dealing with the [double-quotes on Windows](https://github.com/sindresorhus/open#double-quotes-on-windows) caveat.
* @default false
*/
readonly url?: boolean;
}
async function isFile(fileName: string): Promise<boolean> {
try {
const info = await Deno.stat(fileName);
return info.isFile;
} catch (err) {
if (err instanceof Deno.errors.NotFound) {
return false; // File or directory exists
} else {
throw err;
}
}
}
// Path to included `xdg-open`.
const localXdgOpenPath = join(getDir(import.meta.url), "xdg-open");
export async function open(
target: string,
options?: OpenOptions,
): Promise<Deno.ChildProcess> {
if (typeof target !== "string") {
throw new TypeError("Expected a target");
}
const defaults = {
wait: false,
background: false,
url: false,
};
options = { ...defaults, ...options };
let command;
let appArguments: string[] = [];
const cliArguments: string[] = [];
if (Array.isArray(options.app)) {
appArguments = options.app.slice(1);
options.app = options.app[0];
}
// Encodes the target as if it were an URL. Especially useful to get
// double-quotes through the “double-quotes on Windows caveat”, but it
// can be used on any platform.
if (options.url) {
target = encodeURI(target);
}
if (os === "darwin") {
command = "open";
if (options.wait) {
cliArguments.push("--wait-apps");
}
if (options.background) {
cliArguments.push("--background");
}
if (options.app) {
cliArguments.push("-a", options.app);
}
} else if (os === "windows") {
command = "cmd";
cliArguments.push("/s", "/c", "start", "", "/b");
if (options.wait) {
cliArguments.push("/wait");
}
if (options.app) {
cliArguments.push(options.app);
}
if (appArguments.length > 0) {
cliArguments.push(...appArguments);
}
} else {
const wsl = await isWsl();
if (options.app) {
command = options.app;
} else if (wsl) {
command = "wslview";
} else {
// When bundled by Webpack, there's no actual package file path and no local `xdg-open`.
const isBundled = !getDir(import.meta.url) ||
getDir(import.meta.url) === "/";
// Check if local `xdg-open` exists and is executable.
const exeLocalXdgOpen = await isFile(localXdgOpenPath);
const useSystemXdgOpen = isBundled || !exeLocalXdgOpen;
command = useSystemXdgOpen ? "xdg-open" : localXdgOpenPath;
}
if (appArguments.length > 0) {
cliArguments.push(...appArguments);
}
}
cliArguments.push(target);
if (os === "darwin" && appArguments.length > 0) {
cliArguments.push("--args", ...appArguments);
}
const subprocess = new Deno.Command(command, {
args: cliArguments,
stderr: "piped",
stdout: "piped",
});
const process = subprocess.spawn();
// command: open /Applications/Google\ Chrome.app {url}
if (options.wait) {
const output = await process.output();
return new Promise((resolve, reject) => {
const err = output.stderr;
if (err && err.length > 0) {
reject(new TextDecoder().decode(err));
}
if (!output.success) {
reject(new Error(`Exited with code ${output.code}`));
return;
}
resolve(process);
});
}
return process;
}