21621: Add copy button to virtual code snippet for io panel json tab
[arvados.git] / services / workbench2 / config / env.js
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 'use strict';
6
7 const fs = require('fs');
8 const path = require('path');
9 const paths = require('./paths');
10
11 // Make sure that including paths.js after env.js will read .env variables.
12 delete require.cache[require.resolve('./paths')];
13
14 const NODE_ENV = process.env.NODE_ENV;
15 if (!NODE_ENV) {
16   throw new Error(
17     'The NODE_ENV environment variable is required but was not specified.'
18   );
19 }
20
21 // https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use
22 const dotenvFiles = [
23   `${paths.dotenv}.${NODE_ENV}.local`,
24   // Don't include `.env.local` for `test` environment
25   // since normally you expect tests to produce the same
26   // results for everyone
27   NODE_ENV !== 'test' && `${paths.dotenv}.local`,
28   `${paths.dotenv}.${NODE_ENV}`,
29   paths.dotenv,
30 ].filter(Boolean);
31
32 // Load environment variables from .env* files. Suppress warnings using silent
33 // if this file is missing. dotenv will never modify any environment variables
34 // that have already been set.  Variable expansion is supported in .env files.
35 // https://github.com/motdotla/dotenv
36 // https://github.com/motdotla/dotenv-expand
37 dotenvFiles.forEach(dotenvFile => {
38   if (fs.existsSync(dotenvFile)) {
39     require('dotenv-expand')(
40       require('dotenv').config({
41         path: dotenvFile,
42       })
43     );
44   }
45 });
46
47 // We support resolving modules according to `NODE_PATH`.
48 // This lets you use absolute paths in imports inside large monorepos:
49 // https://github.com/facebook/create-react-app/issues/253.
50 // It works similar to `NODE_PATH` in Node itself:
51 // https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders
52 // Note that unlike in Node, only *relative* paths from `NODE_PATH` are honored.
53 // Otherwise, we risk importing Node.js core modules into an app instead of webpack shims.
54 // https://github.com/facebook/create-react-app/issues/1023#issuecomment-265344421
55 // We also resolve them to make sure all tools using them work consistently.
56 const appDirectory = fs.realpathSync(process.cwd());
57 process.env.NODE_PATH = (process.env.NODE_PATH || '')
58   .split(path.delimiter)
59   .filter(folder => folder && !path.isAbsolute(folder))
60   .map(folder => path.resolve(appDirectory, folder))
61   .join(path.delimiter);
62
63 // Grab NODE_ENV and REACT_APP_* environment variables and prepare them to be
64 // injected into the application via DefinePlugin in webpack configuration.
65 const REACT_APP = /^REACT_APP_/i;
66
67 function getClientEnvironment(publicUrl) {
68   const raw = Object.keys(process.env)
69     .filter(key => REACT_APP.test(key))
70     .reduce(
71       (env, key) => {
72         env[key] = process.env[key];
73         return env;
74       },
75       {
76         // Useful for determining whether we’re running in production mode.
77         // Most importantly, it switches React into the correct mode.
78         NODE_ENV: process.env.NODE_ENV || 'development',
79         // Useful for resolving the correct path to static assets in `public`.
80         // For example, <img src={process.env.PUBLIC_URL + '/img/logo.png'} />.
81         // This should only be used as an escape hatch. Normally you would put
82         // images into the `src` and `import` them in code to get their paths.
83         PUBLIC_URL: publicUrl,
84         // We support configuring the sockjs pathname during development.
85         // These settings let a developer run multiple simultaneous projects.
86         // They are used as the connection `hostname`, `pathname` and `port`
87         // in webpackHotDevClient. They are used as the `sockHost`, `sockPath`
88         // and `sockPort` options in webpack-dev-server.
89         WDS_SOCKET_HOST: process.env.WDS_SOCKET_HOST,
90         WDS_SOCKET_PATH: process.env.WDS_SOCKET_PATH,
91         WDS_SOCKET_PORT: process.env.WDS_SOCKET_PORT,
92         // Whether or not react-refresh is enabled.
93         // It is defined here so it is available in the webpackHotDevClient.
94         FAST_REFRESH: process.env.FAST_REFRESH !== 'false',
95       }
96     );
97   // Stringify all values so we can feed into webpack DefinePlugin
98   const stringified = {
99     'process.env': Object.keys(raw).reduce((env, key) => {
100       env[key] = JSON.stringify(raw[key]);
101       return env;
102     }, {}),
103   };
104
105   return { raw, stringified };
106 }
107
108 module.exports = getClientEnvironment;