@@ -19,7 +19,11 @@ class gitCommitMsg {
1919 'test' ,
2020 'revert'
2121 ] ,
22- lineLength : 72
22+ lineLength : 72 ,
23+ scope : {
24+ mandatory : false ,
25+ rules : ""
26+ }
2327 }
2428
2529 this . init ( ) ;
@@ -39,6 +43,10 @@ class gitCommitMsg {
3943 this . stop ( ) ;
4044 }
4145
46+ if ( ! this . checkScope ( ) ) {
47+ this . stop ( ) ;
48+ }
49+
4250 // Check if lines length is fine, check only if lineLength is integer and not boolean false
4351 if ( this . config . lineLength !== 0 && ! this . checkLength ( ) ) {
4452 this . stop ( ) ;
@@ -66,6 +74,11 @@ class gitCommitMsg {
6674 } else if ( config . lineLength ) {
6775 this . config . lineLength = config . lineLength ;
6876 }
77+
78+ // In case there is config.scope, replace default one
79+ if ( config . scope ) {
80+ this . config . scope = config . scope ;
81+ }
6982 }
7083
7184 // Check length of every line of commit message and validate that there are no long lines in it
@@ -111,6 +124,45 @@ class gitCommitMsg {
111124 return true ;
112125 }
113126
127+ // Check if scope after type is mandatory
128+ checkScope ( ) {
129+ // Check first if config for scope is set up
130+ // If not, return true as default or old rules are set
131+ if ( ! this . config . scope ) {
132+ return true ;
133+ }
134+
135+ // Check if rule for mandatory scope is set
136+ // If not, return true
137+ if ( ! this . config . scope . mandatory ) {
138+ return true ;
139+ }
140+
141+ let rules = this . config . scope . rules ;
142+
143+ // Check if rules already have brackets
144+ // If yes, strip them
145+ if ( rules . substr ( 0 , 1 ) === '(' && rules . substr ( - 1 ) === ')' ) {
146+ rules = rules . slice ( 1 , - 1 ) ;
147+ }
148+
149+ // Add proper regex round brackets
150+ // Add additional backslashes for string to work properly with regex
151+ const regStr = '\\(' + rules + '\\)' ;
152+
153+ // Check if we have scope in round brackets
154+ const regExpType = new RegExp ( regStr ) ;
155+
156+ // Check type and semicolon
157+ if ( this . message [ 0 ] . search ( regExpType ) === - 1 ) {
158+ console . error ( `git-commit-hook: Scope is not following the regex rules "${ regStr } "` ) ;
159+ console . error ( `git-commit-hook: Don't forget to put 2 backslashes instead of one` ) ;
160+ return false ;
161+ }
162+
163+ return true ;
164+ }
165+
114166 // Killing the process
115167 stop ( ) {
116168 process . exit ( 1 ) ;
0 commit comments