import { execFile } from "node:child_process" import scripts from "../config/backup-script.json" type BackupScript = { key: string label: string downloadFolders?: string[] command: string args?: string[] } function runCommand(command: string, args: string[] = []): Promise { return new Promise((resolve, reject) => { execFile(command, args, { timeout: 10 * 60 * 1000 }, (error, stdout, stderr) => { if (error) { reject(stderr || error.message) return } resolve(stdout || stderr) }) }) } export default defineEventHandler(async (event) => { const body = await readBody<{ key?: string }>(event) const key = typeof body?.key === "string" ? body.key : null if (!key) { throw createError({ statusCode: 400, statusMessage: "Clé de script manquante" }) } const script = (scripts as BackupScript[]).find((item) => item.key === key) if (!script) { throw createError({ statusCode: 404, statusMessage: "Script introuvable" }) } try { const output = await runCommand(script.command, script.args || []) return { ok: true, key: script.key, label: script.label, downloadFolders: script.downloadFolders || [], output: output.trim() } } catch (error) { throw createError({ statusCode: 500, statusMessage: `Erreur execution script: ${String(error)}` }) } })