Skip to content

Commit ebaf1d4

Browse files
committed
feat(index.js): add option to have maximum line length in commit message
1 parent df49d3a commit ebaf1d4

1 file changed

Lines changed: 51 additions & 9 deletions

File tree

index.js

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ class gitCommitMsg {
1818
'perf',
1919
'test',
2020
'revert'
21-
]
21+
],
22+
lineLength: 72
2223
}
2324

2425
this.init();
@@ -27,14 +28,7 @@ class gitCommitMsg {
2728
init() {
2829
// Check if config exists and if we need to use the default one
2930
if (this.package['git-commit-hook']) {
30-
const config = this.package['git-commit-hook'];
31-
32-
if (config.types && !Array.isArray(config.types)) {
33-
console.error('git-commit-hook: config.types is suppose to be array');
34-
process.exit(1);
35-
} else if (config.types) {
36-
this.config.types = config.types;
37-
}
31+
this.populateConfig();
3832
}
3933

4034
// Breaking the message by line
@@ -44,17 +38,63 @@ class gitCommitMsg {
4438
if (!this.checkTypeSubject()) {
4539
this.stop();
4640
}
41+
42+
// Check if lines length is fine, check only if lineLength is integer and not boolean false
43+
if (this.config.lineLength !== 0 && !this.checkLength()) {
44+
this.stop();
45+
}
46+
4747
console.log('error');
4848
this.stop();
4949

5050
process.exit(0);
5151
}
5252

53+
// Replace default config with config from package.json
54+
populateConfig() {
55+
const config = this.package['git-commit-hook'];
56+
57+
// In case there is config.types available in package.json, check if it's array and if set, if set, replace default one
58+
if (config.types && !Array.isArray(config.types)) {
59+
console.error('git-commit-hook: config.types is suppose to be array');
60+
this.stop();
61+
} else if (config.types) {
62+
this.config.types = config.types;
63+
}
64+
65+
// In case there is config.lineLength available in package.json, check if it's number and if set, if set, replace default one
66+
if (config.lineLength && typeof config.lineLength !== 'number') {
67+
console.error('git-commit-hook: config.lineLength is suppose to be number, remove the rule or set to 0 if you don\'t want to force it');
68+
this.stop();
69+
} else if (config.lineLength) {
70+
this.config.lineLength = config.lineLength;
71+
}
72+
}
73+
74+
// Check length of every line of commit message and validate that there are no long lines in it
75+
checkLength() {
76+
// Start line with number one, to make it less confusing
77+
let line = 1;
78+
79+
for (const message of this.message) {
80+
// If line is longer than allowed, show error message and exit the process
81+
if (message.length > this.config.lineLength) {
82+
console.error(`git-commit-hook: Commit message in line ${line} have more characters than allowed in a single line, please break it down, maximum is ${this.config.lineLength}`);
83+
return false;
84+
}
85+
86+
line++;
87+
}
88+
}
89+
90+
// Check first line of commit message, if there is [Type]([optional scope]): [Subject]
91+
// Semicolon with space is always mandatory after the Type
5392
checkTypeSubject() {
5493
const types = this.config.types.join('|');
5594
let regStr = `(${types})(\(.*\))?\\:`;
5695
const regExpType = new RegExp(regStr);
5796

97+
// Check type and semicolon
5898
if (this.message[0].search(regExpType) === -1) {
5999
console.error(`git-commit-hook: Type should follow the rules "${types}(scope/filename): Subject"`);
60100
return false;
@@ -63,6 +103,7 @@ class gitCommitMsg {
63103
regStr += ' .*';
64104
const regExpSubject = new RegExp(regStr);
65105

106+
// Check if there is subject after semicolon
66107
if (this.message[0].search(regExpSubject) === -1) {
67108
console.error('git-commit-hook: Subject is not set or there is no space after semicolon');
68109
return false;
@@ -71,6 +112,7 @@ class gitCommitMsg {
71112
return true;
72113
}
73114

115+
// Killing the process
74116
stop() {
75117
process.exit(1);
76118
}

0 commit comments

Comments
 (0)