Skip to content

Commit 2b5ac0a

Browse files
committed
feat: add support for conventionalcommits preset
1 parent 79f3ed3 commit 2b5ac0a

File tree

4 files changed

+22
-13
lines changed

4 files changed

+22
-13
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,15 @@ With this example:
6161
| `config` | npm package name of a custom [conventional-changelog](https://github.com/conventional-changelog/conventional-changelog) preset. | - |
6262
| `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. | - |
6363
| `releaseRules` | An external module, a path to a module or an `Array` of rules. See [`releaseRules`](#releaserules). | See [`releaseRules`](#releaserules) |
64-
64+
| `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). | - |
6565
**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`)
6666

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

6969
**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.
7070

71+
**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.
72+
7173
#### releaseRules
7274

7375
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. Each rule property can be defined as a [glob](https://github.com/micromatch/micromatch#matching-features).

lib/load-parser-config.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const {promisify} = require('util');
2+
const {isPlainObject} = require('lodash');
23
const importFrom = require('import-from');
34
const conventionalChangelogAngular = require('conventional-changelog-angular');
45

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

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

28-
if (typeof loadedConfig === 'function') {
29-
loadedConfig = await promisify(loadedConfig)();
30-
} else {
31-
loadedConfig = await loadedConfig;
32-
}
29+
loadedConfig = await (typeof loadedConfig === 'function'
30+
? isPlainObject(presetConfig)
31+
? loadedConfig(presetConfig)
32+
: promisify(loadedConfig)()
33+
: loadedConfig);
3334

34-
return !preset && !config && parserOpts ? parserOpts : {...loadedConfig.parserOpts, ...parserOpts};
35+
return {...loadedConfig.parserOpts, ...parserOpts};
3536
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"codecov": "^3.0.0",
3838
"commitizen": "^4.0.0",
3939
"conventional-changelog-atom": "^2.0.0",
40+
"conventional-changelog-conventionalcommits": "^4.1.0",
4041
"conventional-changelog-ember": "^2.0.0",
4142
"conventional-changelog-eslint": "^3.0.0",
4243
"conventional-changelog-express": "^2.0.0",

test/load-parser-config.test.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ const cwd = process.cwd();
99
* @method loadPreset
1010
* @param {Object} t AVA assertion library.
1111
* @param {[type]} preset the `conventional-changelog` preset to test.
12+
* @param {Object} pluginOptions The plugin configuration.
1213
*/
13-
async function loadPreset(t, preset) {
14-
t.truthy((await loadParserConfig({preset}, {cwd})).headerPattern);
14+
async function loadPreset(t, preset, pluginOptions) {
15+
t.truthy((await loadParserConfig({...pluginOptions, preset}, {cwd})).headerPattern);
1516
}
1617

1718
loadPreset.title = (providedTitle, preset) => `${providedTitle} Load "${preset}" preset`.trim();
@@ -22,9 +23,12 @@ loadPreset.title = (providedTitle, preset) => `${providedTitle} Load "${preset}"
2223
* @method loadPreset
2324
* @param {Object} t AVA assertion library.
2425
* @param {[type]} config the `conventional-changelog` config to test.
26+
* @param {Object} pluginOptions The plugin configuration.
2527
*/
26-
async function loadConfig(t, config) {
27-
t.truthy((await loadParserConfig({config: `conventional-changelog-${config}`}, {cwd})).headerPattern);
28+
async function loadConfig(t, config, pluginOptions) {
29+
t.truthy(
30+
(await loadParserConfig({...pluginOptions, config: `conventional-changelog-${config}`}, {cwd})).headerPattern
31+
);
2832
}
2933

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

4044
t.is(customParserOpts.headerPattern, parserOpts.headerPattern);
4145
t.deepEqual(customParserOpts.headerCorrespondence, parserOpts.headerCorrespondence);
42-
t.falsy(parserOpts.noteKeywords);
4346
});
4447

4548
test('Accept a partial "parserOpts" object as option that overlaod a preset', async t => {
@@ -75,6 +78,8 @@ test(loadPreset, 'express');
7578
test(loadConfig, 'express');
7679
test(loadPreset, 'jshint');
7780
test(loadConfig, 'jshint');
81+
test(loadPreset, 'conventionalcommits', {presetConfig: {}});
82+
test(loadConfig, 'conventionalcommits', {presetConfig: {}});
7883

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

0 commit comments

Comments
 (0)