UNPKG

@computerrock/toolchain-react-app

Version:

The Formation Framework template for starting react app project.

70 lines (59 loc) 1.95 kB
'use strict'; const fs = require('fs'); const path = require('path'); const paths = require('./paths'); const packageJSON = require(paths.appPackageJson); // make sure that including paths.js after env.js will read .env variables. delete require.cache[require.resolve('./paths')]; const NODE_ENV = process.env.NODE_ENV; if (!NODE_ENV) { throw new Error( 'The NODE_ENV environment variable is required but was not specified.', ); } const dotenvFiles = [ `${paths.dotenv}.${NODE_ENV}.local`, `${paths.dotenv}.${NODE_ENV}`, NODE_ENV !== 'test' && `${paths.dotenv}.local`, paths.dotenv, ].filter(Boolean); dotenvFiles.forEach(dotenvFile => { if (fs.existsSync(dotenvFile)) { require('dotenv-expand')( require('dotenv').config({ path: dotenvFile, }), ); } }); const appDirectory = fs.realpathSync(process.cwd()); process.env.NODE_PATH = (process.env.NODE_PATH || '') .split(path.delimiter) .filter(folder => folder && !path.isAbsolute(folder)) .map(folder => path.resolve(appDirectory, folder)) .join(path.delimiter); function getEnvironment(publicPath) { const currentTime = new Date(); const raw = Object.keys(process.env) .reduce( (env, key) => { env[key] = process.env[key]; return env; }, { NODE_ENV: process.env.NODE_ENV || 'development', PUBLIC_URL: publicPath, APP_VERSION: packageJSON.version, APP_BUILD: currentTime.toISOString(), }, ); // stringify all values so we can feed into Webpack DefinePlugin const stringified = { 'process.env': Object.keys(raw).reduce((env, key) => { env[key] = JSON.stringify(raw[key]); return env; }, {}), }; return {raw, stringified}; } module.exports = getEnvironment;