reloadEnv.js 832 B

12345678910111213141516171819202122232425262728
  1. export default async () => {
  2. const file = Bun.file(`.env`);
  3. if ((await file.exists())) {
  4. const text = await file.text();
  5. const lines = text.split("\n");
  6. for (const line of lines) {
  7. // Ignore comments and empty lines
  8. if (!line || line.trim().startsWith("#")) continue;
  9. // Match key=value pairs
  10. const match = line.match(/^\s*([\w.-]+)\s*=\s*(.*)?\s*$/);
  11. if (match) {
  12. const key = match[1];
  13. let value = match[2] || "";
  14. // Strip surrounding quotes if they exist
  15. value = value.replace(/^(['"])(.*)\1$/, "$2").trim();
  16. // Update the environment variable
  17. process.env[key] = value;
  18. }
  19. }
  20. };
  21. console.log(`.env reloaded`);
  22. }