Skip to content

Commit

Permalink
refactor: split flags
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Aug 9, 2023
1 parent b80677b commit 584e9f4
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 64 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Available exports:
- `provider`
- `isColorSupported`

You can read more about how each flag works from [./src/index.ts](./src/index.ts).
You can read more about how each flag works from [./src/flags.ts](./src/flags.ts).

List of well known providers can be found from [./src/providers.ts](./src/providers.ts).

Expand Down
2 changes: 2 additions & 0 deletions src/_process.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const _process: typeof process =
typeof process === "undefined" ? ({} as typeof process) : process;
3 changes: 3 additions & 0 deletions src/_utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function toBoolean(val) {
return val ? val !== "false" : false;
}
53 changes: 53 additions & 0 deletions src/flags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { detectProvider, ProviderName } from "./providers";
import { env, nodeENV } from "./env";
import { toBoolean } from "./_utils";
import { _process } from "./_process";

const _providerInfo = detectProvider(env);

/** Value of process.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(env.CI) || _providerInfo.ci !== false;

/** Detect if stdout.TTY is available */
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(env.DEBUG);

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

/** Detect if `NODE_ENV` environment variable is `production` */
export const isProduction = nodeENV === "production";

/** Detect if `NODE_ENV` environment variable is `dev` or `development` */
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(env.MINIMAL) || isCI || isTest || !hasTTY;

/** Detect if process.platform is Windows */
export const isWindows = /^win/i.test(platform);

/** Detect if process.platform is Linux */
export const isLinux = /^linux/i.test(platform);

/** Detect if process.platform is macOS (darwin kernel) */
export const isMacOS = /^darwin/i.test(platform);

/** Color Support */
export const isColorSupported =
!toBoolean(env.NO_COLOR) &&
(toBoolean(env.FORCE_COLOR) ||
(isWindows && !(env.TERM === "dumb")) ||
(hasTTY && env.TERM && !(env.TERM !== "dumb")) ||
isCI);
66 changes: 3 additions & 63 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,3 @@
import { detectProvider, ProviderName } from "./providers";
import { env, nodeENV } from "./env";
export { env, nodeENV } from "./env";

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

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

/** Value of process.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(env.CI) || providerInfo.ci !== false;

/** Detect if stdout.TTY is available */
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(env.DEBUG);

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

/** Detect if `NODE_ENV` environment variable is `production` */
export const isProduction = nodeENV === "production";

/** Detect if `NODE_ENV` environment variable is `dev` or `development` */
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(env.MINIMAL) || isCI || isTest || !hasTTY;

/** Detect if process.platform is Windows */
export const isWindows = /^win/i.test(platform);

/** Detect if process.platform is Linux */
export const isLinux = /^linux/i.test(platform);

/** Detect if process.platform is macOS (darwin kernel) */
export const isMacOS = /^darwin/i.test(platform);

/** Color Support */
export const isColorSupported =
!toBoolean(env.NO_COLOR) &&
(toBoolean(env.FORCE_COLOR) ||
(isWindows && !(env.TERM === "dumb")) ||
(hasTTY && env.TERM && !(env.TERM !== "dumb")) ||
isCI);

// -- Utils --

function toBoolean(val) {
return val ? val !== "false" : false;
}
export * from "./providers";
export * from "./env";
export * from "./flags";
1 change: 1 addition & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ describe("std-env", () => {
it("has expected exports", () => {
expect(Object.keys(stdEnv)).toMatchInlineSnapshot(`
[
"detectProvider",
"env",
"nodeENV",
"platform",
Expand Down

0 comments on commit 584e9f4

Please sign in to comment.