Skip to content

Commit 5265d3c

Browse files
chore: cleanup
1 parent 5b3b1f6 commit 5265d3c

File tree

7 files changed

+99
-30
lines changed

7 files changed

+99
-30
lines changed

src/engine.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { footerMaker } from './prompts/footer-maker';
77
import { bodyMaker } from './prompts/body-maker';
88
import { scopeMaker } from './prompts/scope-maker';
99
import { subjectMaker } from './prompts/subject-maker';
10-
import { Question } from './commit-template';
10+
import { Question, commitTemplate } from './commit-template';
1111

1212
function buildQuestions(rules: Rules) {
1313
const combinedQuestions = pipeWith<Question[]>(
@@ -24,7 +24,10 @@ function buildQuestions(rules: Rules) {
2424

2525
export async function engine(config: CommitlintConfig, prompt: PromptModule, commit: Commit) {
2626
const questions = buildQuestions(config.rules);
27+
2728
const answers = await prompt(questions);
2829

29-
console.log(JSON.stringify(answers));
30+
const message = commitTemplate(answers);
31+
32+
return commit(message);
3033
}

src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,9 @@ export async function prompter(cz: { prompt: PromptModule }, commit: Commit) {
88
engine(clConfig, cz.prompt, commit);
99
}
1010

11-
prompter({ prompt }, value => console.log(value));
11+
/**
12+
* Hacking
13+
*
14+
* Uncomment the following line for simplified local development without commitizen.
15+
*/
16+
// prompter({ prompt }, value => console.log(value));

src/prompts/footer-maker.ts

Lines changed: 76 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,83 @@
11
import { 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';
54
import { validate, maxLengthValidator, minLengthValidator } from '../validators';
5+
import { leadingBlankFilter, maxLineLengthFilter } from '../filters';
66

77
export 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

src/prompts/subject-maker.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { Rules, Level } from '@commitlint/load';
22

33
import { green, red } from 'chalk';
4-
import { PromptAnswers } from '../commit-template';
4+
import { Answers } from '../commit-template';
55
import { validatorFactory, filterFactory, messageFactory, transformerFactory, subjectMaker } from './subject-maker';
66

77
describe('subject-maker', () => {
88
describe('validatorFactory', () => {
9-
test.each<[Rules, string, PromptAnswers, string | true]>([
9+
test.each<[Rules, string, Answers, string | true]>([
1010
[
1111
{ 'header-max-length': [Level.Error, 'always', 3] },
1212
'too long',
@@ -52,7 +52,7 @@ describe('subject-maker', () => {
5252
});
5353

5454
describe('messageFactory', () => {
55-
test.each<[Rules, PromptAnswers, string]>([
55+
test.each<[Rules, Answers, string]>([
5656
[
5757
{ 'header-max-length': [Level.Error, 'always', 72] },
5858
{ type: 'feat', scope: 'foo' },
@@ -69,7 +69,7 @@ describe('subject-maker', () => {
6969
});
7070

7171
describe('transformerFactory', () => {
72-
test.each<[Rules, string, PromptAnswers, string]>([
72+
test.each<[Rules, string, Answers, string]>([
7373
[{ 'header-max-length': [Level.Error, 'always', 14] }, 'foo', { type: 'feat', scope: 'bar' }, green('(3) foo')],
7474
[{ 'header-max-length': [Level.Error, 'always', 13] }, 'foo', { type: 'feat', scope: 'bar' }, red('(3) foo')],
7575
[{ 'subject-max-length': [Level.Error, 'always', 3] }, 'foo', {}, green('(3) foo')],

src/prompts/subject-maker.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import { red, green } from 'chalk';
33
import { pipeWith, valueFromRule } from '../utils';
44
import { caseValidator, emptyValidator, maxLengthValidator, minLengthValidator, validate } from '../validators';
55
import { fullStopFilter, wordCaseFilter } from '../filters';
6-
import { headerTemplate, PromptAnswers, Question } from '../commit-template';
6+
import { headerTemplate, Answers, Question } from '../commit-template';
77

88
export function validatorFactory(rules: Rules) {
9-
return (value: string, answers: PromptAnswers) => {
9+
return (value: string, answers: Answers) => {
1010
const headerValue = headerTemplate(answers.type, answers.scope, value);
1111

1212
return validate([
@@ -54,7 +54,7 @@ export function filterFactory(rules: Rules) {
5454
}
5555

5656
export function messageFactory(rules: Rules) {
57-
return (answers: PromptAnswers) => {
57+
return (answers: Answers) => {
5858
const maxLength = valueFromRule(rules['header-max-length']);
5959

6060
if (!maxLength) {
@@ -69,7 +69,7 @@ export function messageFactory(rules: Rules) {
6969
export function transformerFactory(rules: Rules) {
7070
const filter = filterFactory(rules);
7171

72-
return (value: string, answers: PromptAnswers) => {
72+
return (value: string, answers: Answers) => {
7373
const headerMaxLength = valueFromRule(rules['header-max-length']);
7474

7575
if (headerMaxLength) {

src/prompts/type-maker.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { Rules } from '@commitlint/load';
2-
import { ChoiceOptions, DistinctQuestion, ListQuestion } from 'inquirer';
2+
import { ChoiceOptions, ListQuestion } from 'inquirer';
33
import { types, CommitType } from 'conventional-commit-types';
44
import { getLongest } from '../utils';
55
import { caseValidator, emptyValidator, maxLengthValidator, minLengthValidator, validate } from '../validators';
66
import { whenFactory } from '../when';
77
import { wordCaseFilter } from '../filters';
8-
import { Question, PromptAnswers } from '../commit-template';
8+
import { Question, Answers } from '../commit-template';
99

1010
export function validatorFactory(rules: Rules) {
1111
return (value: string) => {
@@ -59,7 +59,7 @@ export function choicesFactory(rules: Rules, commitTypes: CommitType) {
5959
}
6060

6161
export function typeMaker(questions: Question[], rules: Rules): Question[] {
62-
const question: ListQuestion<PromptAnswers> = {
62+
const question: ListQuestion<Answers> = {
6363
name: 'type',
6464
message: "Select the type of change you're committing:\n",
6565
type: 'list',

src/typings.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ declare module '@commitlint/load' {
3131
'footer-leading-blank'?: Rule<undefined>;
3232
'footer-max-length'?: Rule<number>;
3333
'footer-min-length'?: Rule<number>;
34+
'footer-max-line-length'?: Rule<number>;
3435
'header-case'?: Rule<Case>;
3536
'header-full-stop'?: Rule<string>;
3637
'header-max-length'?: Rule<number>;

0 commit comments

Comments
 (0)