Skip to content

Commit cf62c4a

Browse files
committed
refactor: add docs to isWsl and make easier to test
1 parent d75883b commit cf62c4a

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/node/util.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -377,10 +377,16 @@ export const getMediaMime = (filePath?: string): string => {
377377
return (filePath && mimeTypes[path.extname(filePath)]) || "text/plain"
378378
}
379379

380-
export const isWsl = async (): Promise<boolean> => {
380+
/**
381+
* A helper function that checks if the platform is Windows Subsystem for Linux
382+
* (WSL)
383+
*
384+
* @returns {Boolean} boolean if it is WSL
385+
*/
386+
export const isWsl = async (platform: NodeJS.Platform, procVersionFilePath: string): Promise<boolean> => {
381387
return (
382-
(process.platform === "linux" && os.release().toLowerCase().indexOf("microsoft") !== -1) ||
383-
(await fs.readFile("/proc/version", "utf8")).toLowerCase().indexOf("microsoft") !== -1
388+
(platform === "linux" && os.release().toLowerCase().indexOf("microsoft") !== -1) ||
389+
(await fs.readFile(procVersionFilePath, "utf8")).toLowerCase().indexOf("microsoft") !== -1
384390
)
385391
}
386392

@@ -398,7 +404,7 @@ export const open = async (address: URL | string): Promise<void> => {
398404
}
399405
const args = [] as string[]
400406
const options = {} as cp.SpawnOptions
401-
const platform = (await isWsl()) ? "wsl" : process.platform
407+
const platform = (await isWsl(process.platform, "/proc/version")) ? "wsl" : process.platform
402408
let command = platform === "darwin" ? "open" : "xdg-open"
403409
if (platform === "win32" || platform === "wsl") {
404410
command = platform === "wsl" ? "cmd.exe" : "cmd"

0 commit comments

Comments
 (0)