2023-05-06 12:22:00 +03:00
|
|
|
"use strict";
|
|
|
|
const fs = require("fs");
|
|
|
|
const path = require("path");
|
|
|
|
|
|
|
|
const prefix = "REACT_APP_";
|
2023-05-06 14:29:59 +03:00
|
|
|
const publicUrl = process.env.PUBLIC_URL || "";
|
|
|
|
const scriptPath = path.join("./application", publicUrl, "env.js");
|
2023-05-06 12:22:00 +03:00
|
|
|
|
|
|
|
function generateScriptContent() {
|
|
|
|
const prefixRegex = new RegExp(`^${prefix}`);
|
|
|
|
const env = process.env;
|
|
|
|
const config = Object.keys(env)
|
|
|
|
.filter(key => prefixRegex.test(key))
|
|
|
|
.reduce((c, key) => Object.assign({}, c, { [key]: env[key] }), {});
|
|
|
|
return `window.env=${JSON.stringify(config)};`;
|
|
|
|
}
|
|
|
|
|
|
|
|
function saveScriptContent(scriptContents) {
|
|
|
|
fs.writeFile(scriptPath, scriptContents, "utf8", function (err) {
|
|
|
|
if (err) throw err;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log("Setting environment variables...");
|
|
|
|
const scriptContent = generateScriptContent();
|
|
|
|
saveScriptContent(scriptContent);
|
|
|
|
console.log(
|
|
|
|
`Updated ${scriptPath} with ${prefix}* environment variables: ${scriptContent}.`
|
|
|
|
);
|