@@ -5,16 +5,23 @@ const glob = require('glob');
5
5
const yaml = require ( 'js-yaml' ) ;
6
6
const fs = require ( 'fs' ) ;
7
7
const { log} = require ( 'console' ) ;
8
+
8
9
const ERROR = 1 ;
9
10
const isCLI = require . main === module ;
11
+ const colors = {
12
+ red : '\x1b[31m' ,
13
+ cyan : '\x1b[36m' ,
14
+ green : '\x1b[32m' ,
15
+ yellow : '\x1b[33m'
16
+ } ;
10
17
11
18
// @todo only load new/changed yaml - use git diff
12
19
function getYamlFilePaths ( ) {
13
20
return glob . sync ( '{**/*.yaml,**/*.yml}' ) ;
14
21
}
15
22
16
23
function failure ( error = 'Error' ) {
17
- log ( `${ error } ` ) ;
24
+ log ( colors . red , `${ error } ` ) ;
18
25
process . exitCode = ERROR ;
19
26
return ERROR ;
20
27
}
@@ -24,23 +31,25 @@ function loadYamlFile(path) {
24
31
let data = yaml . safeLoad ( fs . readFileSync ( path , 'utf8' ) ) ;
25
32
return { data, path} ;
26
33
} catch ( error ) {
27
- return failure ( `> Invalid yaml file: ${ path } \n` ) ;
34
+ return failure ( `Invalid yaml file: ${ path } \n` ) ;
28
35
}
29
36
}
30
37
31
38
function logYamlDoc ( { data, path} ) {
32
- log ( path ) ;
33
- log ( data ) ;
39
+ log ( colors . cyan , path ) ;
40
+ yaml . safeDump ( data ) . split ( '\n' ) . forEach ( line => log ( colors . green , line ) ) ;
34
41
}
35
42
36
43
function run ( ) {
37
44
let paths = getYamlFilePaths ( ) ;
38
45
if ( ! paths . length ) {
39
- return failure ( '> 0 yaml files' ) ;
46
+ return failure ( '0 yaml files' ) ;
40
47
}
41
48
42
49
let docs = paths . map ( loadYamlFile ) . filter ( x => x !== ERROR ) ;
43
- log ( '> Valid yaml file(s):\n' ) ;
50
+ if ( docs . length ) {
51
+ log ( colors . yellow , 'Valid yaml file(s):' ) ;
52
+ }
44
53
docs . forEach ( logYamlDoc ) ;
45
54
}
46
55
0 commit comments