diff --git a/README.md b/README.md index 524e314..454eaf9 100644 --- a/README.md +++ b/README.md @@ -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). diff --git a/src/_process.ts b/src/_process.ts new file mode 100644 index 0000000..f8cc217 --- /dev/null +++ b/src/_process.ts @@ -0,0 +1,2 @@ +export const _process: typeof process = + typeof process === "undefined" ? ({} as typeof process) : process; diff --git a/src/_utils.ts b/src/_utils.ts new file mode 100644 index 0000000..2854da0 --- /dev/null +++ b/src/_utils.ts @@ -0,0 +1,3 @@ +export function toBoolean(val) { + return val ? val !== "false" : false; +} diff --git a/src/flags.ts b/src/flags.ts new file mode 100644 index 0000000..c42d693 --- /dev/null +++ b/src/flags.ts @@ -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); diff --git a/src/index.ts b/src/index.ts index 2c7cd22..09a3eb4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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"; diff --git a/test/index.test.ts b/test/index.test.ts index 0345245..f2d9b7e 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -5,6 +5,7 @@ describe("std-env", () => { it("has expected exports", () => { expect(Object.keys(stdEnv)).toMatchInlineSnapshot(` [ + "detectProvider", "env", "nodeENV", "platform",