UNPKG

probot

Version:

A framework for building GitHub Apps to automate and improve your workflow

31 lines (30 loc) 887 B
import { readFileSync, writeFileSync } from "node:fs"; import { join as pathJoin } from "node:path"; import dotenv from "dotenv"; function escapeNewlines(str) { return str.replace(/\n/g, "\\n"); } function format(key, value) { return `${key}=${escapeNewlines(value)}`; } export function updateEnv(env) { const filename = pathJoin(process.cwd(), ".env"); // Merge with existing values try { const existing = dotenv.parse(readFileSync(filename, "utf-8")); env = Object.assign(existing, env); } catch (err) { if (err.code !== "ENOENT") { throw err; } } const contents = Object.keys(env) .map((key) => format(key, env[key])) .join("\n"); // Write to file writeFileSync(filename, contents); // Update current env with new values Object.assign(process.env, env); return env; }