From 3a35ed407358eae40a84045921371687f33bb7a5 Mon Sep 17 00:00:00 2001 From: Kai Ritthaler Date: Thu, 15 May 2025 16:24:40 +0200 Subject: [PATCH] .env generator --- .gitignore | 2 +- code/backend/.env.example | 2 ++ code/backend/package.json | 3 ++- code/backend/scripts/install.ts | 29 ++++++++++++++++++++++++++++- code/backend/yarn.lock | 5 +++++ 5 files changed, 38 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index ff53806..caf5ad7 100644 --- a/.gitignore +++ b/.gitignore @@ -132,7 +132,7 @@ code/frontend/node_modules # production build/ - +dist/ # misc .DS_Store .env.local diff --git a/code/backend/.env.example b/code/backend/.env.example index 85e1c59..54cc487 100644 --- a/code/backend/.env.example +++ b/code/backend/.env.example @@ -1,2 +1,4 @@ DATABASE_URL="postgres://postgres:YourPassword@localhost:5432/prisma" TOKEN_SECRET=ThisIsVeryImportantChangeThisToSomethingSecureThisIsUsedToEncryptTheJWT +DB_USER=postgres +DB_PASSWORD=YourPassword \ No newline at end of file diff --git a/code/backend/package.json b/code/backend/package.json index c31cc10..4a7ec11 100644 --- a/code/backend/package.json +++ b/code/backend/package.json @@ -6,6 +6,7 @@ "dependencies": { "@prisma/client": "^6.7.0", "@types/bcryptjs": "^3.0.0", + "@types/readline-sync": "^1.4.8", "bcryptjs": "^3.0.2", "cors": "^2.8.5", "dotenv": "^16.5.0", @@ -32,7 +33,7 @@ "typescript": "^5.8.3" }, "scripts": { - "start-containers": "docker-compose -f ../db/docker-compose.yaml up -d", + "start-containers": "docker-compose -f ../db/docker-compose.yaml --env-file .env up -d", "stop-containers": "docker-compose down", "start": "yarn start-containers && nodemon src/server.ts", "start-no-docker": "nodemon src/server.ts", diff --git a/code/backend/scripts/install.ts b/code/backend/scripts/install.ts index 17642c0..db63139 100644 --- a/code/backend/scripts/install.ts +++ b/code/backend/scripts/install.ts @@ -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 { diff --git a/code/backend/yarn.lock b/code/backend/yarn.lock index 8d1ac5e..b187f62 100644 --- a/code/backend/yarn.lock +++ b/code/backend/yarn.lock @@ -349,6 +349,11 @@ resolved "https://registry.yarnpkg.com/@types/range-parser/-/range-parser-1.2.7.tgz#50ae4353eaaddc04044279812f52c8c65857dbcb" integrity sha512-hKormJbkJqzQGhziax5PItDUTMAM9uE2XXQmM37dyd4hVM+5aVl7oVxMVUiVQn2oCQFN/LKCZdvSM0pFRqbSmQ== +"@types/readline-sync@^1.4.8": + version "1.4.8" + resolved "https://registry.yarnpkg.com/@types/readline-sync/-/readline-sync-1.4.8.tgz#dc9767a93fc83825d90331f2549a2e90fc3255f0" + integrity sha512-BL7xOf0yKLA6baAX6MMOnYkoflUyj/c7y3pqMRfU0va7XlwHAOTOIo4x55P/qLfMsuaYdJJKubToLqRVmRtRZA== + "@types/send@*": version "0.17.4" resolved "https://registry.yarnpkg.com/@types/send/-/send-0.17.4.tgz#6619cd24e7270793702e4e6a4b958a9010cfc57a"