1
1
// polyfills
2
2
import 'babel-polyfill' ;
3
3
4
- // core modules
5
- import {
6
- readFile as readFileNodeback
7
- } from 'fs' ;
8
-
9
4
// npm modules
10
5
import chalk from 'chalk' ;
11
- import denodeify from 'denodeify' ;
12
- import gitRawCommits from 'git-raw-commits' ;
13
6
import meow from 'meow' ;
14
- import merge from 'lodash.merge' ;
15
7
import pick from 'lodash.pick' ;
16
8
import stdin from 'get-stdin' ;
17
- import rc from 'rc' ;
18
9
19
10
// local modules
11
+ import help from './help' ;
20
12
import lint from './' ;
21
- import pkg from '../package' ;
13
+ import {
14
+ format ,
15
+ getConfiguration ,
16
+ getPreset ,
17
+ getMessages
18
+ } from './' ;
22
19
23
- // denodeifications
24
- const readFile = denodeify ( readFileNodeback ) ;
20
+ import pkg from '../package' ;
25
21
26
22
/**
27
23
* Behavioural rules
28
24
*/
29
25
const rules = {
30
- fromStdin : ( input , settings ) => input . length === 0 &&
31
- settings . from === null &&
32
- settings . to === null &&
33
- settings . edit === null
26
+ fromStdin : ( input , flags ) => input . length === 0 &&
27
+ flags . from === null &&
28
+ flags . to === null &&
29
+ ! flags . edit
34
30
} ;
35
31
36
- // Init meow 😸cli
37
- const cli = meow ( {
38
- help : [ '' ] ,
39
- description : `${ pkg . name } @${ pkg . version } - ${ pkg . description } `
40
- } , {
32
+ const configuration = {
41
33
// flags of string type
42
34
string : [ 'from' , 'to' , 'preset' ] ,
43
35
// flags of bool type
44
- boolean : [ 'edit' , 'quiet' , 'color' ] ,
36
+ boolean : [ 'edit' , 'help' , 'version' , ' quiet', 'color' ] ,
45
37
// flag aliases
46
38
alias : {
47
39
c : 'color' ,
48
40
e : 'edit' ,
49
41
f : 'from' ,
50
42
p : 'preset' ,
51
43
t : 'to' ,
52
- q : 'quiet'
44
+ q : 'quiet' ,
45
+ h : 'help' ,
46
+ v : 'version'
47
+ } ,
48
+ description : {
49
+ color : 'toggle formatted output' ,
50
+ edit : 'read last commit message found in ./git/COMMIT_EDITMSG' ,
51
+ from : 'lower end of the commit range to lint; applies if edit=false' ,
52
+ preset : 'conventional-changelog-preset to use for commit message parsing' ,
53
+ to : 'upper end of the commit range to lint; applies if edit=false' ,
54
+ quiet : 'toggle console output'
53
55
} ,
54
56
// flag defaults
55
57
default : {
@@ -64,92 +66,13 @@ const cli = meow({
64
66
unknown ( arg ) {
65
67
throw new Error ( `unknown flags: ${ arg } ` ) ;
66
68
}
67
- } ) ;
68
-
69
- // Get commit messages
70
- // TODO: move this to an own moduleddd
71
- function getCommits ( options ) {
72
- return new Promise ( ( resolve , reject ) => {
73
- const data = [ ] ;
74
- gitRawCommits ( options )
75
- . on ( 'data' , chunk => data . push ( chunk . toString ( 'utf-8' ) ) )
76
- . on ( 'error' , reject )
77
- . on ( 'end' , ( ) => {
78
- resolve ( data ) ;
79
- } ) ;
80
- } ) ;
81
- }
82
-
83
- // Get commit messages
84
- // TODO: move this to an own module
85
- async function getMessages ( settings ) {
86
- const { from, to, edit} = settings ;
87
-
88
- if ( edit ) {
89
- const editFile = await readFile ( `.git/COMMIT_EDITMSG` ) ;
90
- return [ editFile . toString ( 'utf-8' ) ] ;
91
- } else {
92
- return await getCommits ( {
93
- from,
94
- to
95
- } ) ;
96
- }
97
- }
98
-
99
- // Resolve extend configs
100
- // TODO: move this to own module
101
- function resolveExtends ( config , prefix = '' , key = 'extends' ) {
102
- return Object . values ( config [ key ] || [ ] )
103
- . reduce ( ( merged , extender ) => {
104
- const name = [ prefix , extender ]
105
- . filter ( String )
106
- . join ( '-' ) ;
107
- return merge (
108
- { } ,
109
- merged ,
110
- resolveExtends ( require ( name ) )
111
- ) ;
112
- } , config ) ;
113
- }
114
-
115
- // Get linting config
116
- // TODO: move this to own module
117
- function getConfiguration ( name , settings ) {
118
- const config = rc ( name , settings . defaults ) ;
119
- return resolveExtends ( config , settings . prefix ) ;
120
- }
121
-
122
- // Get commit messages
123
- // TODO: move this to an own module
124
- function format ( report , options = { } ) {
125
- const { signs, colors, color : enabled } = options ;
126
- const fmt = new chalk . constructor ( { enabled} ) ;
127
-
128
- const problems = [ ...report . errors , ...report . warnings ]
129
- . map ( problem => {
130
- const sign = signs [ problem . level ] ;
131
- const color = colors [ problem . level ] ;
132
- const decoration = fmt [ color ] ( sign ) ;
133
- const name = chalk . grey ( `[${ problem . name } ]` ) ;
134
- return `${ decoration } ${ problem . message } ${ name } `
135
- } ) ;
136
-
137
- const sign = report . errors . length ?
138
- '✖' :
139
- report . warnings . length ?
140
- '⚠' :
141
- '✔' ;
142
-
143
- const color = report . errors . length ?
144
- 'red' :
145
- report . warnings . length ?
146
- 'yellow' :
147
- 'green' ;
69
+ } ;
148
70
149
- const decoration = fmt [ color ] ( sign ) ;
150
- const summary = `${ decoration } found ${ report . errors . length } problems, ${ report . warnings . length } warnings` ;
151
- return [ ...problems , chalk . bold ( summary ) ] ;
152
- }
71
+ // Init meow 😸cli
72
+ const cli = meow ( {
73
+ help : `[input] reads from stdin if --edit, --from, --to are omitted\n${ help ( configuration ) } ` ,
74
+ description : `${ pkg . name } @${ pkg . version } - ${ pkg . description } `
75
+ } , configuration ) ;
153
76
154
77
// Assemble the engine
155
78
async function main ( options ) {
@@ -164,11 +87,11 @@ async function main(options) {
164
87
165
88
return Promise . all ( input
166
89
. map ( async commit => {
90
+ const fmt = new chalk . constructor ( { enabled : flags . color } ) ;
91
+
167
92
const report = lint ( commit , {
168
- preset : await require ( `conventional-changelog-${ flags . preset } ` ) ,
169
- configuration : getConfiguration ( 'conventional-changelog-lint' , {
170
- prefix : `conventional-changelog-lint-config`
171
- } )
93
+ preset : await getPreset ( flags . preset ) ,
94
+ configuration : await getConfiguration ( )
172
95
} ) ;
173
96
174
97
const formatted = format ( report , {
@@ -178,15 +101,17 @@ async function main(options) {
178
101
} ) ;
179
102
180
103
if ( ! flags . quiet ) {
181
- console . log ( `validating : ${ commit . split ( '\n' ) [ 0 ] } ` ) ;
104
+ console . log ( `${ fmt . grey ( '⧗' ) } input : ${ fmt . bold ( commit . split ( '\n' ) [ 0 ] ) } ` ) ;
182
105
console . log (
183
106
formatted
184
107
. join ( '\n' )
185
108
) ;
186
109
}
187
110
188
111
if ( report . errors . length > 0 ) {
189
- throw new Error ( formatted [ formatted . length - 1 ] ) ;
112
+ const error = new Error ( formatted [ formatted . length - 1 ] ) ;
113
+ error . type = pkg . name ;
114
+ throw error ;
190
115
}
191
116
192
117
console . log ( '' ) ;
0 commit comments