@gentbajko/slopify 0.4.0 → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,29 @@
1
1
  import { spawn } from "node:child_process";
2
- export function browserCommand(platform, url) {
2
+ import { readFileSync } from "node:fs";
3
+ // WSL reports itself as linux, so the browser it opens is one inside the distribution -
4
+ // usually none at all - rather than the Windows browser the user is actually looking at.
5
+ // The kernel string is the reliable marker: the environment variables below are absent
6
+ // under `sudo` and in a login shell started by a service, but /proc/version always says.
7
+ export function isWsl(platform, env = process.env, kernel = readKernel) {
8
+ if (platform !== "linux") {
9
+ return false;
10
+ }
11
+ if (env.WSL_DISTRO_NAME !== undefined || env.WSL_INTEROP !== undefined) {
12
+ return true;
13
+ }
14
+ // Guarded here rather than in the reader: whatever is injected, a browser that does not
15
+ // open is a nuisance and must not be able to fail the boot.
16
+ try {
17
+ return /microsoft|wsl/i.test(kernel());
18
+ }
19
+ catch {
20
+ return false;
21
+ }
22
+ }
23
+ function readKernel() {
24
+ return readFileSync("/proc/version", "utf8");
25
+ }
26
+ export function browserCommand(platform, url, wsl = false) {
3
27
  if (platform === "darwin") {
4
28
  return ["open", [url]];
5
29
  }
@@ -7,10 +31,16 @@ export function browserCommand(platform, url) {
7
31
  // `start` is a cmd builtin, and its first quoted argument is the window title.
8
32
  return ["cmd", ["/c", "start", "", url]];
9
33
  }
34
+ if (wsl) {
35
+ // The same builtin, reached across the interop boundary, so the URL opens in the
36
+ // Windows default browser. `.exe` is required from the linux side. cmd complains about
37
+ // the linux working directory on stderr, which is already discarded.
38
+ return ["cmd.exe", ["/c", "start", "", url]];
39
+ }
10
40
  return ["xdg-open", [url]];
11
41
  }
12
42
  export function openBrowser(url, warn) {
13
- const [command, args] = browserCommand(process.platform, url);
43
+ const [command, args] = browserCommand(process.platform, url, isWsl(process.platform));
14
44
  const child = spawn(command, [...args], { stdio: "ignore", detached: true });
15
45
  // No browser is a nuisance, not a failure: the URL is already on the terminal.
16
46
  child.on("error", (error) => {