UNPKG

1.52 kBJavaScriptView Raw
1/**
2 * Cached process.env
3 *
4 * @type {Record<string, string>}
5 */
6export let environment = {};
7
8/**
9 * This should only be necessary when you or a package mutates the environment variables.
10 * The `mainFn` / `mainTestFn` / `mainBenchFn` / ... will call this function by default
11 * after loading your `.env` file.
12 *
13 * Accessing an environment variable via `process.env.XXXX` is relatively slow compared
14 * to direct property access. As can be seen in the following benchmark:
15 *
16 * ```txt
17 * property access 500000000 iterations 0 ns/op
18 * process.env access 5000000 iterations 246 ns/op
19 * ```
20 *
21 * See this thread: https://github.com/nodejs/node/issues/3104 for more information.
22 *
23 * @since 0.1.0
24 * @summary Repopulate the cached environment copy.
25 *
26 * @returns {void}
27 */
28export function refreshEnvironmentCache() {
29 environment = JSON.parse(JSON.stringify(process.env));
30}
31
32/**
33 * Returns true when the `NODE_ENV` variable is not set, or when it does not equal to
34 * `development`. This allows for a 'safe by default' experience.
35 *
36 * @since 0.1.0
37 *
38 * @returns {boolean}
39 */
40export function isProduction() {
41 return environment.NODE_ENV !== "development";
42}
43
44/**
45 * Returns true when `NODE_ENV` is explicitly set to 'development' or when the
46 * environment variable `IS_STAGING` is explicitly set to 'true'.
47 *
48 * @since 0.1.0
49 *
50 * @returns {boolean}
51 */
52export function isStaging() {
53 return (
54 environment.NODE_ENV === "development" || environment.IS_STAGING === "true"
55 );
56}