#!/usr/bin/env node /** * run-if-exists — шелл-агностичный guard для lefthook: запустить бинарь, ТОЛЬКО если он есть. * Заменяет хрупкий `test -f ./bin/X.exe || exit 0; ./bin/X.exe …` (падал exit-127, когда команда * `test` недоступна в шелле хука). Бинарь отсутствует → чистый пропуск (exit 0). Присутствует → * запуск с пробросом кода выхода. Использование: node tools/run-if-exists.mjs -- . */ import { existsSync } from 'node:fs'; import { execFileSync } from 'node:child_process'; export function shouldRun(binaryPath, fsImpl = { existsSync }) { return !!binaryPath && fsImpl.existsSync(binaryPath); } function main() { const argv = process.argv.slice(2); const sep = argv.indexOf('--'); const binaryPath = argv[0]; const args = sep >= 0 ? argv.slice(sep + 1) : argv.slice(1); if (!shouldRun(binaryPath)) { console.log(`[run-if-exists] ${binaryPath || '(пусто)'} отсутствует — пропуск`); process.exit(0); } try { execFileSync(binaryPath, args, { stdio: 'inherit' }); process.exit(0); } catch (e) { process.exit(typeof e.status === 'number' ? e.status : 1); } } import { fileURLToPath } from 'node:url'; const isCli = process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]; if (isCli) main();