Archived
1
0

feat: add tests for isWsl (#5251)

* refactor: add docs to isWsl and make easier to test

* feat: add tests for isWsl

* fixup!: refactor isWsl and tests

* Update test/unit/node/util.test.ts

Co-authored-by: Asher <ash@coder.com>

* Update test/unit/node/util.test.ts

Co-authored-by: Asher <ash@coder.com>

Co-authored-by: Asher <ash@coder.com>
This commit is contained in:
Joe Previte
2022-06-10 13:00:20 -07:00
committed by GitHub
parent d75883bcc6
commit 2c1cf0259e
2 changed files with 81 additions and 6 deletions

View File

@ -377,11 +377,31 @@ export const getMediaMime = (filePath?: string): string => {
return (filePath && mimeTypes[path.extname(filePath)]) || "text/plain"
}
export const isWsl = async (): Promise<boolean> => {
return (
(process.platform === "linux" && os.release().toLowerCase().indexOf("microsoft") !== -1) ||
(await fs.readFile("/proc/version", "utf8")).toLowerCase().indexOf("microsoft") !== -1
)
/**
* A helper function that checks if the platform is Windows Subsystem for Linux
* (WSL)
*
* @see https://github.com/sindresorhus/is-wsl/blob/main/index.js
* @returns {Boolean} boolean if it is WSL
*/
export const isWsl = async (
platform: NodeJS.Platform,
osRelease: string,
procVersionFilePath: string,
): Promise<boolean> => {
if (platform !== "linux") {
return false
}
if (osRelease.toLowerCase().includes("microsoft")) {
return true
}
try {
return (await fs.readFile(procVersionFilePath, "utf8")).toLowerCase().includes("microsoft")
} catch (_) {
return false
}
}
/**
@ -398,7 +418,7 @@ export const open = async (address: URL | string): Promise<void> => {
}
const args = [] as string[]
const options = {} as cp.SpawnOptions
const platform = (await isWsl()) ? "wsl" : process.platform
const platform = (await isWsl(process.platform, os.release(), "/proc/version")) ? "wsl" : process.platform
let command = platform === "darwin" ? "open" : "xdg-open"
if (platform === "win32" || platform === "wsl") {
command = platform === "wsl" ? "cmd.exe" : "cmd"