probot
Version:
A framework for building GitHub Apps to automate and improve your workflow
26 lines (25 loc) • 830 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(values, options = {}) {
const path = options.path || pathJoin(process.cwd(), ".env");
// Merge with existing values
const env = Object.assign(loadEnv({
path,
}), values);
const contents = Object.entries(env)
.filter((entry) => entry[1] !== undefined)
.map(([key, value]) => format(key, value))
.join("\n");
// Write to file
writeFileSync(path, contents);
// Update current env with new values
Object.assign(options.env || process.env, env);
return env;
}