-
Notifications
You must be signed in to change notification settings - Fork 0
/
propagate-env.js
29 lines (21 loc) · 1011 Bytes
/
propagate-env.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const fs = require("fs");
const dotenv = require("dotenv");
const config = dotenv.config();
let whitelisted = Object.keys(process.env)
.filter((key) => key.startsWith("MSQ_"))
.reduce((prev, cur) => ({ ...prev, [`VITE_${cur}`]: JSON.stringify(process.env[cur]) }), {});
whitelisted = Object.keys(config.parsed)
.filter((key) => key.startsWith("CANISTER_ID_"))
.map((key) => key.toUpperCase())
.reduce((prev, cur) => ({ ...prev, [`VITE_${cur}`]: JSON.stringify(config.parsed[cur]) }), whitelisted);
let mode = process.env.MSQ_MODE.toLowerCase();
if (mode !== "dev" && mode !== "prod") {
throw new Error("Invalid mode:", mode);
}
console.log("Whitelisted env variables:", JSON.stringify(whitelisted, 2));
const envFileName = `.env.${mode === "prod" ? "production" : mode}`;
const newEnv = Object.keys(whitelisted)
.map((cur) => `${cur}=${whitelisted[cur]}`)
.join("\n");
fs.writeFileSync(envFileName, newEnv);
console.log("Successfully written whitelisted env variables to", envFileName);