1
1
import c = require( "ansi-colors" ) ;
2
- import * as dotProp from "dot-prop" ;
3
- import fs = require( "fs" ) ;
4
- import yaml = require( "js-yaml" ) ;
5
2
import * as winston from "winston" ;
6
3
import yargs = require( "yargs" ) ;
7
4
import { Job } from "./job" ;
5
+ import { Parser } from "./parser" ;
6
+
7
+ export interface IKeyValue {
8
+ [ key : string ] : string | undefined ;
9
+ }
8
10
9
11
const colorizer = winston . format . colorize ( ) ;
10
12
@@ -19,113 +21,82 @@ const logger: winston.Logger = winston.createLogger({
19
21
] ,
20
22
} ) ;
21
23
22
- const illigalJobName = [ "include" , "local_configuration" , "image" , "services" , "stages" , "pages" , "types" , "before_script" , "default" , "after_script" , "variables" , "cache" , "include" ] ;
23
24
const argv = yargs . argv ;
24
25
const cwd = argv . cwd || process . cwd ( ) ;
25
26
26
- // Parse .gitlab-ci.yml
27
- const gitlabCiYmlPath = `${ cwd } /.gitlab-ci.yml` ;
28
- if ( ! fs . existsSync ( gitlabCiYmlPath ) ) {
29
- logger . error ( `Could not find ${ gitlabCiYmlPath } ` ) ;
30
- process . exit ( 1 ) ;
31
- }
32
- const gitlabCiContent = fs . readFileSync ( gitlabCiYmlPath , "utf8" ) ;
33
- const gitlabCiData = yaml . safeLoad ( gitlabCiContent ) ;
34
-
35
- // Parse .gitlab-local.yml
36
- const gitlabCiLocalYmlPath = `${ cwd } /.gitlab-ci.local.yml` ;
37
- if ( ! fs . existsSync ( gitlabCiLocalYmlPath ) ) {
38
- logger . error ( `Could not find ${ gitlabCiLocalYmlPath } ` ) ;
39
- process . exit ( 1 ) ;
40
- }
41
- const gitlabCiLocalContent = fs . readFileSync ( gitlabCiLocalYmlPath , "utf8" ) ;
42
- const gitlabLocalData = yaml . safeLoad ( gitlabCiLocalContent ) ;
43
-
44
- const jobs = new Map < string , Job > ( ) ;
45
- const stages = new Map < string , Job [ ] > ( ) ;
46
-
47
- export interface IKeyValue {
48
- [ key : string ] : string | undefined ;
49
- }
50
-
51
- const globalVariables = dotProp . get < IKeyValue > ( gitlabCiData , "variables" ) || { } ;
52
- const globalLocalVariables = dotProp . get < IKeyValue > ( gitlabLocalData , "variables" ) || { } ;
53
-
54
- for ( const value of gitlabCiData . stages ) {
55
- stages . set ( value , [ ] ) ;
56
- }
57
-
58
- const addToMaps = ( key : string , job : Job ) => {
59
- const stage = stages . get ( job . stage ) ;
60
- if ( stage ) {
61
- stage . push ( job ) ;
62
- } else {
63
- const stagesJoin = Array . from ( stages . keys ( ) ) . join ( ", " ) ;
64
- console . error ( `${ c . blueBright ( `${ job . name } ` ) } ${ c . yellow ( `${ job . stage } ` ) } ${ c . red ( `isn't specified in stages. Must be one of the following` ) } ${ c . yellow ( `${ stagesJoin } ` ) } ` ) ;
65
- process . exit ( 1 ) ;
66
- }
67
-
68
- jobs . set ( key , job ) ;
69
- } ;
70
-
71
- for ( const [ key , value ] of Object . entries ( gitlabCiData ) ) {
72
- if ( illigalJobName . includes ( key ) || key [ 0 ] === "." ) {
73
- continue ;
74
- }
75
-
76
- const job = new Job ( value , key , cwd , { ...globalVariables , ...globalLocalVariables } ) ;
77
- addToMaps ( key , job ) ;
78
- }
79
-
80
- for ( const [ key , value ] of Object . entries ( gitlabLocalData || { } ) ) {
81
- if ( illigalJobName . includes ( key ) ) {
82
- continue ;
83
- }
84
-
85
- let job = jobs . get ( key ) ;
86
- if ( job ) {
87
- job . override ( value ) ;
88
- } else {
89
- job = new Job ( value , key , cwd , { ...globalVariables , ...globalLocalVariables } ) ;
90
- addToMaps ( key , job ) ;
91
- }
92
- }
93
-
94
- const runJobs = async ( ) => {
95
-
96
- for ( const [ stageName , jobList ] of stages ) {
97
- const promises : Array < Promise < any > > = [ ] ;
98
-
99
- if ( jobList . length === 0 ) {
100
- console . log ( `=> ${ c . yellow ( `${ stageName } ` ) } has no jobs` ) ;
101
- console . log ( "" ) ;
102
- continue ;
103
- }
104
-
105
- if ( jobList . length === 0 ) {
106
- continue ;
107
- }
108
-
109
- const jobNames = `${ jobList . join ( " " ) } ` ;
110
- console . log ( `=> ${ c . yellow ( `${ stageName } ` ) } > ${ c . blueBright ( `${ jobNames } ` ) } ${ c . magentaBright ( `starting` ) } ...` ) ;
111
- for ( const job of jobList ) {
112
- const jobPromise = job . start ( ) ;
113
- promises . push ( jobPromise ) ;
114
- }
115
-
116
- try {
117
- await Promise . all ( promises ) ;
118
- console . log ( "" ) ;
119
- } catch ( e ) {
120
- if ( e !== "" ) { console . error ( e ) ; }
121
- process . exit ( 1 ) ;
122
- }
123
- }
124
- } ;
125
-
126
- process . on ( "uncaughtException" , ( err ) => {
127
- // handle the error safely
128
- console . log ( err ) ;
129
- } ) ;
130
-
131
- runJobs ( ) . catch ( ) ;
27
+ const parser = new Parser ( cwd , logger ) ;
28
+ //
29
+ // console.log(gitlabData);
30
+ process . exit ( 0 ) ;
31
+
32
+ // const jobs = new Map<string, Job>();
33
+ // const stages = new Map<string, Job[]>();
34
+ //
35
+ // export interface IKeyValue {
36
+ // [key: string]: string | undefined;
37
+ // }
38
+ //
39
+ // for (const value of gitlabCiData.stages) {
40
+ // stages.set(value, []);
41
+ // }
42
+ //
43
+ // const addToMaps = (key: string, job: Job) => {
44
+ // const stage = stages.get(job.stage);
45
+ // if (stage) {
46
+ // stage.push(job);
47
+ // } else {
48
+ // const stagesJoin = Array.from(stages.keys()).join(", ");
49
+ // console.error(`${c.blueBright(`${job.name}`)} ${c.yellow(`${job.stage}`)} ${c.red(`isn't specified in stages. Must be one of the following`)} ${c.yellow(`${stagesJoin}`)}`);
50
+ // process.exit(1);
51
+ // }
52
+ //
53
+ // jobs.set(key, job);
54
+ // };
55
+ //
56
+ // for (const [key, value] of Object.entries(gitlabCiData)) {
57
+ // if (illigalJobName.includes(key) || key[0] === ".") {
58
+ // continue;
59
+ // }
60
+ //
61
+ // const job = new Job(value, key, cwd, {...globalVariables, ...globalLocalVariables});
62
+ // addToMaps(key, job);
63
+ // }
64
+ //
65
+ // const runJobs = async () => {
66
+ //
67
+ // for (const [stageName, jobList] of stages) {
68
+ // const promises: Array<Promise<any>> = [];
69
+ //
70
+ // if (jobList.length === 0) {
71
+ // console.log(`=> ${c.yellow(`${stageName}`)} has no jobs`);
72
+ // console.log("");
73
+ // continue;
74
+ // }
75
+ //
76
+ // if (jobList.length === 0) {
77
+ // continue;
78
+ // }
79
+ //
80
+ // const jobNames = `${jobList.join(" ")}`;
81
+ // console.log(`=> ${c.yellow(`${stageName}`)} > ${c.blueBright(`${jobNames}`)} ${c.magentaBright(`starting`)}...`);
82
+ // for (const job of jobList) {
83
+ // const jobPromise = job.start();
84
+ // promises.push(jobPromise);
85
+ // }
86
+ //
87
+ // try {
88
+ // await Promise.all(promises);
89
+ // console.log("");
90
+ // } catch (e) {
91
+ // if (e !== "") { console.error(e); }
92
+ // process.exit(1);
93
+ // }
94
+ // }
95
+ // };
96
+ //
97
+ // process.on("uncaughtException", (err) => {
98
+ // // handle the error safely
99
+ // console.log(err);
100
+ // });
101
+ //
102
+ // runJobs().catch();
0 commit comments