import { access, readFile, writeFile } from "node:fs/promises"; const candidates = [ "node_modules/@indiekit/indiekit/config/express.js", ]; const marker = "SESSION_MAX_AGE_DAYS"; const from = "maxAge: 7 * 24 * 60 * 60 * 1000, // 7 days,"; const to = "maxAge: Number.parseInt(process.env.SESSION_MAX_AGE_DAYS || '30', 10) * 24 * 60 * 60 * 1000, // SESSION_MAX_AGE_DAYS days"; async function exists(filePath) { try { await access(filePath); return true; } catch { return false; } } let checked = 0; let patched = 0; for (const filePath of candidates) { if (!(await exists(filePath))) continue; checked += 1; const source = await readFile(filePath, "utf8"); if (source.includes(marker)) { console.log("[postinstall] Session maxAge patch already applied"); continue; } if (!source.includes(from)) { console.warn("[postinstall] Skipping session maxAge patch: upstream format changed"); continue; } await writeFile(filePath, source.replace(from, to), "utf8"); patched += 1; } if (checked === 0) { console.log("[postinstall] No indiekit express config found"); } else if (patched > 0) { console.log("[postinstall] Patched session maxAge to use SESSION_MAX_AGE_DAYS env var (default 30)"); }