1 | const {promisify} = require('util');
|
2 | const {isPlainObject} = require('lodash');
|
3 | const importFrom = require('import-from');
|
4 | const conventionalChangelogAngular = require('conventional-changelog-angular');
|
5 |
|
6 | /**
|
7 | * Load `conventional-changelog-parser` options. Handle presets that return either a `Promise<Array>` or a `Promise<Function>`.
|
8 | *
|
9 | * @param {Object} pluginConfig The plugin configuration.
|
10 | * @param {Object} pluginConfig.preset conventional-changelog preset ('angular', 'atom', 'codemirror', 'ember', 'eslint', 'express', 'jquery', 'jscs', 'jshint')
|
11 | * @param {string} pluginConfig.config Requierable npm package with a custom conventional-changelog preset
|
12 | * @param {Object} pluginConfig.parserOpts Additionnal `conventional-changelog-parser` options that will overwrite ones loaded by `preset` or `config`.
|
13 | * @param {Object} pluginConfig.writerOpts Additionnal `conventional-changelog-writer` options that will overwrite ones loaded by `preset` or `config`.
|
14 | * @param {Object} context The semantic-release context.
|
15 | * @param {Array<Object>} context.commits The commits to analyze.
|
16 | * @param {String} context.cwd The current working directory.
|
17 | *
|
18 | * @return {Promise<Object>} a `Promise` that resolve to the `conventional-changelog-core` config.
|
19 | */
|
20 | module.exports = async ({preset, config, parserOpts, writerOpts, presetConfig}, {cwd}) => {
|
21 | let loadedConfig;
|
22 |
|
23 | if (preset) {
|
24 | const presetPackage = `conventional-changelog-${preset.toLowerCase()}`;
|
25 | loadedConfig = importFrom.silent(__dirname, presetPackage) || importFrom(cwd, presetPackage);
|
26 | } else if (config) {
|
27 | loadedConfig = importFrom.silent(__dirname, config) || importFrom(cwd, config);
|
28 | } else {
|
29 | loadedConfig = conventionalChangelogAngular;
|
30 | }
|
31 |
|
32 | loadedConfig = await (typeof loadedConfig === 'function'
|
33 | ? isPlainObject(presetConfig)
|
34 | ? loadedConfig(presetConfig)
|
35 | : promisify(loadedConfig)()
|
36 | : loadedConfig);
|
37 |
|
38 | return {
|
39 | parserOpts: {...loadedConfig.parserOpts, ...parserOpts},
|
40 | writerOpts: {...loadedConfig.writerOpts, ...writerOpts},
|
41 | };
|
42 | };
|