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

Commit

Permalink
feat(config): config through rc file, has precedence over package.json (
Browse files Browse the repository at this point in the history
  • Loading branch information
spirosikmd authored and Kent C. Dodds committed Jan 22, 2017
1 parent e60ec37 commit d924a34
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 28 deletions.
17 changes: 17 additions & 0 deletions .vcmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"helpMessage": "\nPlease fix your commit message (and consider using http://npm.im/commitizen)\n",
"types": [
"feat",
"fix",
"docs",
"style",
"refactor",
"perf",
"test",
"chore",
"revert",
"custom"
],
"warnOnFail": false,
"autoFix": false
}
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,21 @@ Validates that your commit message follows this format:

### options

You can specify options in `package.json`
You can specify options in `.vcmrc`

```js
{
"types": ["feat", "fix", "docs", "style", "refactor", "perf", "test", "chore", "revert"], // default
"warnOnFail": false, // default
"maxSubjectLength": 100, // default
"subjectPattern": ".+", // default
"subjectPatternErrorMsg": "subject does not match subject pattern!", // default
"helpMessage": "", // default
"autoFix": false // default
}
```

or in `package.json`

```javascript
{
Expand All @@ -47,6 +61,8 @@ You can specify options in `package.json`
}
```

`.vcmrc` has precedence, if it does not exist, then `package.json` will be used.

#### types

These are the types that are allowed for your commit message. If omitted, the value is what is shown above.
Expand Down
34 changes: 34 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';

var findup = require('findup');
var fs = require('fs');
var resolve = require('path').resolve;

function getConfigObject(filename) {
try {
var rcFile = findup.sync(process.cwd(), filename);
return JSON.parse(fs.readFileSync(resolve(rcFile, filename)));
} catch (e) {
return null;
}
}

function getRcConfig() {
return getConfigObject('.vcmrc');
}

function getPackageConfig() {
var configObject = getConfigObject('package.json');
return configObject && configObject.config && configObject.config['validate-commit-msg'];
}

function getConfig() {
return getRcConfig() || getPackageConfig() || {};
}

module.exports = {
getConfig: getConfig,
getRcConfig: getRcConfig,
getPackageConfig: getPackageConfig,
getConfigObject: getConfigObject
};
11 changes: 0 additions & 11 deletions lib/getConfig.js

This file was deleted.

2 changes: 1 addition & 1 deletion lib/validateMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var util = require('util');
var semverRegex = require('semver-regex');
var getConfig = require('./getConfig');
var getConfig = require('./config').getConfig;

var config = getConfig();
var MAX_LENGTH = config.maxSubjectLength || 100;
Expand Down
86 changes: 86 additions & 0 deletions test/config.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
'use strict';

var config = require('../lib/config');
var expect = require('chai').expect;
var sinon = require('sinon');
var fs = require('fs');

function testConfigObject(configObject) {
expect(configObject.helpMessage).to.contain('fix your commit');
}

describe('config', function() {
before(function() {
sinon.stub(fs, 'readFileSync', function() {
return JSON.stringify({
config: {
'validate-commit-msg': {
helpMessage: 'fix your commit'
}
}
});
});
});

describe('getPackageConfig', function() {
it('returns the validate-commit-msg config object if it exists', function() {
var configObject = config.getPackageConfig();
testConfigObject(configObject);
});
});

describe('getRcConfig', function() {
it('returns the config object if it exists', function() {
fs.readFileSync.restore();
sinon.stub(fs, 'readFileSync', function() {
return JSON.stringify({
helpMessage: 'fix your commit'
});
});
var configObject = config.getRcConfig();
testConfigObject(configObject);
});
});

describe('getConfig', function() {
it('returns .vcmrc config by default', function() {
var configObject = config.getConfig();
testConfigObject(configObject);
});

it('spec name', function() {
sinon.stub(config, 'getRcConfig', function() {
throw null;
});
var configObject = config.getConfig();
testConfigObject(configObject);
config.getRcConfig.restore();
});
});

describe('getConfigObject', function() {
it('returns config object given filename', function() {
fs.readFileSync.restore();
sinon.stub(fs, 'readFileSync', function() {
return JSON.stringify({
helpMessage: 'fix your commit'
});
});
var configObject = config.getConfigObject('package.json');
expect(configObject.helpMessage).to.equal('fix your commit');
});

it('returns null on error', function() {
fs.readFileSync.restore();
sinon.stub(fs, 'readFileSync', function() {
throw new Error();
});
var configObject = config.getConfigObject('package.json');
expect(configObject).to.equal(null);
});
});

after(function() {
fs.readFileSync.restore();
});
});
14 changes: 0 additions & 14 deletions test/getConfig.test.js

This file was deleted.

2 changes: 1 addition & 1 deletion wallaby.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = function() {
return {
files: ['lib/**/*.js', 'package.json'],
files: ['lib/**/*.js', 'package.json', '.vcmrc'],
tests: ['test/**/*.js'],
env: {
type: 'node'
Expand Down

0 comments on commit d924a34

Please sign in to comment.