Permalink
Browse files

@phenomic/core: support .env(.*)? files for PHENOMIC_* env vars

  • Loading branch information...
MoOx committed Mar 15, 2018
1 parent d78bb32 commit 62378ca14bc87365b5b2b0448254b64acdce1944
@@ -0,0 +1,7 @@
declare module "dotenv" {
declare var exports: any;
}
declare module "dotenv-expand" {
declare var exports: any;
}
@@ -18,6 +18,8 @@
"cosmiconfig": "^2.1.1",
"debug": "^2.6.0",
"deep-assign": "^2.0.0",
"dotenv": "^5.0.1",
"dotenv-expand": "^4.2.0",
"express": "^4.14.0",
"get-port": "^2.1.0",
"globby": "^6.1.0",
@@ -1,20 +1,54 @@
// inspired from https://github.com/facebook/create-react-app/blob/779dad55465de81972ec72257c734e4afae17094/packages/react-scripts/config/env.js
import fs from "fs";
import path from "path";
// Grab NODE_ENV and PHENOMIC_* environment variables and prepare them to be
// injected into the application.
const PHENOMIC = /^PHENOMIC_/i;
const NODE_ENV = process.env.NODE_ENV || "development";
const RE_PHENOMIC = /^PHENOMIC_/i;
const root = path.join(process.cwd(), ".env");
// https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use
// Don't include `.env.local` for `test` environment
// since normally you expect tests to produce the same
// results for everyone
const dotenvFiles =
NODE_ENV !== "test"
? [
`${root}.${NODE_ENV}.local`,
`${root}.${NODE_ENV}`,
`${root}.local`,
root
]
: [`${root}.${NODE_ENV}`, root];
// Load environment variables from .env* files. Suppress warnings using silent
// if this file is missing. dotenv will never modify any environment variables
// that have already been set. Variable expansion is supported in .env files.
// https://github.com/motdotla/dotenv
// https://github.com/motdotla/dotenv-expand
dotenvFiles.forEach(dotenvFile => {
if (fs.existsSync(dotenvFile)) {
require("dotenv-expand")(
require("dotenv").config({
path: dotenvFile
})
);
}
});
export default function getClientEnvironment(config: PhenomicConfig) {
process.env.PHENOMIC_APP_BASENAME = config.baseUrl.pathname;
return Object.keys(process.env)
.filter(key => PHENOMIC.test(key))
.filter(key => RE_PHENOMIC.test(key))
.reduce(
(env, key) => {
env[key] = process.env[key];
return env;
},
{
NODE_ENV: process.env.NODE_ENV
NODE_ENV
}
);
}

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

0 comments on commit 62378ca

Please sign in to comment.