Skip to content

Commit

Permalink
feat: support containers flags
Browse files Browse the repository at this point in the history
  • Loading branch information
zAlweNy26 committed Oct 3, 2024
1 parent 543d828 commit a1ff14b
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default defineBuildConfig({
minify: true,
},
},
entries: ["src/index"],
entries: ["src/index", "src/containers/index"],
hooks: {
"rollup:options"(ctx, rollupConfig) {
(rollupConfig.plugins as Plugin[]).push({
Expand Down
6 changes: 3 additions & 3 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import unjs from "eslint-config-unjs";
export default unjs({
ignores: [],
rules: {
"unicorn/no-null": 0,
"unicorn/prevent-abbreviations": 0
},
"unicorn/no-null": 0,
"unicorn/prevent-abbreviations": 0
},
});
22 changes: 22 additions & 0 deletions src/containers/container.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { statSync } from "node:fs";
import { isDocker } from "./docker";

Check warning on line 2 in src/containers/container.ts

View check run for this annotation

Codecov / codecov/patch

src/containers/container.ts#L2

Added line #L2 was not covered by tests

function hasContainerEnv() {
try {
statSync("/run/.containerenv");
return true;
} catch {
return false;
}
}

Check warning on line 11 in src/containers/container.ts

View check run for this annotation

Codecov / codecov/patch

src/containers/container.ts#L4-L11

Added lines #L4 - L11 were not covered by tests

let isContainerCached: boolean;

Check warning on line 13 in src/containers/container.ts

View check run for this annotation

Codecov / codecov/patch

src/containers/container.ts#L13

Added line #L13 was not covered by tests

function _isContainer() {
if (isContainerCached === undefined) {
isContainerCached = hasContainerEnv();
}
return isContainerCached;
}

Check warning on line 20 in src/containers/container.ts

View check run for this annotation

Codecov / codecov/patch

src/containers/container.ts#L15-L20

Added lines #L15 - L20 were not covered by tests

export const isContainer = _isContainer() || isDocker;

Check warning on line 22 in src/containers/container.ts

View check run for this annotation

Codecov / codecov/patch

src/containers/container.ts#L22

Added line #L22 was not covered by tests
29 changes: 29 additions & 0 deletions src/containers/docker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { readFileSync, statSync } from "node:fs";

let isDockerCached: boolean;

Check warning on line 3 in src/containers/docker.ts

View check run for this annotation

Codecov / codecov/patch

src/containers/docker.ts#L3

Added line #L3 was not covered by tests

function _isDocker() {
if (isDockerCached === undefined) {
isDockerCached = hasDockerEnv() || hasDockerCGroup();
}
return isDockerCached;
}

Check warning on line 10 in src/containers/docker.ts

View check run for this annotation

Codecov / codecov/patch

src/containers/docker.ts#L5-L10

Added lines #L5 - L10 were not covered by tests

function hasDockerEnv() {
try {
statSync("/.dockerenv");
return true;
} catch {
return false;
}
}

Check warning on line 19 in src/containers/docker.ts

View check run for this annotation

Codecov / codecov/patch

src/containers/docker.ts#L12-L19

Added lines #L12 - L19 were not covered by tests

function hasDockerCGroup() {
try {
return readFileSync("/proc/self/cgroup", "utf8").includes("docker");
} catch {
return false;
}
}

Check warning on line 27 in src/containers/docker.ts

View check run for this annotation

Codecov / codecov/patch

src/containers/docker.ts#L21-L27

Added lines #L21 - L27 were not covered by tests

export const isDocker = _isDocker();

Check warning on line 29 in src/containers/docker.ts

View check run for this annotation

Codecov / codecov/patch

src/containers/docker.ts#L29

Added line #L29 was not covered by tests
3 changes: 3 additions & 0 deletions src/containers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./wsl";
export * from "./docker";
export * from "./container";

Check warning on line 3 in src/containers/index.ts

View check run for this annotation

Codecov / codecov/patch

src/containers/index.ts#L2-L3

Added lines #L2 - L3 were not covered by tests
35 changes: 35 additions & 0 deletions src/containers/wsl.ts
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";

Check warning on line 3 in src/containers/wsl.ts

View check run for this annotation

Codecov / codecov/patch

src/containers/wsl.ts#L2-L3

Added lines #L2 - L3 were not covered by tests

let isWSLCached: boolean;

Check warning on line 5 in src/containers/wsl.ts

View check run for this annotation

Codecov / codecov/patch

src/containers/wsl.ts#L5

Added line #L5 was not covered by tests

function _isWsl() {
if (isWSLCached === undefined) {
isWSLCached = hasUnameOrProcVersion();
}
return isWSLCached;
}

Check warning on line 12 in src/containers/wsl.ts

View check run for this annotation

Codecov / codecov/patch

src/containers/wsl.ts#L7-L12

Added lines #L7 - L12 were not covered by tests

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;
}
}

Check warning on line 33 in src/containers/wsl.ts

View check run for this annotation

Codecov / codecov/patch

src/containers/wsl.ts#L14-L33

Added lines #L14 - L33 were not covered by tests

export const isWsl = _isWsl();

Check warning on line 35 in src/containers/wsl.ts

View check run for this annotation

Codecov / codecov/patch

src/containers/wsl.ts#L35

Added line #L35 was not covered by tests

0 comments on commit a1ff14b

Please sign in to comment.