Skip to content

Commit

Permalink
feat(env): support import.meta.env and Deno.env
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Aug 9, 2023
1 parent 0d7b84f commit ceb04d5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"prepack": "unbuild",
"release": "pnpm test && changelogen --release && npm publish && git push --follow-tags",
"test": "pnpm lint && pnpm typecheck && vitest run --coverage",
"test:deno": "pnpm build && deno run -A test/deno.ts",
"typecheck": "tsc --noEmit"
},
"devDependencies": {
Expand Down
4 changes: 3 additions & 1 deletion src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ const _envShim = Object.create(null);
export type EnvObject = Record<string, string | undefined>;

const _getEnv = (useShim?: boolean) =>
(globalThis as unknown as { __env__: EnvObject }).__env__ ||
globalThis.process?.env ||
(import.meta as unknown as { env: EnvObject }).env ||
((globalThis as any).Deno?.env.toObject() as EnvObject) ||
(globalThis as unknown as { __env__: EnvObject }).__env__ ||
(useShim ? _envShim : globalThis);

export const env = new Proxy<EnvObject>(_envShim, {
Expand Down
7 changes: 5 additions & 2 deletions test/deno.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// @ts-ignore
// @ts-nocheck

import * as stdEnv from "../dist/index.mjs";

console.log(stdEnv.process.env.HOME);
Deno.env.set("FOOBAR", "baz");

console.log(stdEnv.process.env.FOOBAR);

0 comments on commit ceb04d5

Please sign in to comment.