Skip to content

Commit

Permalink
exit if capture failed
Browse files Browse the repository at this point in the history
  • Loading branch information
kla committed Sep 19, 2024
1 parent aeb0e93 commit 5257c44
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/shell.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
import { execSync, spawn } from 'child_process'
import { spawn, spawnSync } from 'child_process'
import { RunOptions } from '~/types'
import icons from '~/icons'
import chalk from 'chalk'

function outputCommand(command: string, { cwd, silent }: RunOptions): void {
if (silent)
return
if (silent) return

command = chalk.green(`${icons.play} ${command}`)
if (cwd) command += chalk.green(` (in ${cwd})`)
console.log(command)
}

export function capture(command: string, options={}) {
options = { silent: true, encoding: 'utf-8', ...options }
export function capture(command: string, options = {}) {
options = { silent: true, encoding: 'utf8', shell: true, ...options }
outputCommand(command, options)
return execSync(command, options).trim()

const result = spawnSync(command, [], options)
const stdout = result.stdout ? result.stdout.toString().trim() : ''
const stderr = result.stderr ? result.stderr.toString().trim() : ''

if (result.error || result.status !== 0) {
outputCommand(command, { silent: false })
console.error(stdout, '\n', stderr)
process.exit(result.status)
}
return stdout
}

export async function run(command: string, { env, cwd } = {}) {
Expand Down

0 comments on commit 5257c44

Please sign in to comment.