11import { Rules } from '@commitlint/load' ;
2- import { DistinctQuestion , Answers } from 'inquirer' ;
3- import { valueFromRule } from '../utils' ;
4- import { PromptAnswers } from '../commit-template' ;
2+ import { valueFromRule , maxLengthTransformerFactory , pipeWith } from '../utils' ;
3+ import { Answers , Question } from '../commit-template' ;
54import { validate , maxLengthValidator , minLengthValidator } from '../validators' ;
5+ import { leadingBlankFilter , maxLineLengthFilter } from '../filters' ;
66
77export function validatorFactory ( rules : Rules ) {
8- return ( value : string ) =>
9- validate ( [
8+ return ( value : string , answers : Answers ) => {
9+ const breaking = answers . breaking ?? '' ;
10+
11+ return validate ( [
1012 {
11- value,
13+ value : value + breaking ,
1214 rule : rules [ 'footer-max-length' ] ,
1315 validator : maxLengthValidator ,
14- message : length => `Body maximum length of ${ length } has been exceeded`
16+ message : length => 'Footer maximum length of ${length} has been exceeded'
1517 } ,
1618 {
17- value,
19+ value : value + breaking ,
1820 rule : rules [ 'footer-min-length' ] ,
1921 validator : minLengthValidator ,
20- message : length => `Subject minimum length of ${ length } has not been met`
22+ message : length => `Footer minimum length of ${ length } has not been met`
2123 }
2224 ] ) ;
25+ } ;
26+ }
27+
28+ export function filterFactory ( rules : Rules ) {
29+ return ( value : string ) : string =>
30+ pipeWith < string > (
31+ value ,
32+ v => leadingBlankFilter ( v , rules [ 'footer-leading-blank' ] ) ,
33+ v => maxLineLengthFilter ( v , rules [ 'footer-max-line-length' ] )
34+ ) ;
2335}
2436
25- function breakingChangeMessageFactory ( rules : Rules ) {
26- return ( answers : Answers ) => {
37+ export function breakingChangeMessageFactory ( rules : Rules ) {
38+ return ( ) => {
2739 const maxLength = valueFromRule ( rules [ 'footer-max-length' ] ) ;
2840
2941 if ( ! maxLength ) {
30- return `Describe the breaking changes:\n:\n ` ;
42+ return `Describe the breaking changes:\n` ;
3143 }
3244
3345 return `Describe the breaking changes:\n (max ${ maxLength } chars):\n` ;
3446 } ;
3547}
3648
37- export function footerMaker ( questions : DistinctQuestion [ ] , rules : Rules ) : DistinctQuestion [ ] {
38- const breakingQuestions : DistinctQuestion < PromptAnswers > [ ] = [
49+ export function issuesMessageFactory ( rules : Rules ) {
50+ return ( ) => {
51+ const maxLength = valueFromRule ( rules [ 'footer-max-length' ] ) ;
52+
53+ if ( ! maxLength ) {
54+ return `List issues fixed:\n` ;
55+ }
56+
57+ return `List issues fixed:\n (max ${ maxLength } chars):\n` ;
58+ } ;
59+ }
60+
61+ function isFixCommit ( answers : Answers ) {
62+ return answers ?. type == 'fix' ?? false ;
63+ }
64+
65+ export function issuesTransformerFactory ( rules : Rules ) {
66+ return ( value : string , answers : Answers ) => {
67+ const breaking = answers . breaking ?? '' ;
68+
69+ const footerMaxLength = valueFromRule ( rules [ 'footer-max-length' ] ) ;
70+
71+ if ( footerMaxLength ) {
72+ return maxLengthTransformerFactory ( footerMaxLength - breaking . length ) ( value ) ;
73+ }
74+
75+ return value ;
76+ } ;
77+ }
78+
79+ export function footerMaker ( questions : Question [ ] , rules : Rules ) : Question [ ] {
80+ const breakingQuestions : Question [ ] = [
3981 {
4082 type : 'confirm' ,
4183 name : 'isBreaking' ,
@@ -44,10 +86,28 @@ export function footerMaker(questions: DistinctQuestion[], rules: Rules): Distin
4486 } ,
4587 {
4688 type : 'input' ,
47- name : 'beaking ' ,
89+ name : 'breaking ' ,
4890 message : breakingChangeMessageFactory ( rules ) ,
4991 when : answers => ! ! answers . isBreaking ,
50- validate : validatorFactory ( rules )
92+ validate : validatorFactory ( rules ) ,
93+ transformer : maxLengthTransformerFactory ( valueFromRule ( rules [ 'footer-max-length' ] ) ) ,
94+ filter : filterFactory ( rules )
95+ } ,
96+ {
97+ type : 'confirm' ,
98+ name : 'isIssue' ,
99+ message : 'Does this fix any issues?' ,
100+ when : answers => ! isFixCommit ( answers ) ,
101+ default : false
102+ } ,
103+ {
104+ type : 'input' ,
105+ name : 'issue' ,
106+ message : issuesMessageFactory ( rules ) ,
107+ when : answers => isFixCommit ( answers ) || ! ! answers . isIssue ,
108+ validate : validatorFactory ( rules ) ,
109+ transformer : issuesTransformerFactory ( rules ) ,
110+ filter : filterFactory ( rules )
51111 }
52112 ] ;
53113
0 commit comments