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

Commit

Permalink
refactor(scopes): Update scopes feature per PR feedback. config prope…
Browse files Browse the repository at this point in the history
…rty now . Validation moved into its own function. Fat arrow removed

since it wasn't necessary.
  • Loading branch information
Garbee committed Jan 26, 2017
1 parent fde2abb commit 3d21903
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 48 deletions.
6 changes: 5 additions & 1 deletion .all-contributorsrc
Expand Up @@ -253,7 +253,11 @@
"name": "Jonathan Garbee",
"avatar_url": "https://avatars.githubusercontent.com/u/868301?v=3",
"profile": "http://jonathan.garbee.me",
"contributions": []
"contributions": [
"code",
"doc",
"test"
]
}
]
}
8 changes: 4 additions & 4 deletions README.md
Expand Up @@ -34,7 +34,7 @@ You can specify options in `.vcmrc`
```js
{
"types": ["feat", "fix", "docs", "style", "refactor", "perf", "test", "chore", "revert"], // default
"scopes": {
"scope": {
required: false, // default,
allowed: ['button', 'card'], // default is '*' for anything,
validate: false, // default,
Expand All @@ -56,7 +56,7 @@ or in `package.json`
"config": {
"validate-commit-msg": {
"types": ["feat", "fix", "docs", "style", "refactor", "perf", "test", "chore", "revert"], // default
"scopes": {
"scope": {
required: false, // default,
allowed: ['button', 'card'], // default is '*' for anything,
validate: false, // default,
Expand Down Expand Up @@ -85,7 +85,7 @@ Or you can specify the name of a module that exports types according to the
[conventional-commit-types](https://github.com/adjohnson916/conventional-commit-types)
spec, e.g. `"types": "conventional-commit-types"`.

#### scopes
#### scope

This object defines scope requirements for the commit message. Possible properties are:

Expand Down Expand Up @@ -189,7 +189,7 @@ Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
| [<img src="https://avatars.githubusercontent.com/u/2112202?v=3" width="100px;"/><br /><sub>Shawn Erquhart</sub>](http://www.professant.com)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=erquhart) [📖](https://github.com/kentcdodds/validate-commit-msg/commits?author=erquhart) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=erquhart) | [<img src="https://avatars.githubusercontent.com/u/194482?v=3" width="100px;"/><br /><sub>Tushar Mathur</sub>](http://tusharm.com)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=tusharmath) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=tusharmath) | [<img src="https://avatars.githubusercontent.com/u/904007?v=3" width="100px;"/><br /><sub>Jason Dreyzehner</sub>](https://twitter.com/bitjson)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=bitjson) [📖](https://github.com/kentcdodds/validate-commit-msg/commits?author=bitjson) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=bitjson) | [<img src="https://avatars.githubusercontent.com/u/9654923?v=3" width="100px;"/><br /><sub>Abimbola Idowu</sub>](http://twitter.com/hisabimbola)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=hisabimbola) | [<img src="https://avatars.githubusercontent.com/u/2212006?v=3" width="100px;"/><br /><sub>Gleb Bahmutov</sub>](https://glebbahmutov.com/)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=bahmutov) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=bahmutov) | [<img src="https://avatars.githubusercontent.com/u/332905?v=3" width="100px;"/><br /><sub>Dennis</sub>](http://dennis.io)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=ds82) | [<img src="https://avatars.githubusercontent.com/u/6425649?v=3" width="100px;"/><br /><sub>Matt Lewis</sub>](https://mattlewis.me/)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=mattlewis92) |
| [<img src="https://avatars.githubusercontent.com/u/323761?v=3" width="100px;"/><br /><sub>Tom Vincent</sub>](https://tlvince.com)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=tlvince) | [<img src="https://avatars.githubusercontent.com/u/615381?v=3" width="100px;"/><br /><sub>Anders D. Johnson</sub>](https://andrz.me/)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=adjohnson916) [📖](https://github.com/kentcdodds/validate-commit-msg/commits?author=adjohnson916) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=adjohnson916) | [<img src="https://avatars.githubusercontent.com/u/1643758?v=3" width="100px;"/><br /><sub>James Zetlen</sub>](jameszetlen.com)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=zetlen) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=zetlen) | [<img src="https://avatars.githubusercontent.com/u/235784?v=3" width="100px;"/><br /><sub>Paul Bienkowski</sub>](http://opatut.de)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=opatut) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=opatut) | [<img src="https://avatars.githubusercontent.com/u/324073?v=3" width="100px;"/><br /><sub>Barney Scott</sub>](https://github.com/bmds)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=bmds) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=bmds) | [<img src="https://avatars.githubusercontent.com/u/5572221?v=3" width="100px;"/><br /><sub>Emmanuel Murillo Sánchez</sub>](https://github.com/Emmurillo)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=Emmurillo) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=Emmurillo) | [<img src="https://avatars.githubusercontent.com/u/968267?v=3" width="100px;"/><br /><sub>Hans Kristian Flaatten</sub>](https://starefossen.github.io)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=Starefossen) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=Starefossen) |
| [<img src="https://avatars.githubusercontent.com/u/16605186?v=3" width="100px;"/><br /><sub>Bo Lingen</sub>](https://github.com/citycide)<br />[📖](https://github.com/kentcdodds/validate-commit-msg/commits?author=citycide) | [<img src="https://avatars.githubusercontent.com/u/1057324?v=3" width="100px;"/><br /><sub>Spyros Ioakeimidis</sub>](http://www.spyros.io)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=spirosikmd) [📖](https://github.com/kentcdodds/validate-commit-msg/commits?author=spirosikmd) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=spirosikmd) | [<img src="https://avatars.githubusercontent.com/u/126441?v=3" width="100px;"/><br /><sub>Matt Travi</sub>](https://matt.travi.org)<br />[🐛](https://github.com/kentcdodds/validate-commit-msg/issues?q=author%3Atravi) | [<img src="https://avatars.githubusercontent.com/u/868301?v=3" width="100px;"/><br /><sub>Jonathan Garbee</sub>](http://jonathan.garbee.me)<br /> |
| [<img src="https://avatars.githubusercontent.com/u/16605186?v=3" width="100px;"/><br /><sub>Bo Lingen</sub>](https://github.com/citycide)<br />[📖](https://github.com/kentcdodds/validate-commit-msg/commits?author=citycide) | [<img src="https://avatars.githubusercontent.com/u/1057324?v=3" width="100px;"/><br /><sub>Spyros Ioakeimidis</sub>](http://www.spyros.io)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=spirosikmd) [📖](https://github.com/kentcdodds/validate-commit-msg/commits?author=spirosikmd) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=spirosikmd) | [<img src="https://avatars.githubusercontent.com/u/126441?v=3" width="100px;"/><br /><sub>Matt Travi</sub>](https://matt.travi.org)<br />[🐛](https://github.com/kentcdodds/validate-commit-msg/issues?q=author%3Atravi) | [<img src="https://avatars.githubusercontent.com/u/868301?v=3" width="100px;"/><br /><sub>Jonathan Garbee</sub>](http://jonathan.garbee.me)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=Garbee) [📖](https://github.com/kentcdodds/validate-commit-msg/commits?author=Garbee) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=Garbee) |
<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind welcome!
74 changes: 41 additions & 33 deletions lib/validateMessage.js
Expand Up @@ -63,21 +63,6 @@ exports.validateMessage = function validateMessage(raw) {
var type = match[3];
var scope = match[4];
var subject = match[5];
config.scopes = config.scopes || {};
var validateScopes = config.scopes.validate || false;
var multipleScopesAllowed = config.scopes.multiple || false;
var allowedScopes = config.scopes.allowed || '*';
var scopeRequired = config.scopes.required || false;
var scopes = scope ? scope.split(',') : [];
var validateScope = (item) => {
if (allowedScopes[0] === '*') {
return;
}
if (allowedScopes.indexOf(item) === -1) {
error('"%s" is not an allowed scope ! Valid scopes are: %s', item, allowedScopes.join(', '));
isValid = false;
}
};

var SUBJECT_PATTERN = new RegExp(config.subjectPattern || '.+');
var SUBJECT_PATTERN_ERROR_MSG = config.subjectPatternErrorMsg || 'subject does not match subject pattern!';
Expand All @@ -97,24 +82,7 @@ exports.validateMessage = function validateMessage(raw) {
isValid = false;
}

if (validateScopes) {
if (scopeRequired && scopes.length === 0) {
error('a scope is required !');
isValid = false;
}
if (isValid && multipleScopesAllowed) {
scopes.forEach(validateScope);
}
if (isValid && !multipleScopesAllowed) {
if (scopes.length > 1) {
error('only one scope can be provided !');
isValid = false;
}
if (isValid) {
validateScope(scopes[0]);
}
}
}
isValid = validateScope(isValid, scope);

if (config.autoFix) {
subject = lowercaseFirstLetter(subject);
Expand Down Expand Up @@ -161,3 +129,43 @@ function lowercase(string) {
function lowercaseFirstLetter(string) {
return lowercase(string.charAt(0)) + string.slice(1);
}

function validateScope(isValid, scope) {
config.scope = config.scope || {};
var validateScopes = config.scope.validate || false;
var multipleScopesAllowed = config.scope.multiple || false;
var allowedScopes = config.scope.allowed || '*';
var scopeRequired = config.scope.required || false;
var scopes = scope ? scope.split(',') : [];

function validateIndividualScope(item) {
if (allowedScopes[0].trim() === '*') {
return;
}
if (allowedScopes.indexOf(item) === -1) {
error('"%s" is not an allowed scope ! Valid scope are: %s', item, allowedScopes.join(', '));
isValid = false;
}
}

if (validateScopes) {
if (scopeRequired && scopes.length === 0) {
error('a scope is required !');
isValid = false;
}
if (isValid && multipleScopesAllowed) {
scopes.forEach(validateIndividualScope);
}
if (isValid && !multipleScopesAllowed) {
if (scopes.length > 1) {
error('only one scope can be provided !');
isValid = false;
}
if (isValid) {
validateIndividualScope(scopes[0]);
}
}
}

return isValid;
};
20 changes: 10 additions & 10 deletions test/validateMessage.test.js
Expand Up @@ -160,7 +160,7 @@ describe('validate-commit-msg.js', function() {
it('should require a scope', function() {
var msg = 'feat: Add new feature';

m.config.scopes = {
m.config.scope = {
validate: true,
allowed: '*',
required: true
Expand All @@ -170,28 +170,28 @@ describe('validate-commit-msg.js', function() {
expect(errors[0]).to.equal('INVALID COMMIT MSG: a scope is required !');
expect(logs).to.deep.equal([msg]);

m.config.scopes = undefined;
m.config.scope = undefined;
});

it('should validate scope', function() {
var msg = 'feat(nonexistant): Add new feature';

m.config.scopes = {
m.config.scope = {
validate: true,
allowed: ['button', 'card']
};

expect(m.validateMessage(msg)).to.equal(INVALID);
expect(errors[0]).to.equal('INVALID COMMIT MSG: "nonexistant" is not an allowed scope ! Valid scopes are: ' + m.config.scopes.allowed.join(', '));
expect(errors[0]).to.equal('INVALID COMMIT MSG: "nonexistant" is not an allowed scope ! Valid scope are: ' + m.config.scope.allowed.join(', '));
expect(logs).to.deep.equal([msg]);

m.config.scopes = undefined;
m.config.scope = undefined;
});

it('should only allow a single scope when multiples is off', function() {
var msg = 'feat(button,card): Add new feature';

m.config.scopes = {
m.config.scope = {
validate: true,
allowed: '*'
};
Expand All @@ -200,23 +200,23 @@ describe('validate-commit-msg.js', function() {
expect(errors[0]).to.equal('INVALID COMMIT MSG: only one scope can be provided !');
expect(logs).to.deep.equal([msg]);

m.config.scopes = undefined;
m.config.scope = undefined;
});

it('should catch an invalid scope among many', function() {
var msg = 'feat(button,card,ripple): Add new feature';

m.config.scopes = {
m.config.scope = {
validate: true,
allowed: ['button', 'card'],
multiple: true
};

expect(m.validateMessage(msg)).to.equal(INVALID);
expect(errors[0]).to.equal('INVALID COMMIT MSG: "ripple" is not an allowed scope ! Valid scopes are: ' + m.config.scopes.allowed.join(', '));
expect(errors[0]).to.equal('INVALID COMMIT MSG: "ripple" is not an allowed scope ! Valid scope are: ' + m.config.scope.allowed.join(', '));
expect(logs).to.deep.equal([msg]);

m.config.scopes = undefined;
m.config.scope = undefined;
});

it('should allow empty scope', function() {
Expand Down

0 comments on commit 3d21903

Please sign in to comment.