probot
Version:
A framework for building GitHub Apps to automate and improve your workflow
25 lines (24 loc) • 713 B
JavaScript
import { writeFileSync } from "node:fs";
import { join as pathJoin } from "node:path";
import { loadEnv } from "./load-env.js";
function escapeNewlines(str) {
return str.replace(/\n/g, "\\n");
}
function format(key, value) {
return `${key}=${escapeNewlines(value)}`;
}
export function updateEnv(env) {
const path = pathJoin(process.cwd(), ".env");
// Merge with existing values
env = Object.assign(loadEnv({
path,
}), env);
const contents = Object.keys(env)
.map((key) => format(key, env[key]))
.join("\n");
// Write to file
writeFileSync(path, contents);
// Update current env with new values
Object.assign(process.env, env);
return env;
}