@@ -2,7 +2,6 @@ import * as c from "ansi-colors";
2
2
import * as deepExtend from "deep-extend" ;
3
3
import * as fs from "fs-extra" ;
4
4
import * as glob from "glob" ;
5
- import * as path from "path" ;
6
5
import * as prettyHrtime from "pretty-hrtime" ;
7
6
import * as shelljs from "shelljs" ;
8
7
@@ -18,16 +17,19 @@ if (process.env.EXEPATH) {
18
17
19
18
export class Job {
20
19
public readonly name : string ;
20
+ public readonly needs : string [ ] ;
21
21
public readonly stage : string ;
22
22
23
23
private readonly afterScripts : string [ ] = [ ] ;
24
24
private readonly allowFailure : boolean ;
25
25
private readonly beforeScripts : string [ ] = [ ] ;
26
26
private readonly cwd : any ;
27
+ private finished = false ;
27
28
private readonly globals : any ;
28
29
private readonly maxJobNameLength : number ;
29
30
private running = false ;
30
31
private readonly scripts : string [ ] = [ ] ;
32
+ private success = true ;
31
33
private readonly variables : { [ key : string ] : string } ;
32
34
private readonly when : string ;
33
35
@@ -57,20 +59,36 @@ export class Job {
57
59
this . stage = jobData . stage || ".pre" ;
58
60
this . scripts = [ ] . concat ( jobData . script || [ ] ) ;
59
61
62
+ const jobNameStr = this . getJobNameString ( ) ;
63
+
64
+ if ( this . scripts . length === 0 ) {
65
+ console . error ( `${ jobNameStr } ${ c . red ( "must have script specified" ) } ` ) ;
66
+ process . exit ( 1 ) ;
67
+ }
68
+
60
69
const ciDefault = globals . default || { } ;
61
70
this . when = jobData . when || "on_success" ;
62
71
this . beforeScripts = [ ] . concat ( jobData . before_script || ciDefault . before_script || globals . before_script || [ ] ) ;
63
72
this . afterScripts = [ ] . concat ( jobData . after_script || ciDefault . after_script || globals . after_script || [ ] ) ;
64
73
this . allowFailure = jobData . allow_failure || false ;
65
74
this . variables = jobData . variables || { } ;
75
+ if ( this . needs && this . needs . length === 0 ) {
76
+ console . error ( `${ jobNameStr } ${ c . red ( "'needs' cannot be empty array" ) } ` ) ;
77
+ process . exit ( 1 ) ;
78
+ }
79
+ this . needs = jobData . needs || [ ] ;
66
80
}
67
81
68
82
public getJobNameString ( ) {
69
83
return `${ c . blueBright ( `${ this . name . padEnd ( this . maxJobNameLength + 1 ) } ` ) } ` ;
70
84
}
71
85
72
86
public getOutputFilesPath ( ) {
73
- return `${ this . cwd } /.gitlab-ci-local/output/${ this . getEnvs ( ) . CI_PIPELINE_ID } /${ this . name } .log` ;
87
+ return `${ this . cwd } /.gitlab-ci-local/output/${ this . name } .log` ;
88
+ }
89
+
90
+ public isFinished ( ) {
91
+ return this . finished ;
74
92
}
75
93
76
94
public isManual ( ) : boolean {
@@ -81,38 +99,50 @@ export class Job {
81
99
return this . when === "never" ;
82
100
}
83
101
84
- public async start ( ) : Promise < void > {
85
- const jobNameStr = this . getJobNameString ( ) ;
102
+ public isRunning ( ) {
103
+ return this . running ;
104
+ }
86
105
87
- this . running = true ;
106
+ public isSuccess ( ) {
107
+ return this . success ;
108
+ }
88
109
89
- if ( this . scripts . length === 0 ) {
90
- console . error ( `${ jobNameStr } ${ c . red ( "must have script specified" ) } ` ) ;
91
- process . exit ( 1 ) ;
92
- }
110
+ public setFinished ( finished : boolean ) {
111
+ this . finished = finished ;
112
+ }
113
+
114
+ public async start ( ) : Promise < void > {
115
+ fs . ensureFileSync ( this . getOutputFilesPath ( ) ) ;
116
+ fs . truncateSync ( this . getOutputFilesPath ( ) ) ;
117
+ console . log ( this . getStartingString ( ) ) ;
118
+ this . running = true ;
93
119
94
120
const startTime = process . hrtime ( ) ;
95
121
const prescripts = this . beforeScripts . concat ( this . scripts ) ;
96
122
const prescriptsExitCode = await this . exec ( prescripts . join ( " && " ) ) ;
97
-
98
123
if ( this . afterScripts . length === 0 && prescriptsExitCode > 0 && ! this . allowFailure ) {
99
- throw this . getExitedString ( prescriptsExitCode , false ) ;
124
+ console . error ( this . getExitedString ( startTime , prescriptsExitCode , false ) ) ;
125
+ this . running = false ;
126
+ this . finished = true ;
127
+ this . success = false ;
128
+
129
+ return ;
100
130
}
101
131
102
132
if ( this . afterScripts . length === 0 && prescriptsExitCode > 0 && this . allowFailure ) {
103
- console . error ( this . getExitedString ( prescriptsExitCode , true ) ) ;
104
- console . log ( this . getFinishedString ( startTime ) ) ;
133
+ console . error ( this . getExitedString ( startTime , prescriptsExitCode , true ) ) ;
105
134
this . running = false ;
135
+ this . finished = true ;
106
136
107
137
return ;
108
138
}
109
139
110
140
if ( prescriptsExitCode > 0 && this . allowFailure ) {
111
- console . error ( this . getExitedString ( prescriptsExitCode , true ) ) ;
141
+ console . error ( this . getExitedString ( startTime , prescriptsExitCode , true ) ) ;
112
142
}
113
143
114
144
if ( prescriptsExitCode > 0 && ! this . allowFailure ) {
115
- console . error ( this . getExitedString ( prescriptsExitCode , false ) ) ;
145
+ console . error ( this . getExitedString ( startTime , prescriptsExitCode , false ) ) ;
116
146
}
117
147
118
148
let afterScriptsCode = 0 ;
@@ -121,15 +151,17 @@ export class Job {
121
151
}
122
152
123
153
if ( afterScriptsCode > 0 ) {
124
- console . error ( this . getExitedString ( prescriptsExitCode , true , " (after_script)" ) ) ;
154
+ console . error ( this . getExitedString ( startTime , prescriptsExitCode , true , " (after_script)" ) ) ;
125
155
}
126
156
127
157
if ( prescriptsExitCode > 0 && ! this . allowFailure ) {
128
- throw "" ;
158
+ this . success = false ;
129
159
}
130
160
131
161
console . log ( this . getFinishedString ( startTime ) ) ;
162
+
132
163
this . running = false ;
164
+ this . finished = true ;
133
165
134
166
return ;
135
167
}
@@ -139,9 +171,6 @@ export class Job {
139
171
}
140
172
141
173
private async exec ( script : string ) : Promise < number > {
142
- fs . ensureFileSync ( this . getOutputFilesPath ( ) ) ;
143
- fs . truncateSync ( this . getOutputFilesPath ( ) ) ;
144
-
145
174
return new Promise < any > ( ( resolve , reject ) => {
146
175
const jobNameStr = this . getJobNameString ( ) ;
147
176
const outputFilesPath = this . getOutputFilesPath ( ) ;
@@ -176,14 +205,13 @@ export class Job {
176
205
return { ...this . globals . variables || { } , ...this . variables , ...process . env } ;
177
206
}
178
207
179
- private getExitedString ( code : number , warning : boolean = false , prependString : string = "" ) {
180
- const seeLogStr = `See ${ path . resolve ( this . getOutputFilesPath ( ) ) } ` ;
181
- const jobNameStr = this . getJobNameString ( ) ;
208
+ private getExitedString ( startTime : [ number , number ] , code : number , warning : boolean = false , prependString : string = "" ) {
209
+ const finishedStr = this . getFinishedString ( startTime ) ;
182
210
if ( warning ) {
183
- return `${ jobNameStr } ${ c . yellowBright ( `warning with code ${ code } ` ) } ${ prependString } ${ seeLogStr } ` ;
211
+ return `${ finishedStr } ${ c . yellowBright ( `warning with code ${ code } ` ) } ${ prependString } ` ;
184
212
}
185
213
186
- return `${ jobNameStr } ${ c . red ( `exited with code ${ code } ` ) } ${ prependString } ${ seeLogStr } ` ;
214
+ return `${ finishedStr } ${ c . red ( `exited with code ${ code } ` ) } ${ prependString } ` ;
187
215
}
188
216
189
217
private getFinishedString ( startTime : [ number , number ] ) {
@@ -193,4 +221,10 @@ export class Job {
193
221
194
222
return `${ jobNameStr } ${ c . magentaBright ( "finished" ) } in ${ c . magenta ( `${ timeStr } ` ) } ` ;
195
223
}
224
+
225
+ private getStartingString ( ) {
226
+ const jobNameStr = this . getJobNameString ( ) ;
227
+
228
+ return `${ jobNameStr } ${ c . magentaBright ( "starting" ) } ...` ;
229
+ }
196
230
}
0 commit comments