Skip to content

Commit 67525e2

Browse files
committed
✨ Feat: add rules max-char, min-char, end-with-dot
1 parent f978ec9 commit 67525e2

File tree

4 files changed

+55
-4
lines changed

4 files changed

+55
-4
lines changed

.sgcrc_default

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,11 @@
4545
"type": "Test:",
4646
"description": "Adding missing tests or correcting existing tests"
4747
}
48-
]
48+
],
49+
"rules": {
50+
"max-char-commit-message": true,
51+
"max-char": 72,
52+
"min-char": 10,
53+
"end-with-dot": false
54+
}
4955
}

lib/cli.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { choices, questions } from './promptConfig';
1111

1212
const configuration = getConfig();
1313
const choicesList = choices(configuration);
14-
const questionsList = questions(choicesList);
14+
const questionsList = questions(choicesList, configuration);
1515
const argv = yargs
1616
.usage('Usage: $0')
1717
.alias('v', 'version')

lib/promptConfig.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import chalk from 'chalk';
2+
import { checkRulesMaxLength, checkRulesEndWithDot, checkRulesMinLength } from './rulesConfig';
23

34
const choices = (configuration) => {
45
const choicesList = [];
@@ -17,7 +18,7 @@ const choices = (configuration) => {
1718
return choicesList;
1819
};
1920

20-
const questions = (choicesList) => {
21+
const questions = (choicesList, configuration) => {
2122
const questionsList = [
2223
{
2324
type: 'list',
@@ -29,7 +30,12 @@ const questions = (choicesList) => {
2930
type: 'input',
3031
name: 'description',
3132
message: 'Enter your commit message:',
32-
validate: input => (input ? true : 'A commit message is mandatory!'),
33+
validate: (input) => {
34+
if (checkRulesEndWithDot(input) === false) return 'The commit message can not end with a dot';
35+
else if (checkRulesMaxLength(input) === false) return `The commit message is not allowed to be longer as ${configuration.rules['max-char']}. Consider writing a body.`;
36+
else if (checkRulesMinLength(input) === false) return `The commit message hast to be at least ${configuration.rules['min-char']} character.`;
37+
return true;
38+
},
3339
},
3440
{
3541
type: 'confirm',

lib/rulesConfig.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import getConfig from './getConfig';
2+
3+
const checkRulesMaxLength = (input) => {
4+
const configuration = getConfig();
5+
6+
if (configuration.rules['max-char-commit-message'] === true && input.length > configuration.rules['max-char']) {
7+
return false;
8+
} else if ((configuration.rules['max-char-commit-message'] === true && input.length <= configuration.rules['max-char']) || configuration.rules['max-char-commit-message'] === false) {
9+
return true;
10+
}
11+
12+
return false;
13+
};
14+
15+
const checkRulesMinLength = (input) => {
16+
const configuration = getConfig();
17+
18+
if (input.length < configuration.rules['min-char']) {
19+
return false;
20+
} else if (input.length >= configuration.rules['min-char']) {
21+
return true;
22+
}
23+
24+
return false;
25+
};
26+
27+
const checkRulesEndWithDot = (input) => {
28+
const configuration = getConfig();
29+
30+
if (configuration.rules['end-with-dot'] === true && input[input.length - 1] === '.') {
31+
return false;
32+
} else if (configuration.rules['end-with-dot'] === false && input[input.length - 1] !== '.') {
33+
return true;
34+
}
35+
36+
return false;
37+
};
38+
39+
export { checkRulesMaxLength, checkRulesMinLength, checkRulesEndWithDot };

0 commit comments

Comments
 (0)