Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: universal env proxy #61

Merged
merged 1 commit into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const { isCI } = require('std-env')

Available exports:

- `env`
- `nodeENV`
- `hasTTY`
- `hasWindow`
- `isCI`
Expand Down
39 changes: 39 additions & 0 deletions src/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const _envShim = Object.create(null);

const _getEnv = (useShim?: boolean) =>
globalThis.__env__ ||
globalThis.process?.env ||
(useShim ? _envShim : globalThis);

export const env = new Proxy(_envShim, {
get(_, prop) {
const env = _getEnv();
return env[prop as any] ?? _envShim[prop];
},
has(_, prop) {
const env = _getEnv();
return prop in env || prop in _envShim;
},
set(_, prop, value) {
const env = _getEnv(true);
env[prop as any] = value;
return true;
},
deleteProperty(_, prop) {
if (!prop) {
return false;
}
const env = _getEnv(true);
delete env[prop as any];
return true;
},
ownKeys() {
const env = _getEnv();
return Object.keys(env);
},
});

export const nodeENV =
(typeof process !== "undefined" && process.env && process.env.NODE_ENV) ||
env.NODE_ENV ||
"";
22 changes: 10 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
import { detectProvider, ProviderName } from "./providers";
import { env, nodeENV } from "./env";
export { env, nodeENV } from "./env";

export type { ProviderName, ProviderInfo } from "./providers";

const processShim: typeof process =
const _process: typeof process =
typeof process !== "undefined" ? process : ({} as typeof process);
const envShim = processShim.env || ({} as typeof process.env);
const envShim = _process.env || ({} as typeof process.env);
const providerInfo = detectProvider(envShim);

const nodeENV =
(typeof process !== "undefined" && process.env && process.env.NODE_ENV) || "";

/** Value of process.platform */
export const platform = processShim.platform;
export const platform = _process.platform;

/** Current provider name */
export const provider: ProviderName = providerInfo.name;

/** Detect if `CI` environment variable is set or a provider CI detected */
export const isCI = toBoolean(envShim.CI) || providerInfo.ci !== false;
export const isCI = toBoolean(env.CI) || providerInfo.ci !== false;

/** Detect if stdout.TTY is available */
export const hasTTY = toBoolean(processShim.stdout && processShim.stdout.isTTY);
export const hasTTY = toBoolean(_process.stdout && _process.stdout.isTTY);

/** Detect if global `window` object is available */
export const hasWindow = typeof window !== "undefined";

/** Detect if `DEBUG` environment variable is set */
export const isDebug = toBoolean(envShim.DEBUG);
export const isDebug = toBoolean(env.DEBUG);

/** Detect if `NODE_ENV` environment variable is `test` */
export const isTest = nodeENV === "test" || toBoolean(envShim.TEST);
export const isTest = nodeENV === "test" || toBoolean(env.TEST);

/** Detect if `NODE_ENV` environment variable is `production` */
export const isProduction = nodeENV === "production";
Expand All @@ -38,8 +37,7 @@ export const isProduction = nodeENV === "production";
export const isDevelopment = nodeENV === "dev" || nodeENV === "development";

/** Detect if MINIMAL environment variable is set, running in CI or test or TTY is unavailable */
export const isMinimal =
toBoolean(envShim.MINIMAL) || isCI || isTest || !hasTTY;
export const isMinimal = toBoolean(env.MINIMAL) || isCI || isTest || !hasTTY;

/** Detect if process.platform is Windows */
export const isWindows = /^win/i.test(platform);
Expand Down
2 changes: 2 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ describe("std-env", () => {
it("has expected exports", () => {
expect(Object.keys(stdEnv)).toMatchInlineSnapshot(`
[
"env",
"nodeENV",
"platform",
"provider",
"isCI",
Expand Down
Loading