Skip to content

Commit 0272f74

Browse files
author
Max Orlovsky
committed
feat(scope): add mandatory/optional scope config
1 parent b602785 commit 0272f74

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ Configuration is simple and is done in `package.json`, you just need to add git-
3939
"test",
4040
"revert"
4141
],
42-
"lineLength": 72
42+
"lineLength": 72,
43+
"scope": {
44+
"mandatory": false,
45+
"rules": ""
46+
}
4347
}
4448
```
4549

index.js

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ class gitCommitMsg {
1919
'test',
2020
'revert'
2121
],
22-
lineLength: 72
22+
lineLength: 72,
23+
scope: {
24+
mandatory: false,
25+
rules: ""
26+
}
2327
}
2428

2529
this.init();
@@ -39,6 +43,10 @@ class gitCommitMsg {
3943
this.stop();
4044
}
4145

46+
if (!this.checkScope()) {
47+
this.stop();
48+
}
49+
4250
// Check if lines length is fine, check only if lineLength is integer and not boolean false
4351
if (this.config.lineLength !== 0 && !this.checkLength()) {
4452
this.stop();
@@ -66,6 +74,11 @@ class gitCommitMsg {
6674
} else if (config.lineLength) {
6775
this.config.lineLength = config.lineLength;
6876
}
77+
78+
// In case there is config.scope, replace default one
79+
if (config.scope) {
80+
this.config.scope = config.scope;
81+
}
6982
}
7083

7184
// Check length of every line of commit message and validate that there are no long lines in it
@@ -111,6 +124,45 @@ class gitCommitMsg {
111124
return true;
112125
}
113126

127+
// Check if scope after type is mandatory
128+
checkScope() {
129+
// Check first if config for scope is set up
130+
// If not, return true as default or old rules are set
131+
if (!this.config.scope) {
132+
return true;
133+
}
134+
135+
// Check if rule for mandatory scope is set
136+
// If not, return true
137+
if (!this.config.scope.mandatory) {
138+
return true;
139+
}
140+
141+
let rules = this.config.scope.rules;
142+
143+
// Check if rules already have brackets
144+
// If yes, strip them
145+
if (rules.substr(0, 1) === '(' && rules.substr(-1) === ')') {
146+
rules = rules.slice(1, -1);
147+
}
148+
149+
// Add proper regex round brackets
150+
// Add additional backslashes for string to work properly with regex
151+
const regStr = '\\(' + rules + '\\)';
152+
153+
// Check if we have scope in round brackets
154+
const regExpType = new RegExp(regStr);
155+
156+
// Check type and semicolon
157+
if (this.message[0].search(regExpType) === -1) {
158+
console.error(`git-commit-hook: Scope is not following the regex rules "${regStr}"`);
159+
console.error(`git-commit-hook: Don't forget to put 2 backslashes instead of one`);
160+
return false;
161+
}
162+
163+
return true;
164+
}
165+
114166
// Killing the process
115167
stop() {
116168
process.exit(1);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "git-commit-msg",
3-
"version": "0.2.2",
3+
"version": "0.3.0",
44
"description": "Install commit-msg hook into .git folder",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)