diff --git a/.vcmrc b/.vcmrc new file mode 100644 index 0000000..21f1456 --- /dev/null +++ b/.vcmrc @@ -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 +} diff --git a/README.md b/README.md index f445ecc..ca0688f 100644 --- a/README.md +++ b/README.md @@ -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 { @@ -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. diff --git a/lib/config.js b/lib/config.js new file mode 100644 index 0000000..5361804 --- /dev/null +++ b/lib/config.js @@ -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 +}; diff --git a/lib/getConfig.js b/lib/getConfig.js deleted file mode 100644 index 709ee16..0000000 --- a/lib/getConfig.js +++ /dev/null @@ -1,11 +0,0 @@ -'use strict'; - -var findup = require('findup'); -var fs = require('fs'); -var resolve = require('path').resolve; - -module.exports = function getConfig() { - var pkgFile = findup.sync(process.cwd(), 'package.json'); - var pkg = JSON.parse(fs.readFileSync(resolve(pkgFile, 'package.json'))); - return pkg && pkg.config && pkg.config['validate-commit-msg'] || {}; -}; diff --git a/lib/validateMessage.js b/lib/validateMessage.js index 49e5e2d..ae28f5c 100644 --- a/lib/validateMessage.js +++ b/lib/validateMessage.js @@ -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; diff --git a/test/config.test.js b/test/config.test.js new file mode 100644 index 0000000..7be8e99 --- /dev/null +++ b/test/config.test.js @@ -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(); + }); +}); diff --git a/test/getConfig.test.js b/test/getConfig.test.js deleted file mode 100644 index 5720071..0000000 --- a/test/getConfig.test.js +++ /dev/null @@ -1,14 +0,0 @@ -'use strict'; - -var getConfig = require('../lib/getConfig'); -var expect = require('chai').expect; - -describe('getConfig', function() { - it('returns the validate-commit-msg config object if exists', function() { - var config = getConfig(); - expect(config.helpMessage).to.contain('fix your commit message'); - expect(config.types.length).to.equal(10); - expect(config.warnOnFail).to.equal(false); - expect(config.autoFix).to.equal(false); - }); -}); diff --git a/wallaby.config.js b/wallaby.config.js index d6492a8..ffe2208 100644 --- a/wallaby.config.js +++ b/wallaby.config.js @@ -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'