-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
93 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { statSync } from "node:fs"; | ||
import { isDocker } from "./docker"; | ||
|
||
function hasContainerEnv() { | ||
try { | ||
statSync("/run/.containerenv"); | ||
return true; | ||
} catch { | ||
return false; | ||
} | ||
} | ||
|
||
let isContainerCached: boolean; | ||
|
||
function _isContainer() { | ||
if (isContainerCached === undefined) { | ||
isContainerCached = hasContainerEnv(); | ||
} | ||
return isContainerCached; | ||
} | ||
|
||
export const isContainer = _isContainer() || isDocker; | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { readFileSync, statSync } from "node:fs"; | ||
|
||
let isDockerCached: boolean; | ||
|
||
function _isDocker() { | ||
if (isDockerCached === undefined) { | ||
isDockerCached = hasDockerEnv() || hasDockerCGroup(); | ||
} | ||
return isDockerCached; | ||
} | ||
|
||
function hasDockerEnv() { | ||
try { | ||
statSync("/.dockerenv"); | ||
return true; | ||
} catch { | ||
return false; | ||
} | ||
} | ||
|
||
function hasDockerCGroup() { | ||
try { | ||
return readFileSync("/proc/self/cgroup", "utf8").includes("docker"); | ||
} catch { | ||
return false; | ||
} | ||
} | ||
|
||
export const isDocker = _isDocker(); | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from "./wsl"; | ||
export * from "./docker"; | ||
export * from "./container"; | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { readFileSync } from "node:fs"; | ||
import { release } from "node:os"; | ||
import { isDocker } from "./docker"; | ||
|
||
let isWSLCached: boolean; | ||
|
||
function _isWsl() { | ||
if (isWSLCached === undefined) { | ||
isWSLCached = hasUnameOrProcVersion(); | ||
} | ||
return isWSLCached; | ||
} | ||
|
||
function hasUnameOrProcVersion() { | ||
if (globalThis.process?.platform !== "linux") { | ||
return false; | ||
} | ||
if (release().toLowerCase().includes("microsoft")) { | ||
if (isDocker) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
try { | ||
return readFileSync("/proc/version", "utf8") | ||
.toLowerCase() | ||
.includes("microsoft") | ||
? !isDocker | ||
: false; | ||
} catch { | ||
return false; | ||
} | ||
} | ||
|
||
export const isWsl = _isWsl(); | ||