Skip to content

Commit

Permalink
Merge pull request #252 from UniversityRadioYork/marks/build-baps
Browse files Browse the repository at this point in the history
Rework BAPS build to avoid NODE_ENV problems
  • Loading branch information
mstratford authored Nov 3, 2021
2 parents bddd49f + 63ec853 commit 7cfd48b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
20 changes: 12 additions & 8 deletions config/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,31 @@ if (!NODE_ENV) {
);
}

const BAPSICLE =
(process.env.REACT_APP_BAPSICLE_INTERFACE || "false") == "true";

// https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use
const dotenvFiles = [
`${paths.dotenv}.${NODE_ENV}.local`,
BAPSICLE && `${paths.dotenv}.baps-${NODE_ENV}`,
`${paths.dotenv}.${NODE_ENV}`,
// Don't include `.env.local` for `test` environment
// since normally you expect tests to produce the same
// results for everyone
NODE_ENV !== "test" && `${paths.dotenv}.local`,
paths.dotenv
paths.dotenv,
].filter(Boolean);

// 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 => {
dotenvFiles.forEach((dotenvFile) => {
if (fs.existsSync(dotenvFile)) {
require("dotenv-expand")(
require("dotenv").config({
path: dotenvFile
path: dotenvFile,
})
);
}
Expand All @@ -52,8 +56,8 @@ dotenvFiles.forEach(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))
.filter((folder) => folder && !path.isAbsolute(folder))
.map((folder) => path.resolve(appDirectory, folder))
.join(path.delimiter);

// Grab NODE_ENV and REACT_APP_* environment variables and prepare them to be
Expand All @@ -62,7 +66,7 @@ const REACT_APP = /^REACT_APP_/i;

function getClientEnvironment(publicUrl) {
const raw = Object.keys(process.env)
.filter(key => REACT_APP.test(key))
.filter((key) => REACT_APP.test(key))
.reduce(
(env, key) => {
env[key] = process.env[key];
Expand All @@ -79,15 +83,15 @@ function getClientEnvironment(publicUrl) {
PUBLIC_URL: publicUrl,
// This is a bodge.
// See https://github.com/bunkat/later/issues/155
LATER_COV: false
LATER_COV: false,
}
);
// 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 };
Expand Down
10 changes: 8 additions & 2 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ process.env.NODE_ENV = "production";
// If we want BAPS, specify it as the first command line argument.
var args = process.argv.slice(2); // Remove node start.js
if (args.length > 0 && args[0] === "baps") {
process.env.NODE_ENV = "baps-production";
// We set this here first; later on, in env.js, we'll reference it to load in
// the other variables from .env.baps-${NODE_ENV}
process.env.REACT_APP_BAPSICLE_INTERFACE = "true";
}

// Makes the script crash on unhandled rejections instead of silently
Expand Down Expand Up @@ -147,7 +149,11 @@ function build(previousFileSizes) {
console.log();
}

console.log("Creating an optimized production build...");
console.log(
`Creating an optimized production ${
process.env.REACT_APP_BAPSICLE_INTERFACE === "true" ? "BAPS" : "WebStudio"
} build...`
);

const compiler = webpack(config);
return new Promise((resolve, reject) => {
Expand Down
4 changes: 3 additions & 1 deletion scripts/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ process.env.NODE_ENV = "development";
// If we want BAPS, specify it as the first command line argument.
var args = process.argv.slice(2); // Remove node start.js
if (args.length > 0 && args[0] === "baps") {
process.env.NODE_ENV = "baps-development";
// We set this here first; later on, in env.js, we'll reference it to load in
// the other variables from .env.baps-${NODE_ENV}
process.env.REACT_APP_BAPSICLE_INTERFACE = "true";
}

// Makes the script crash on unhandled rejections instead of silently
Expand Down

0 comments on commit 7cfd48b

Please sign in to comment.