Skip to content

Commit 4312fe8

Browse files
fix: fix open command on linux (#24)
1 parent 87fb55a commit 4312fe8

File tree

8 files changed

+1129
-47
lines changed

8 files changed

+1129
-47
lines changed

commands/console/mod.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { open } from "../../deps.ts";
1+
import { open } from "../../lib/open.ts";
22
import { env } from "../../env.ts";
33
import { command } from "../../zcli.ts";
44

commands/docs/mod.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { args, command, z } from "../../zcli.ts";
2-
import { open } from "../../deps.ts";
2+
import { open } from "../../lib/open.ts";
33
import { env } from "../../env.ts";
44

55
/**

commands/login/mod.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { args, command, fmt, z } from "../../zcli.ts";
2-
import { cursorUp, eraseLine, open } from "../../deps.ts";
2+
import { cursorUp, eraseLine } from "../../deps.ts";
3+
import { open } from "../../lib/open.ts";
34
import { config } from "../../config.ts";
45
import { credentials } from "../../credentials.ts";
56
import { session } from "../../api/auth.ts";

commands/signup/mod.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { command } from "../../zcli.ts";
2-
import { open } from "../../deps.ts";
2+
import { open } from "../../lib/open.ts";
33
import { env } from "../../env.ts";
44

55
/**

deno.lock

Lines changed: 0 additions & 42 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

deps.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ export * as YAML from "https://deno.land/[email protected]/encoding/yaml.ts";
1818
export * as TOML from "https://deno.land/[email protected]/encoding/toml.ts";
1919

2020
export * as Sentry from "https://deno.land/x/[email protected]/main.ts";
21-
export { open } from "https://deno.land/x/[email protected]/index.ts";
2221
export { ms } from "https://deno.land/x/[email protected]/ms.ts";
2322
export {
2423
cursorBack,

lib/open.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
export async function open(
2+
target: string,
3+
options?: OpenOptions,
4+
): Promise<Deno.Process> {
5+
const { os } = Deno.build;
6+
options = { url: false, ...options };
7+
8+
let command;
9+
const cliArguments: string[] = [];
10+
11+
// Encodes the target as if it were an URL. Especially useful to get
12+
// double-quotes through the “double-quotes on Windows caveat”, but it
13+
// can be used on any platform.
14+
if (options.url) {
15+
target = encodeURI(target);
16+
}
17+
18+
if (os === "darwin") {
19+
command = "open";
20+
} else if (os === "windows") {
21+
command = "cmd";
22+
cliArguments.push("/s", "/c", "start", "", "/b");
23+
} else {
24+
const xdgOpen = import.meta.resolve("./xdg-open").replace(/^file:\/\//, "");
25+
let xdgOpenExists = true;
26+
27+
try {
28+
Deno.statSync(xdgOpen);
29+
} catch (_err) {
30+
xdgOpenExists = false;
31+
}
32+
33+
command = xdgOpenExists ? xdgOpen : "xdg-open";
34+
}
35+
36+
cliArguments.push(target);
37+
38+
/* Options for the spawned process */
39+
const runOptions: Deno.RunOptions = {
40+
cmd: [command, ...cliArguments],
41+
stdin: "piped",
42+
stderr: "piped",
43+
stdout: "piped",
44+
};
45+
46+
const subprocess = Deno.run(runOptions);
47+
await subprocess.status();
48+
return subprocess;
49+
}
50+
51+
export interface OpenOptions {
52+
/**
53+
Uses `encodeURI` to encode the `target` before executing it.
54+
The use with targets that are not URLs is not recommended.
55+
@default false
56+
*/
57+
readonly url?: boolean;
58+
}

0 commit comments

Comments
 (0)