Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@tiffany352
Created August 15, 2020 03:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tiffany352/75efb6146a55107c937de530122cd8f8 to your computer and use it in GitHub Desktop.
Save tiffany352/75efb6146a55107c937de530122cd8f8 to your computer and use it in GitHub Desktop.
import fs from "fs";
export function config() {
const contents = fs.readFileSync(".env", {
flag: "r",
encoding: "utf-8",
});
for (let line of contents.split("\n")) {
line = line.split("#")[0] || line;
line = line.trim();
line = line.replace(/^export\s+/, "");
if (line == "") {
continue;
}
let [key, value] = line.split("=");
if (value.match(/^"(.*)"$/)) {
value = value.replace(/^"([^"]*)"$/, "$1");
} else if (value.match(/^'(.*)'$/)) {
value = value.replace(/^'([^']*)'$/, "$1");
}
process.env[key] = value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment