Skip to content

Commit

Permalink
node compatibility fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Pinta365 committed Mar 4, 2024
1 parent 48534fc commit a679f91
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion jsr.jsonc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@cross/env",
"version": "0.1.0",
"version": "0.1.1",
"exports": "./mod.ts"
}
8 changes: 2 additions & 6 deletions lib/filehandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,8 @@ export async function loadEnvFile(
fileContent = await Bun.file(filePath).text();
break;
case Runtimes.Node: {
if (typeof fs === "undefined") {
const fs = require("fs");
fileContent = fs.readFileSync(filePath, "utf-8");
} else {
throw new Error("Node.js 'fs' module is not available in this environment.");
}
const fs = await import("node:fs");
fileContent = fs.readFileSync(filePath, "utf-8");
break;
}
default:
Expand Down
11 changes: 9 additions & 2 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ declare const Bun: { env: Record<string, string> };
// deno-lint-ignore no-explicit-any
declare const require: (module: string) => any;
//shims Node.js process object
declare const process: { env: Record<string, string> };
declare const process: {
// deno-lint-ignore no-explicit-any
versions: any;
env: Record<string, string>;
};

// Flags to control behavior (initialized with defaults)
let throwErrors = false;
Expand All @@ -43,7 +47,10 @@ function getCurrentRuntime(): Runtimes {
return Runtimes.Deno;
} else if (typeof Bun === "object") {
return Runtimes.Bun;
} else if (typeof process === "object" && typeof require === "function") {
} else if (
typeof process === "object" && typeof process.versions !== "undefined" &&
typeof process.versions.node !== "undefined"
) {
return Runtimes.Node;
} else {
return Runtimes.Unsupported;
Expand Down

0 comments on commit a679f91

Please sign in to comment.