56 lines
1.4 KiB
TypeScript
56 lines
1.4 KiB
TypeScript
import { execFile } from "child_process"
|
|
import diskSources from "../config/disk-commands.json"
|
|
|
|
type DiskSource = {
|
|
key: string
|
|
label: string
|
|
command: string
|
|
args?: string[]
|
|
}
|
|
|
|
function getCommand(source: DiskSource) {
|
|
const envKey = `DISK_COMMAND_${source.key.toUpperCase()}`
|
|
const legacyEnvKey =
|
|
source.key === "remote" ? "DISK_REMOTE_COMMAND" : source.key === "local" ? "DISK_LOCAL_COMMAND" : ""
|
|
|
|
return process.env[envKey] || (legacyEnvKey ? process.env[legacyEnvKey] : undefined) || source.command
|
|
}
|
|
|
|
function runCommand(command: string, args: string[] = []): Promise<string> {
|
|
return new Promise((resolve, reject) => {
|
|
execFile(command, args, (error, stdout, stderr) => {
|
|
if (error) {
|
|
reject(stderr || error.message)
|
|
return
|
|
}
|
|
resolve(stdout)
|
|
})
|
|
})
|
|
}
|
|
|
|
export default defineEventHandler(async () => {
|
|
const results = await Promise.all(
|
|
(diskSources as DiskSource[]).map(async (source) => {
|
|
try {
|
|
const output = await runCommand(source.command, source.args || [])
|
|
return {
|
|
key: source.key,
|
|
label: source.label,
|
|
ok: true,
|
|
output
|
|
}
|
|
} catch (error) {
|
|
console.error(`Erreur disk source ${source.key}:`, error)
|
|
return {
|
|
key: source.key,
|
|
label: source.label,
|
|
ok: false,
|
|
output: "Erreur lors de l'opération"
|
|
}
|
|
}
|
|
})
|
|
)
|
|
|
|
return { results }
|
|
})
|