Completed
Push — master ( c12867...9cb664 )
by Junior
28s
created

env.js ➔ getClientEnvironment   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
c 1
b 0
f 1
nc 1
dl 0
loc 29
rs 8.8571
nop 1

1 Function

Rating   Name   Duplication   Size   Complexity  
A env.js ➔ ... ➔ ??? 0 1 1
1
'use strict';
2
3
const fs = require('fs');
4
const path = require('path');
5
const paths = require('./paths');
6
7
// Make sure that including paths.js after env.js will read .env variables.
8
delete require.cache[require.resolve('./paths')];
9
10
const NODE_ENV = process.env.NODE_ENV;
11
if (!NODE_ENV) {
12
  throw new Error(
13
    'The NODE_ENV environment variable is required but was not specified.'
14
  );
15
}
16
17
// https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use
18
var dotenvFiles = [
19
  `${paths.dotenv}.${NODE_ENV}.local`,
20
  `${paths.dotenv}.${NODE_ENV}`,
21
  // Don't include `.env.local` for `test` environment
22
  // since normally you expect tests to produce the same
23
  // results for everyone
24
  NODE_ENV !== 'test' && `${paths.dotenv}.local`,
25
  paths.dotenv,
26
].filter(Boolean);
27
28
// Load environment variables from .env* files. Suppress warnings using silent
29
// if this file is missing. dotenv will never modify any environment variables
30
// that have already been set.
31
// https://github.com/motdotla/dotenv
32
dotenvFiles.forEach(dotenvFile => {
33
  if (fs.existsSync(dotenvFile)) {
34
    require('dotenv').config({
35
      path: dotenvFile,
36
    });
37
  }
38
});
39
40
// We support resolving modules according to `NODE_PATH`.
41
// This lets you use absolute paths in imports inside large monorepos:
42
// https://github.com/facebookincubator/create-react-app/issues/253.
43
// It works similar to `NODE_PATH` in Node itself:
44
// https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders
45
// Note that unlike in Node, only *relative* paths from `NODE_PATH` are honored.
46
// Otherwise, we risk importing Node.js core modules into an app instead of Webpack shims.
47
// https://github.com/facebookincubator/create-react-app/issues/1023#issuecomment-265344421
48
// We also resolve them to make sure all tools using them work consistently.
49
const appDirectory = fs.realpathSync(process.cwd());
50
process.env.NODE_PATH = (process.env.NODE_PATH || '')
51
  .split(path.delimiter)
52
  .filter(folder => folder && !path.isAbsolute(folder))
53
  .map(folder => path.resolve(appDirectory, folder))
54
  .join(path.delimiter);
55
56
// Grab NODE_ENV and REACT_APP_* environment variables and prepare them to be
57
// injected into the application via DefinePlugin in Webpack configuration.
58
const REACT_APP = /^REACT_APP_/i;
59
60
function getClientEnvironment(publicUrl) {
61
  const raw = Object.keys(process.env)
62
    .filter(key => REACT_APP.test(key))
63
    .reduce(
64
      (env, key) => {
65
        env[key] = process.env[key];
66
        return env;
67
      },
68
      {
69
        // Useful for determining whether we’re running in production mode.
70
        // Most importantly, it switches React into the correct mode.
71
        NODE_ENV: process.env.NODE_ENV || 'development',
72
        // Useful for resolving the correct path to static assets in `public`.
73
        // For example, <img src={process.env.PUBLIC_URL + '/img/logo.png'} />.
74
        // This should only be used as an escape hatch. Normally you would put
75
        // images into the `src` and `import` them in code to get their paths.
76
        PUBLIC_URL: publicUrl,
77
      }
78
    );
79
  // Stringify all values so we can feed into Webpack DefinePlugin
80
  const stringified = {
81
    'process.env': Object.keys(raw).reduce((env, key) => {
82
      env[key] = JSON.stringify(raw[key]);
83
      return env;
84
    }, {}),
85
  };
86
87
  return { raw, stringified };
88
}
89
90
module.exports = getClientEnvironment;
91