import { execSync } from 'node:child_process'

export function run(command, exit = true) {
  try {
    return execSync(command, { stdio: 'pipe', encoding: 'utf8' })
  } catch (e) {
    console.error(`Command ${command} failed with status ${e.status}`)

    if (e.stdout) {
      console.error(`\nStandard output:\n${e.stdout}`)
    }

    if (e.stderr) {
      console.error(`\nStandard error:\n${e.stderr}`)
    }

    if (exit) {
      process.exit(2)
    }
  }
}
