|
1 | | -import { sync as spawnSync } from "cross-spawn" |
2 | | -import { SpawnOptions } from "child_process" |
3 | | - |
4 | | -export interface SpawnSafeOptions extends SpawnOptions { |
5 | | - throwOnError?: boolean |
6 | | - logStdErrOnError?: boolean |
7 | | -} |
8 | | - |
9 | | -const defaultOptions: SpawnSafeOptions = { |
10 | | - logStdErrOnError: true, |
11 | | - throwOnError: true, |
12 | | -} |
13 | | - |
14 | | -export const spawnSafeSync = ( |
15 | | - command: string, |
16 | | - args?: string[], |
17 | | - options?: SpawnSafeOptions, |
18 | | -) => { |
19 | | - const mergedOptions = Object.assign({}, defaultOptions, options) |
20 | | - const result = spawnSync(command, args, options) |
21 | | - if (result.error || result.status !== 0) { |
22 | | - if (mergedOptions.logStdErrOnError) { |
23 | | - if (result.stderr) { |
24 | | - console.error(result.stderr.toString()) |
25 | | - } else if (result.error) { |
26 | | - console.error(result.error) |
27 | | - } |
28 | | - } |
29 | | - if (mergedOptions.throwOnError) { |
30 | | - throw result |
31 | | - } |
32 | | - } |
33 | | - return result |
34 | | -} |
| 1 | +import { sync as spawnSync } from "cross-spawn" |
| 2 | +import { SpawnOptions } from "child_process" |
| 3 | + |
| 4 | +export interface SpawnSafeOptions extends SpawnOptions { |
| 5 | + throwOnError?: boolean |
| 6 | + logStdErrOnError?: boolean |
| 7 | +} |
| 8 | + |
| 9 | +const defaultOptions: SpawnSafeOptions = { |
| 10 | + logStdErrOnError: true, |
| 11 | + throwOnError: true, |
| 12 | +} |
| 13 | + |
| 14 | +export const spawnSafeSync = ( |
| 15 | + command: string, |
| 16 | + args?: string[], |
| 17 | + options?: SpawnSafeOptions, |
| 18 | +) => { |
| 19 | + const mergedOptions = Object.assign({}, defaultOptions, options) |
| 20 | + const result = spawnSync(command, args, options) |
| 21 | + if (result.error || result.status !== 0) { |
| 22 | + if (mergedOptions.logStdErrOnError) { |
| 23 | + if (result.stderr) { |
| 24 | + console.error(result.stderr.toString()) |
| 25 | + } else if (result.error) { |
| 26 | + console.error(result.error) |
| 27 | + } |
| 28 | + } |
| 29 | + if (mergedOptions.throwOnError) { |
| 30 | + throw result |
| 31 | + } |
| 32 | + } |
| 33 | + return result |
| 34 | +} |
0 commit comments