Skip to content

Commit

Permalink
feat(config): allow custom error message for pattern validation
Browse files Browse the repository at this point in the history
adds "msg-pattern-error" as a available option in the config object to throw a custom error message when the "msg-pattern" validation fails
  • Loading branch information
hmagrini committed Feb 22, 2017
1 parent 70c2b20 commit 4bfc792
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
8 changes: 8 additions & 0 deletions bin/commit-msg.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ function checkMessageAgainstPattern(msg, pattern) {
var regex = new RegExp(pattern);

if (!regex.test(msg)) {

var msgError = preGit.customMsgPatternError();

if(msgError) {
console.error(msgError);
console.error('');
}

log('invalid commit message, must match the following pattern: ' + pattern, msg);
process.exit(-1);
}
Expand Down
30 changes: 20 additions & 10 deletions src/pre-git.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,20 @@ function getConfig() {
return pkg.config && pkg.config[packageName];
}

function getConfigProperty(propertyName) {
const config = getConfig();
if (!config) {
return false;
}
const property = config[propertyName];

if (!property) {
return false;
}

return property;
}

function hasEnabledOption(config) {
return 'enabled' in config;
}
Expand Down Expand Up @@ -362,17 +376,12 @@ function pickWizard() {
}

function customCommitMsgPattern() {
const config = getConfig();
if (!config) {
return false;
}
const msgPattern = config['msg-pattern'];
return getConfigProperty('msg-pattern');

if (!msgPattern) {
return false;
}
}

return msgPattern;
function customCommitMsgPatternError() {
return getConfigProperty('msg-pattern-error');
}

module.exports = {
Expand All @@ -382,7 +391,8 @@ module.exports = {
printError: printError,
wizard: pickWizard,
hasUntrackedFiles: hasUntrackedFiles,
customMsgPattern: customCommitMsgPattern
customMsgPattern: customCommitMsgPattern,
customMsgPatternError: customCommitMsgPatternError
};

if (!module.parent) {
Expand Down

0 comments on commit 4bfc792

Please sign in to comment.