.env generator

This commit is contained in:
Kai Ritthaler 2025-05-15 16:24:40 +02:00 committed by Rudi Regentonne
parent ec67454987
commit 3a35ed4073
5 changed files with 38 additions and 3 deletions

View file

@ -1,7 +1,9 @@
import { exec } from "child_process";
import util from "util";
const execPromise = util.promisify(exec);
import fs from "fs";
import fs, { read } from "fs";
import readlineSync from "readline-sync";
import crypto from "crypto";
const json_path: string = "scripts/install.json"; // Path to the JSON file
const raw = fs.readFileSync(json_path, "utf8");
@ -12,6 +14,31 @@ if (config.installed) {
console.log("Already installed");
process.exit(0);
}
// getting user input for the PostgreSQL username and password
console.log("generrating .env file");
const postgresUser: string = readlineSync.question(
"Enter the PostgreSQL username (default: postgres): ",
{ defaultInput: "postgres" }
);
let postgresPassword: string = "";
while (!postgresPassword) {
postgresPassword = readlineSync.question("Enter the PostgreSQL password: ", {
hideEchoBack: true, // Hides the characters
});
!postgresPassword && console.log("Password cannot be empty");
}
const jwtSecret: string = crypto.randomBytes(32).toString("hex"); // 64 Zeichen
const env: string = `DATABASE_URL="postgresql://${postgresUser}:${postgresPassword}@localhost:5432/prisma"
JWT_SECRET="${jwtSecret}"
DB_USER="${postgresUser}"
DB_PASSWORD="${postgresPassword}"`;
try {
fs.writeFileSync("./scripts/.env", env);
console.log("File has been written successfully.");
} catch (err) {
console.error("Error writing to file:", err);
}
(async () => {
for (const command of config.commands) {
try {