Skip to content

Commit

Permalink
refactor: simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Aug 16, 2023
1 parent f1665b6 commit 2e081b6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 38 deletions.
3 changes: 0 additions & 3 deletions src/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,3 @@ export const isColorSupported =
export const nodeVersion =
(_process.versions?.node || "").replace(/^v/, "") || null;
export const nodeMajorVersion = Number(nodeVersion?.split(".")[0]) || null;

/** Current runtime name */
export const runtimeName = detectRuntime().name;
54 changes: 20 additions & 34 deletions src/runtimes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,40 +33,26 @@ export const isNode = globalThis.process?.release?.name === "node";
export const isBun = globalThis.process?.release?.name === "bun";
export const isFastly = !!globalThis.fastly;

export function detectRuntime(): RuntimeInfo {
let name: RuntimeName = "";

if (isNetlify) {
name = "netlify";
}

if (isEdgeLight) {
name = "edge-light";
}

if (isWorkerd) {
name = "workerd";
}

if (isDeno) {
name = "deno";
}

if (isLagon) {
name = "lagon";
}

if (isNode) {
name = "node";
}

if (isBun) {
name = "bun";
const runtimeChecks: [boolean, RuntimeName][] = [
[isNetlify, "netlify"],
[isEdgeLight, "edge-light"],
[isWorkerd, "workerd"],
[isDeno, "deno"],
[isLagon, "lagon"],
[isNode, "node"],
[isBun, "bun"],
[isFastly, "fastly"],
];

export function detectRuntime(): RuntimeInfo | undefined {
const detectedRuntime = runtimeChecks.find((check) => check[0]);

if (detectedRuntime) {
const name = detectedRuntime[1];
return { name };
}
}

if (isFastly) {
name = "fastly";
}
export const runtimeInfo = detectRuntime();

return { name };
}
export const runtime = runtimeInfo?.name || "";
3 changes: 2 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ describe("std-env", () => {
"isColorSupported",
"nodeVersion",
"nodeMajorVersion",
"runtimeName",
"_process",
"process",
"detectProvider",
Expand All @@ -37,6 +36,8 @@ describe("std-env", () => {
"isBun",
"isFastly",
"detectRuntime",
"runtimeInfo",
"runtime",
]
`);
});
Expand Down

0 comments on commit 2e081b6

Please sign in to comment.