#!/usr/bin/env node import { execSync } from "node:child_process"; import { existsSync } from "node:fs"; import { platform } from "node:os"; import { dirname, resolve } from "node:path"; import { fileURLToPath } from "node:url"; function log(message) { console.log(`[security-auto] ${message}`); } function commandExists(command) { try { execSync(`command -v ${command}`, { stdio: "ignore" }); return true; } catch { return false; } } function run() { if (platform() !== "linux") { log("skip: only runs on Linux"); return; } if (process.env.DISABLE_PERF_GUARD_AUTO_INSTALL === "1") { log("skip: DISABLE_PERF_GUARD_AUTO_INSTALL=1"); return; } if (typeof process.getuid !== "function" || process.getuid() !== 0) { log("skip: requires root user"); return; } if (!commandExists("systemctl")) { log("skip: systemctl not found"); return; } const currentFilePath = fileURLToPath(import.meta.url); const securityDir = dirname(currentFilePath); const installerPath = resolve(securityDir, "install-perf-guard.sh"); if (!existsSync(installerPath)) { log(`skip: installer not found (${installerPath})`); return; } try { log("ensuring perf-guard is installed and enabled"); execSync(`bash "${installerPath}"`, { stdio: "inherit" }); log("perf-guard ready"); } catch (error) { console.warn( `[security-auto] warn: install-perf-guard failed, deployment continues. ${error instanceof Error ? error.message : String(error)}` ); } } run();