forked from fahrPool/fahrPool
74 lines
2.6 KiB
JavaScript
74 lines
2.6 KiB
JavaScript
#!/usr/bin/env node
|
|
// Rendert die beiden lokal benoetigten Config-Dateien aus ihren committeten
|
|
// .template-Vorlagen, mit den echten Secrets aus .env eingesetzt:
|
|
//
|
|
// volumes/db/init/99-roles.sql <- volumes/db/init/99-roles.sql.template
|
|
// volumes/api/kong.yml <- volumes/api/kong.yml.template
|
|
//
|
|
// Beide Templates enthalten Platzhalter im Format __VARNAME__. Ohne diesen
|
|
// Render-Schritt fehlen die tatsaechlich gemounteten Dateien und
|
|
// auth/rest/storage crashloopen bzw. Kong liefert 401 auf jede Route (siehe
|
|
// Abweichungen in .superpowers/sdd/task-4-report.md).
|
|
//
|
|
// Nutzung (siehe auch Kommentar-Kopf von generate-secrets.js):
|
|
// node scripts/render-templates.js
|
|
//
|
|
// Erwartet eine bereits vorhandene .env (siehe .env.example) mit
|
|
// POSTGRES_PASSWORD, ANON_KEY, SERVICE_ROLE_KEY.
|
|
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
|
|
const repoRoot = path.resolve(__dirname, "..");
|
|
const envPath = path.join(repoRoot, ".env");
|
|
|
|
function loadEnv(filePath) {
|
|
if (!fs.existsSync(filePath)) {
|
|
console.error(
|
|
`Fehler: ${filePath} nicht gefunden. Erst .env aus .env.example anlegen ` +
|
|
`(Werte mit "node scripts/generate-secrets.js" erzeugen).`
|
|
);
|
|
process.exit(1);
|
|
}
|
|
const env = {};
|
|
for (const rawLine of fs.readFileSync(filePath, "utf8").split("\n")) {
|
|
const line = rawLine.trim();
|
|
if (!line || line.startsWith("#")) continue;
|
|
const eq = line.indexOf("=");
|
|
if (eq === -1) continue;
|
|
env[line.slice(0, eq).trim()] = line.slice(eq + 1).trim();
|
|
}
|
|
return env;
|
|
}
|
|
|
|
function renderTemplate(templateRelPath, outputRelPath, replacements) {
|
|
const templatePath = path.join(repoRoot, templateRelPath);
|
|
const outputPath = path.join(repoRoot, outputRelPath);
|
|
let content = fs.readFileSync(templatePath, "utf8");
|
|
for (const [placeholder, value] of Object.entries(replacements)) {
|
|
content = content.split(placeholder).join(value);
|
|
}
|
|
fs.writeFileSync(outputPath, content);
|
|
console.log(`Geschrieben: ${outputRelPath} (aus ${templateRelPath})`);
|
|
}
|
|
|
|
const env = loadEnv(envPath);
|
|
|
|
for (const key of ["POSTGRES_PASSWORD", "ANON_KEY", "SERVICE_ROLE_KEY"]) {
|
|
if (!env[key]) {
|
|
console.error(`Fehler: ${key} fehlt oder ist leer in .env.`);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
renderTemplate(
|
|
path.join("volumes", "db", "init", "99-roles.sql.template"),
|
|
path.join("volumes", "db", "init", "99-roles.sql"),
|
|
{ __POSTGRES_PASSWORD__: env.POSTGRES_PASSWORD }
|
|
);
|
|
|
|
renderTemplate(path.join("volumes", "api", "kong.yml.template"), path.join("volumes", "api", "kong.yml"), {
|
|
__ANON_KEY__: env.ANON_KEY,
|
|
__SERVICE_ROLE_KEY__: env.SERVICE_ROLE_KEY,
|
|
});
|