Skip to content
This repository has been archived by the owner on Feb 5, 2018. It is now read-only.

Commit

Permalink
Merge pull request #22 from bitjson/master
Browse files Browse the repository at this point in the history
feat(config): add helpMessage config option
  • Loading branch information
Kent C. Dodds committed Mar 3, 2016
2 parents 8e3a5a2 + d67ff30 commit 75e6ad0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ You can specify options in `package.json`
"validate-commit-msg": {
"types": ["feat", "fix", "docs", "style", "refactor", "perf", "test", "chore", "revert"], // default
"warnOnFail": false, // default
"maxSubjectLength": 100 // default
"maxSubjectLength": 100, // default
"helpMessage": "" //default
}
}
}
Expand All @@ -48,6 +49,10 @@ If this is set to `true` errors will be logged to the console, however the commi

This will control the maximum length of the subject.

#### helpMessage

If provided, the helpMessage string is displayed when a commit message is not valid. This allows projects to provide a better developer experience for new contributors.

### Other notes

If the commit message begins with `WIP` then none of the validation will happen.
Expand All @@ -57,4 +62,3 @@ If the commit message begins with `WIP` then none of the validation will happen.

This was originally developed by contributors to [the angular.js project](https://github.com/angular/angular.js). I
pulled it out so I could re-use this same kind of thing in other projects.

12 changes: 9 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,14 @@ var validateMessage = function(message) {

isValid = isValid || config.warnOnFail;

// Display original message when it is not valid, otherwise it will be lost
if (!isValid && message) {
console.log(message);
if(!isValid){
if(message){
// Display original message when it is not valid, otherwise it will be lost
console.log(message);
}
if(config.helpMessage) {
console.log(config.helpMessage);
}
}

return isValid;
Expand All @@ -87,6 +92,7 @@ var validateMessage = function(message) {

// publish for testing
exports.validateMessage = validateMessage;
exports.config = config;

// hacky start if not run by mocha :-D
// istanbul ignore next
Expand Down
12 changes: 12 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ describe('validate-commit-msg.js', function() {
var VALID = true;
var INVALID = false;

// modify project config for testing
m.config.helpMessage = undefined;

beforeEach(function() {
errors.length = 0;
logs.length = 0;
Expand Down Expand Up @@ -87,6 +90,15 @@ describe('validate-commit-msg.js', function() {
expect(logs).to.deep.equal([msg]);
});

it('should log the helpMessage on invalid commit messages', function() {
var msg = 'invalid message';
m.config.helpMessage = '\nPlease fix your commit message (and consider using http://npm.im/commitizen)\n';
expect(m.validateMessage(msg)).to.equal(INVALID);
expect(errors).to.deep.equal(['INVALID COMMIT MSG: does not match "<type>(<scope>): <subject>" !']);
expect(logs).to.deep.equal([msg, m.config.helpMessage]);
m.config.helpMessage = undefined;
});


it('should validate type', function() {
var msg = 'weird($filter): something';
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"path": "node_modules/cz-conventional-changelog"
},
"validate-commit-msg": {
"helpMessage": "\nPlease fix your commit message (and consider using http://npm.im/commitizen)\n",
"types": [
"feat",
"fix",
Expand Down

0 comments on commit 75e6ad0

Please sign in to comment.