1
+ /* eslint-disable prefer-promise-reject-errors */
1
2
const Docker = require ( 'dockerode' ) ;
2
3
const RunBaseCommand = require ( './run.base' ) ;
3
4
const path = require ( 'path' ) ;
@@ -8,6 +9,8 @@ const { log } = require('../../../../logic').api;
8
9
const chalk = require ( 'chalk' ) ;
9
10
10
11
const regex = / # # [ 0 - 9 a - f ] { 24 } # # / i;
12
+ const EngineErrorPrefix = 'EngineError' ;
13
+
11
14
12
15
function _customizer ( objValue , srcValue ) {
13
16
if ( Array . isArray ( objValue ) ) {
@@ -39,16 +42,16 @@ class RunLocalCommand extends RunBaseCommand {
39
42
return new Promise ( ( resolve , reject ) => {
40
43
this . docker . pull ( DEFAULTS . ENGINE_IMAGE , ( err , stream ) => {
41
44
if ( err ) {
42
- console . log ( chalk . red ( `Failed to update codefresh engine: \n ${ err } ` ) ) ;
43
45
reject ( err ) ;
44
46
}
45
47
if ( stream ) {
46
48
function onFinished ( error ) {
47
49
if ( error ) {
48
50
reject ( error ) ;
51
+ return ;
49
52
}
50
53
console . log ( '\nFinished Update ==>\n' ) ;
51
- resolve ( undefined ) ;
54
+ resolve ( ) ;
52
55
}
53
56
54
57
function onProgress ( res ) {
@@ -105,22 +108,8 @@ class RunLocalCommand extends RunBaseCommand {
105
108
const currentContext = authManager . getCurrentContext ( ) ;
106
109
console . log ( `Running pipeline: ${ pipelineName } ` ) ;
107
110
108
- process . stdout . on ( 'data' , ( chunk ) => {
109
- const line = chunk . toString ( ) ;
110
- const include = line . match ( regex ) ;
111
- if ( include ) {
112
- const workflowId = include [ 0 ] . substring ( 2 , include [ 0 ] . length - 2 ) ;
113
- log . showWorkflowLogs ( workflowId , true )
114
- . then ( ( ) => Promise . resolve ( ) . then ( console . log ( `Finished running successfully ${ workflowId } ` ) ) ) ;
115
- }
116
- } ) ;
117
- process . stderr . on ( 'data' , ( chunk ) => {
118
- const line = chunk . toString ( ) ;
119
- console . error ( `Error occurred while running pipeline with error : ${ chalk . red ( line ) } ` ) ;
120
- this . status = 'Error' ;
121
- } ) ;
122
111
return new Promise ( ( resolve , reject ) => {
123
- this . docker . run ( DEFAULTS . ENGINE_IMAGE , [ ] , undefined , _ . mergeWith ( {
112
+ const eventEmitter = this . docker . run ( DEFAULTS . ENGINE_IMAGE , [ ] , undefined , _ . mergeWith ( {
124
113
Env : [
125
114
`ACCESS_TOKEN=${ currentContext . token } ` ,
126
115
`PIPELINE_ID=${ pipelineName } ` , `BRANCH=${ branch } ` ,
@@ -134,20 +123,52 @@ class RunLocalCommand extends RunBaseCommand {
134
123
} ,
135
124
} , injectedOpts , _customizer ) , ( err , data ) => {
136
125
if ( err ) {
137
- console . log ( chalk . red ( `Errored when running pipeline : ${ err } ` ) ) ;
126
+ console . log ( chalk . red ( `Error when running pipeline : ${ err } ` ) ) ;
138
127
// eslint-disable-next-line prefer-promise-reject-errors
139
- reject ( 1 ) ;
128
+ resolve ( 1 ) ;
140
129
}
141
- } ) . on ( 'stream' , ( stream ) => {
130
+ } ) ;
131
+ if ( this . argv . detach ) {
132
+ resolve ( 0 ) ;
133
+ return ;
134
+ }
135
+ eventEmitter . on ( 'stream' , ( stream ) => {
142
136
stream . on ( 'data' , ( chunk ) => {
143
137
const line = chunk . toString ( ) ;
138
+ if ( line . indexOf ( `error: ${ EngineErrorPrefix } ` ) !== - 1 ) {
139
+ const error = JSON . parse ( line . substring (
140
+ line . indexOf ( `error: ${ EngineErrorPrefix } ` ) + `error: ${ EngineErrorPrefix } ` . length ,
141
+ line . lastIndexOf ( EngineErrorPrefix ) ,
142
+ ) ) ;
143
+ if ( error . code || error . message ) {
144
+ console . log ( chalk . red ( `Error when running pipeline code: ${ error . code } message: ${ error . message } ` ) ) ;
145
+ } else {
146
+ console . log ( chalk . red ( `Error when running pipeline: ${ error } ` ) ) ;
147
+ }
148
+
149
+ this . status = 'Error' ;
150
+ resolve ( 1 ) ;
151
+ }
144
152
const include = line . match ( regex ) ;
145
153
if ( include ) {
146
154
const workflowId = include [ 0 ] . substring ( 2 , include [ 0 ] . length - 2 ) ;
147
155
log . showWorkflowLogs ( workflowId , true )
148
156
. then ( ( ) => resolve ( 0 ) ) ;
149
157
}
150
158
} ) ;
159
+ stream . on ( 'error' , ( err ) => {
160
+ if ( err ) {
161
+ console . log ( chalk . red ( `Pipeline finished with error: ${ err } ` ) ) ;
162
+ this . status = 'Error' ;
163
+ }
164
+ // eslint-disable-next-line prefer-promise-reject-errors
165
+ resolve ( 1 ) ;
166
+ } ) ;
167
+ stream . on ( 'end' , ( ) => {
168
+ if ( this . status !== 'Error' ) {
169
+ resolve ( 0 ) ;
170
+ }
171
+ } ) ;
151
172
} ) ;
152
173
} ) ;
153
174
}
0 commit comments