Skip to content

Commit

Permalink
feat: add support for conventionalcommits preset
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdlg committed Aug 9, 2019
1 parent 247504d commit edad14c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,15 @@ With this example:
| `config` | npm package name of a custom [conventional-changelog](https://github.com/conventional-changelog/conventional-changelog) preset. | - |
| `parserOpts` | Additional [conventional-commits-parser](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-commits-parser#conventionalcommitsparseroptions) options that will extends the ones loaded by `preset` or `config`. This is convenient to use a [conventional-changelog](https://github.com/conventional-changelog/conventional-changelog) preset with some customizations without having to create a new module. | - |
| `releaseRules` | An external module, a path to a module or an `Array` of rules. See [`releaseRules`](#releaserules). | See [`releaseRules`](#releaserules) |

| `presetConfig` | Additional configuration passed to the [conventional-changelog](https://github.com/conventional-changelog/conventional-changelog) preset. Used for example with [conventional-changelog-conventionalcommits](https://github.com/conventional-changelog/conventional-changelog-config-spec/blob/master/versions/2.0.0/README.md). | - |
**Notes**: in order to use a `preset` it must be installed (for example to use the [eslint preset](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-changelog-eslint) you must install it with `npm install conventional-changelog-eslint -D`)

**Note**: `config` will be overwritten by the values of `preset`. You should use either `preset` or `config`, but not both.

**Note**: Individual properties of `parserOpts` will override ones loaded with an explicitly set `preset` or `config`. If `preset` or `config` are not set, only the properties set in `parserOpts` will be used.

**Note**: For presets that expects a configuration object, such as [`conventionalcommits`](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-changelog-conventionalcommits), the `presetConfig` option **must** be set.

#### releaseRules

Release rules are used when deciding if the commits since the last release warrant a new release. If you define custom release rules the [default rules](lib/default-release-rules.js) will be used if nothing matched. Those rules will be matched against the commit objects resulting of [conventional-commits-parser](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-commits-parser) parsing.
Expand Down
15 changes: 8 additions & 7 deletions lib/load-parser-config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const {promisify} = require('util');
const {isPlainObject} = require('lodash');
const importFrom = require('import-from');
const conventionalChangelogAngular = require('conventional-changelog-angular');

Expand All @@ -13,7 +14,7 @@ const conventionalChangelogAngular = require('conventional-changelog-angular');
* @param {String} context.cwd The current working directory.
* @return {Promise<Object>} a `Promise` that resolve to the `conventional-changelog-parser` options.
*/
module.exports = async ({preset, config, parserOpts}, {cwd}) => {
module.exports = async ({preset, config, parserOpts, presetConfig}, {cwd}) => {
let loadedConfig;

if (preset) {
Expand All @@ -25,11 +26,11 @@ module.exports = async ({preset, config, parserOpts}, {cwd}) => {
loadedConfig = conventionalChangelogAngular;
}

if (typeof loadedConfig === 'function') {
loadedConfig = await promisify(loadedConfig)();
} else {
loadedConfig = await loadedConfig;
}
loadedConfig = await (typeof loadedConfig === 'function'
? isPlainObject(presetConfig)
? loadedConfig(presetConfig)
: promisify(loadedConfig)()
: loadedConfig);

return !preset && !config && parserOpts ? parserOpts : {...loadedConfig.parserOpts, ...parserOpts};
return {...loadedConfig.parserOpts, ...parserOpts};
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"codecov": "^3.0.0",
"commitizen": "^4.0.0",
"conventional-changelog-atom": "^2.0.0",
"conventional-changelog-conventionalcommits": "^4.1.0",
"conventional-changelog-ember": "^2.0.0",
"conventional-changelog-eslint": "^3.0.0",
"conventional-changelog-express": "^2.0.0",
Expand Down
15 changes: 10 additions & 5 deletions test/load-parser-config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ const cwd = process.cwd();
* @method loadPreset
* @param {Object} t AVA assertion library.
* @param {[type]} preset the `conventional-changelog` preset to test.
* @param {Object} pluginOptions The plugin configuration.
*/
async function loadPreset(t, preset) {
t.truthy((await loadParserConfig({preset}, {cwd})).headerPattern);
async function loadPreset(t, preset, pluginOptions) {
t.truthy((await loadParserConfig({...pluginOptions, preset}, {cwd})).headerPattern);
}

loadPreset.title = (providedTitle, preset) => `${providedTitle} Load "${preset}" preset`.trim();
Expand All @@ -22,9 +23,12 @@ loadPreset.title = (providedTitle, preset) => `${providedTitle} Load "${preset}"
* @method loadPreset
* @param {Object} t AVA assertion library.
* @param {[type]} config the `conventional-changelog` config to test.
* @param {Object} pluginOptions The plugin configuration.
*/
async function loadConfig(t, config) {
t.truthy((await loadParserConfig({config: `conventional-changelog-${config}`}, {cwd})).headerPattern);
async function loadConfig(t, config, pluginOptions) {
t.truthy(
(await loadParserConfig({...pluginOptions, config: `conventional-changelog-${config}`}, {cwd})).headerPattern
);
}

loadConfig.title = (providedTitle, config) => `${providedTitle} Load "${config}" config`.trim();
Expand All @@ -39,7 +43,6 @@ test('Accept a "parserOpts" object as option', async t => {

t.is(customParserOpts.headerPattern, parserOpts.headerPattern);
t.deepEqual(customParserOpts.headerCorrespondence, parserOpts.headerCorrespondence);
t.falsy(parserOpts.noteKeywords);
});

test('Accept a partial "parserOpts" object as option that overlaod a preset', async t => {
Expand Down Expand Up @@ -75,6 +78,8 @@ test(loadPreset, 'express');
test(loadConfig, 'express');
test(loadPreset, 'jshint');
test(loadConfig, 'jshint');
test(loadPreset, 'conventionalcommits', {presetConfig: {}});
test(loadConfig, 'conventionalcommits', {presetConfig: {}});

test('Throw error if "config" doesn`t exist', async t => {
await t.throwsAsync(loadParserConfig({config: 'unknown-config'}, {cwd}), {code: 'MODULE_NOT_FOUND'});
Expand Down

0 comments on commit edad14c

Please sign in to comment.