// Gemeinsame Einstellungen und Helfer für die Renovate-Skripte (plattformunabhängig: Windows/Linux/macOS). import { spawnSync } from "node:child_process"; import { fileURLToPath } from "node:url"; import path from "node:path"; const env = process.env; export const ROOT_DIR = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); export const STATE_DIR = path.join(ROOT_DIR, ".renovate-local"); export const RENOVATE_VERSION = env.RENOVATE_VERSION ?? "44.149.3"; export const FORGEJO_IMAGE = env.FORGEJO_IMAGE ?? "codeberg.org/forgejo/forgejo:11"; export const FORGEJO_CONTAINER = env.FORGEJO_CONTAINER ?? "renovate-forgejo"; export const FORGEJO_VOLUME = env.FORGEJO_VOLUME ?? "renovate-forgejo-data"; export const FORGEJO_PORT = env.FORGEJO_PORT ?? "3000"; export const FORGEJO_URL = `http://localhost:${FORGEJO_PORT}`; export const FORGEJO_USER = env.FORGEJO_USER ?? "renovate"; export const FORGEJO_PASSWORD = env.FORGEJO_PASSWORD ?? "renovate-local-123"; export const FORGEJO_REPO = env.FORGEJO_REPO ?? "monorepo"; export const GIT_REMOTE = env.GIT_REMOTE ?? "forgejo"; const isWindows = process.platform === "win32"; export function log(msg) { console.log(`\x1b[1;34m==>\x1b[0m ${msg}`); } /** Führt einen Befehl aus. Bricht bei Fehler ab, außer `allowFail` ist gesetzt. */ export function run(cmd, args, { capture = false, allowFail = false, extraEnv = {} } = {}) { const res = spawnSync(cmd, args, { cwd: ROOT_DIR, stdio: capture ? ["ignore", "pipe", "pipe"] : "inherit", encoding: "utf8", env: { ...env, ...extraEnv }, // npx/npm sind unter Windows .cmd-Dateien und brauchen eine Shell shell: isWindows && (cmd === "npx" || cmd === "npm"), }); if (res.error) { if (allowFail) return res; throw new Error(`'${cmd}' konnte nicht gestartet werden: ${res.error.message}`); } if (res.status !== 0 && !allowFail) { if (capture) process.stderr.write(res.stderr ?? ""); throw new Error(`'${cmd} ${args.join(" ")}' endete mit Exit-Code ${res.status}`); } return res; } export function renovateCli(args, extraEnv = {}) { return run("npx", ["--yes", `renovate@${RENOVATE_VERSION}`, ...args], { extraEnv }); } /** Podman bevorzugen, sonst Docker (CONTAINER_ENGINE=docker erzwingt Docker). */ export function containerEngine() { const candidates = env.CONTAINER_ENGINE ? [env.CONTAINER_ENGINE] : ["podman", "docker"]; for (const c of candidates) { if (!run(c, ["--version"], { capture: true, allowFail: true }).error) return c; } throw new Error("Weder podman noch docker gefunden – wird für Forgejo benötigt."); } /** Startet ein Skript-main() mit sauberer Fehlerausgabe. */ export function main(fn) { fn().catch((err) => { console.error(`\x1b[1;31mFehler:\x1b[0m ${err.message}`); // exitCode statt process.exit(): verhindert unter Windows einen libuv-Absturz bei offenen fetch-Handles process.exitCode = 1; }); }