Execa Async Child Process With Clean Output

Run child processes with execa to get parsed stdout and stderr, shell-free argument arrays, and non-zero exit codes surfaced as typed errors.

JavascriptNodeCli Tooling

Code

snippet
import { execa } from 'execa';

async function runChecks() {
  const result = await execa('git', ['diff', '--stat'], {
    cwd: process.cwd(),
    reject: false,
  });

  if (result.failed) {
    console.error('command failed with code', result.exitCode);
    console.error(result.stderr.trim());
    return false;
  }
  console.log(result.stdout.trim());
  return true;
}

const timeout = await execa('node', ['--version'], { timeout: 5000 });
console.log('node version:', timeout.stdout.trim());