Skip to content

Commit

Permalink
chore: fix internal types and expose EnvObject
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Aug 9, 2023
1 parent 0694e19 commit 058abc6
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"prepack": "unbuild",
"release": "pnpm test && changelogen --release && npm publish && git push --follow-tags",
"test": "pnpm lint && pnpm typecheck && vitest run --coverage",
"typecheck": "vitest typecheck"
"typecheck": "tsc --noEmit"
},
"devDependencies": {
"@types/node": "^20.4.9",
Expand Down
2 changes: 1 addition & 1 deletion src/_utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export function toBoolean(val) {
export function toBoolean(val: boolean | string | undefined) {
return val ? val !== "false" : false;
}
6 changes: 4 additions & 2 deletions src/env.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const _envShim = Object.create(null);

export type EnvObject = Record<string, string | undefined>;

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

export const env = new Proxy<Record<string, string | undefined>>(_envShim, {
export const env = new Proxy<EnvObject>(_envShim, {
get(_, prop) {
const env = _getEnv();
return env[prop as any] ?? _envShim[prop];
Expand Down
10 changes: 5 additions & 5 deletions src/process.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import { env } from "./env";
import { EnvObject, env } from "./env";

export interface Process
extends Partial<Omit<typeof globalThis.process, "versions">> {
env: Record<string, string | undefined>;
env: EnvObject;
versions: Record<string, string>;
}

const _process = (globalThis.process ||
Object.create(null)) as unknown as Process;

const processShims = {
const processShims: Partial<Process> = {
versions: {},
};

export const process = new Proxy<Process>(_process, {
get(target, prop) {
get(target, prop: keyof Process) {
if (prop === "env") {
return env;
}
if (prop in target) {
return target[prop];
}
if (prop in processShims) {
return processShims[prop as keyof typeof processShims];
return processShims[prop];
}
},
});

0 comments on commit 058abc6

Please sign in to comment.