@@ -38,6 +38,17 @@ git commit -m"feat(core)!: commit description"
3838 const pkg = this . _findGitPackage ( ) ;
3939 if ( ! pkg ) return ;
4040
41+ // prepare types
42+ const types = { } ;
43+ for ( const [ type , config ] of Object . entries ( pkg . cliConfig ?. commits . types || { } ) ) {
44+ if ( ! config ) continue ;
45+
46+ types [ type ] = {
47+ ...config ,
48+ type,
49+ } ;
50+ }
51+
4152 const message = fs . readFileSync ( process . cli . arguments . path , "utf8" ) ,
4253 isRevert = fs . existsSync ( ".git/REVERT_HEAD" ) ;
4354
@@ -53,7 +64,7 @@ git commit -m"feat(core)!: commit description"
5364 // merge commit
5465 if ( commit . isMerge ) {
5566
56- // merge commit shoild have default message
67+ // merge commit should have default message
5768 if ( ! commit . hasDefaultMergeSubject ) {
5869 return this . #throwError( `Merge commit message should start with "Merge" keyword.` ) ;
5970 }
@@ -79,28 +90,17 @@ git commit -m"feat(core)!: commit description"
7990 return this . #throwError( `Commit message subject should not ends with ".".` ) ;
8091 }
8192
82- const type = pkg . cliConfig ?. commits . types [ commit . type ] || null ,
83- scopes = new Set ( Object . entries ( pkg . cliConfig ?. commits [ type ?. primaryChange
93+ const type = types [ commit . type ] ,
94+ scopes = new Map ( Object . entries ( pkg . cliConfig ?. commits [ type ?. primaryChange
8495 ? "primaryScopes"
85- : "secondaryScopes" ] || { } )
86- . map ( ( [ key , value ] ) => ( value
87- ? key
88- : null ) )
89- . filter ( item => item ) ) ;
96+ : "secondaryScopes" ] || { } ) . filter ( ( [ key , value ] ) => value ) ) ;
9097
9198 // check strict type
9299 if ( pkg . cliConfig ?. commits . strictType ) {
93100
94101 // commit type is not valid
95102 if ( ! type ) {
96- return this . #throwError( `Commit type is not valid. Allowed types: ${ Object . entries ( pkg . cliConfig . commits . types )
97- . map ( ( key , value ) => ( value
98- ? key
99- : null ) )
100- . filter ( item => item )
101- . sort ( )
102- . map ( item => `"${ item } "` )
103- . join ( ", " ) } .` ) ;
103+ return this . #throwError( `Commit type is not valid. Allowed types:\n${ this . #getAllowedTypes( types ) } ` ) ;
104104 }
105105 }
106106
@@ -113,10 +113,7 @@ git commit -m"feat(core)!: commit description"
113113 // commit scope is not valid
114114 if ( ! scopes . has ( commit . scope ) ) {
115115 if ( scopes . size ) {
116- return this . #throwError( `Commit scope is not valid. Allowed scopes for this commit type are: ${ [ ...scopes ]
117- . sort ( )
118- . map ( item => `"${ item } "` )
119- . join ( ", " ) } .` ) ;
116+ return this . #throwError( `Commit scope is not valid. Allowed scopes for this commit type are:\n${ this . #getAllowedScopes( scopes ) } ` ) ;
120117 }
121118 else {
122119 return this . #throwError( `Commit scope is not allowed for this commit type.` ) ;
@@ -127,10 +124,7 @@ git commit -m"feat(core)!: commit description"
127124
128125 // commit scope is required
129126 else if ( type . requireScope && scopes . size ) {
130- return this . #throwError( `Commit scope is required. Allowed scopes for this commit type are: ${ [ ...scopes ]
131- . sort ( )
132- . map ( item => `"${ item } "` )
133- . join ( ", " ) } .` ) ;
127+ return this . #throwError( `Commit scope is required. Allowed scopes for this commit type are:\n${ this . #getAllowedScopes( scopes ) } ` ) ;
134128 }
135129
136130 // restrict breaking changes to the primary changes only
@@ -148,11 +142,21 @@ git commit -m"feat(core)!: commit description"
148142
149143 // commit type is required
150144 else if ( pkg . cliConfig ?. commits . requireType ) {
151- return this . #throwError( `Commit message must follow conventional commits syntax. ` ) ;
145+ return this . #throwError( `Commit type is required. Allowed types:\n ${ this . #getAllowedTypes ( types ) } ` ) ;
152146 }
153147 }
154148
155149 // private
150+ #getAllowedTypes ( types ) {
151+ return Object . values ( types )
152+ . map ( type => ` - "${ type . type } ": ${ type . description } ;` )
153+ . join ( "\n" ) ;
154+ }
155+
156+ #getAllowedScopes ( scopes ) {
157+ return [ ...scopes . keys ( ) ] . map ( scope => ` - "${ scope } ": ${ scopes . get ( scope ) } ;` ) . join ( "\n" ) ;
158+ }
159+
156160 #throwError ( message ) {
157161 throw result ( [ 500 , `${ message } \nRefer to the documentation: ${ ansi . link ( "https://www.conventionalcommits.org/en/" ) } .` ] ) ;
158162 }
0 commit comments