08919752e3
Checkpoint multi-chantiers (arbre vert : tsc 0 erreur, 70 tests, build OK). - tâche 1.9 Phase 1 : schéma socle (machine_state/events/reports/raw_artifacts/ hardware/metrics + colonnes étendues) + wiring refresh/execute. Migration 0002. - tâche 1.9 Phase 2 : machine_credentials + machine_host_keys (non destructif, dual-read + backfill). Migration 0003. Fix séquence journal de migration. - tâche 2 : SJ-0 (types étendus rétro-compatibles, réducteur Docker, resolveTemplate), SJ-1 (update-analyze enrichi), SJ-2 (apply + diff dpkg + timeout inactivité SSH), SJ-3 (reboot vérifié boot_id). - WIP parallèle inclus : /api/capabilities, auth/apiTokens/apiClients, system metrics, scaffold app_rust, ajustements frontend. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
37 lines
1.2 KiB
TypeScript
37 lines
1.2 KiB
TypeScript
// server/templates/render.ts
|
|
import Mustache from "mustache";
|
|
import { readFileSync, existsSync } from "node:fs";
|
|
import { resolve } from "node:path";
|
|
|
|
const TEMPLATES_ROOT = resolve(process.cwd(), "templates");
|
|
|
|
export interface TemplateVars {
|
|
aptProxy?: string | null;
|
|
}
|
|
|
|
export function renderTemplate(relPath: string, vars: TemplateVars): string {
|
|
const tpl = readFileSync(resolve(TEMPLATES_ROOT, relPath), "utf8");
|
|
// Mustache échappe le HTML par défaut; on désactive (ce sont des scripts shell).
|
|
return Mustache.render(tpl, vars, {}, { escape: (s) => s });
|
|
}
|
|
|
|
/** Existence par défaut d'un template relatif à templates/. */
|
|
function defaultExists(rel: string): boolean {
|
|
return existsSync(resolve(TEMPLATES_ROOT, rel));
|
|
}
|
|
|
|
/**
|
|
* Résout le chemin de template le plus spécifique pour (action, OS) :
|
|
* `<osFamily>/<action>.sh.tpl` s'il existe, sinon fallback base `apt/<action>.sh.tpl`.
|
|
* `exists` est injectable pour les tests.
|
|
*/
|
|
export function resolveTemplate(
|
|
action: string,
|
|
osFamily: string,
|
|
exists: (rel: string) => boolean = defaultExists,
|
|
): string {
|
|
const specific = `${osFamily}/${action}.sh.tpl`;
|
|
if (osFamily !== "unknown" && osFamily !== "apt" && exists(specific)) return specific;
|
|
return `apt/${action}.sh.tpl`;
|
|
}
|