@@ -12,7 +12,6 @@ async function lint(rawInput, flags) {
12
12
const fromStdin = checkFromStdin ( rawInput , flags ) ;
13
13
14
14
const range = pick ( flags , 'edit' , 'from' , 'to' ) ;
15
- const fmt = new chalk . constructor ( { enabled : flags . color } ) ;
16
15
17
16
const input = await ( fromStdin
18
17
? stdin ( )
@@ -23,39 +22,74 @@ async function lint(rawInput, flags) {
23
22
. filter ( Boolean ) ;
24
23
25
24
if ( messages . length === 0 && ! checkFromRepository ( flags ) ) {
26
- const err = new Error (
27
- '[input] is required: supply via stdin, or --edit or --from and --to'
25
+ throw error (
26
+ '[input] is required: supply via stdin, or --edit or --from and --to' ,
27
+ {
28
+ quiet : flags . quiet ,
29
+ help : true ,
30
+ type : pkg . name
31
+ }
28
32
) ;
29
- err . quiet = flags . quiet ;
30
- err . help = true ;
31
- err . type = pkg . name ;
32
- throw err ;
33
33
}
34
34
35
- return Promise . all (
36
- messages . map ( async message => {
37
- const loaded = await core . load ( getSeed ( flags ) , { cwd : flags . cwd } ) ;
38
- const parserOpts = selectParserOpts ( loaded . parserPreset ) ;
39
- const opts = parserOpts ? { parserOpts} : undefined ;
40
- const report = await core . lint ( message , loaded . rules , opts ) ;
41
- const formatted = core . format ( report , { color : flags . color } ) ;
35
+ const loaded = await core . load ( getSeed ( flags ) , { cwd : flags . cwd } ) ;
36
+ const parserOpts = selectParserOpts ( loaded . parserPreset ) ;
37
+ const opts = parserOpts ? { parserOpts} : undefined ;
38
+
39
+ const results = await all ( messages , async msg => {
40
+ return {
41
+ report : await core . lint ( msg , loaded . rules , opts ) ,
42
+ input : msg
43
+ } ;
44
+ } ) ;
45
+
46
+ const valid = results . every ( result => result . report . valid ) ;
47
+
48
+ if ( flags . quiet && valid ) {
49
+ return ;
50
+ }
51
+
52
+ if ( flags . quiet && ! valid ) {
53
+ throw error ( 'linting failed' , { type : pkg . name , quiet : true } ) ;
54
+ }
42
55
43
- if ( ! flags . quiet ) {
56
+ switch ( flags . format ) {
57
+ case 'commitlint' : {
58
+ const fmt = new chalk . constructor ( { enabled : flags . color } ) ;
59
+ const icon = fmt . grey ( '⧗' ) ;
60
+ const formatted = results . map ( result => {
61
+ result . formatted = core . format ( result . report , { color : flags . color } ) ;
62
+ return result ;
63
+ } ) ;
64
+ formatted . forEach ( result => {
65
+ const subject = fmt . bold ( result . input . split ( '\n' ) [ 0 ] ) ;
44
66
console . log (
45
- `${ fmt . grey ( '⧗' ) } input: ${ fmt . bold ( message . split ( '\n' ) [ 0 ] ) } `
67
+ `${ icon } input: ${ subject } \n ${ result . formatted . join ( '\n' ) } \n `
46
68
) ;
47
- console . log ( formatted . join ( '\n' ) ) ;
48
- }
69
+ } ) ;
70
+ break ;
71
+ }
72
+ case 'json' :
73
+ console . log ( JSON . stringify ( { valid, results} ) ) ;
74
+ break ;
75
+ default : {
76
+ throw error ( `unknown format: ${ flags . format } ` ) ;
77
+ }
78
+ }
49
79
50
- if ( report . errors . length > 0 ) {
51
- const error = new Error ( formatted [ formatted . length - 1 ] ) ;
52
- error . quiet = flags . quiet ;
53
- error . type = pkg . name ;
54
- throw error ;
55
- }
56
- console . log ( '' ) ;
57
- } )
58
- ) ;
80
+ if ( ! valid ) {
81
+ throw error ( 'linting failed' , { type : pkg . name , quiet : true } ) ;
82
+ }
83
+ }
84
+
85
+ function all ( things , predecate ) {
86
+ return Promise . all ( things . map ( thing => predecate ( thing ) ) ) ;
87
+ }
88
+
89
+ function error ( message , opts = { } ) {
90
+ const err = new Error ( message ) ;
91
+ Object . assign ( err , opts ) ;
92
+ return err ;
59
93
}
60
94
61
95
function selectParserOpts ( parserPreset ) {
0 commit comments