@@ -2,30 +2,6 @@ const chalk = require('chalk');
2
2
const execa = require ( 'execa' ) ;
3
3
const notifier = require ( 'node-notifier' ) ;
4
4
5
- function handleShResults ( results , exitOnError , streamOutput , showCmdOnError , exitImmediately , resolve , reject ) {
6
- const { code, stdout, stderr, message } = results ;
7
- const output = stderr + '\n\n' + stdout ;
8
- if ( code > 0 ) {
9
- const errorMsg = chalk . red ( showCmdOnError ? message : `Error with code ${ code } ` ) ;
10
- if ( exitOnError ) {
11
- if ( exitImmediately ) {
12
- console . error ( errorMsg + output ) ;
13
- process . exit ( 1 ) ;
14
- }
15
- process . exitCode = 1 ;
16
- reject ( new Error ( errorMsg + output ) ) ;
17
- } else {
18
- notifier . notify ( {
19
- title : cmd ,
20
- message : output ,
21
- sound : true ,
22
- } ) ;
23
- reject ( errorMsg + output ) ;
24
- }
25
- }
26
- resolve ( output ) ;
27
- }
28
-
29
5
/**
30
6
* Run shell command
31
7
* @param cmd {string} - Command to run
@@ -38,16 +14,52 @@ function handleShResults(results, exitOnError, streamOutput, showCmdOnError, exi
38
14
async function sh ( cmd , args , exitOnError , streamOutput , showCmdOnError = true , exitImmediately = false ) {
39
15
return new Promise ( ( resolve , reject ) => {
40
16
const child = execa ( cmd , args ) ;
17
+ let output = '' ;
41
18
42
19
if ( streamOutput ) {
43
20
child . stdout . pipe ( process . stdout ) ;
44
21
child . stderr . pipe ( process . stderr ) ;
45
22
}
46
23
47
- child . then ( ( results ) => handleShResults ( results , exitOnError , streamOutput , showCmdOnError , exitImmediately , resolve , reject ) ) ;
48
- child . catch ( ( results ) => handleShResults ( results , exitOnError , streamOutput , showCmdOnError , exitImmediately , resolve , reject ) ) ;
49
- } ) ;
24
+ child . stdout . on ( 'data' , ( data ) => {
25
+ output += data ;
26
+ if ( streamOutput ) {
27
+ process . stdout . write ( data ) ;
28
+ }
29
+ } ) ;
50
30
31
+ child . stderr . on ( 'data' , ( data ) => {
32
+ output += data ;
33
+ if ( streamOutput ) {
34
+ process . stdout . write ( data ) ;
35
+ }
36
+ } ) ;
37
+
38
+ child . on ( 'close' , ( code ) => {
39
+ if ( code > 0 ) {
40
+ const errorMsg = chalk . red ( `
41
+ Error with code ${ code } ${ showCmdOnError ? ` after running: ${ cmd } ` : '' } :
42
+ ` ) ;
43
+ if ( exitOnError ) {
44
+ if ( exitImmediately ) {
45
+ console . error ( errorMsg + output ) ;
46
+ process . exit ( 1 ) ;
47
+ }
48
+
49
+ process . exitCode = 1 ;
50
+ reject ( new Error ( errorMsg + output ) ) ;
51
+ } else {
52
+ notifier . notify ( {
53
+ title : cmd ,
54
+ message : output ,
55
+ sound : true ,
56
+ } ) ;
57
+ reject ( errorMsg + output ) ;
58
+ }
59
+ }
60
+ resolve ( output ) ;
61
+ } ) ;
62
+ } ) ;
51
63
}
52
64
53
65
module . exports = sh ;
0 commit comments