diff --git a/commands/version/README.md b/commands/version/README.md index 5cdaa3bc3f0..1b4ee79c717 100644 --- a/commands/version/README.md +++ b/commands/version/README.md @@ -166,6 +166,25 @@ Presets are names of built-in or installable configuration for conventional chan Presets may be passed as the full name of the package, or the auto-expanded suffix (e.g., `angular` is expanded to `conventional-changelog-angular`). +This option is can also be specified in `lerna.json` configuration: + +```json +{ + "changelogPreset": "angular" +} +``` + +If the preset exports a builder function (e.g. `conventional-changelog-conventionalcommits`), you can specify the [preset configuration](https://github.com/conventional-changelog/conventional-changelog-config-spec) too: + +```json +{ + "changelogPreset": { + "name": "conventionalcommits", + "issueUrlFormat": "{{host}}/{{owner}}/{{repository}}/issues/{{id}}" + } +} +``` + ### `--exact` ```sh diff --git a/core/conventional-commits/lib/get-changelog-config.js b/core/conventional-commits/lib/get-changelog-config.js index 23212790eb8..16eea0c8073 100644 --- a/core/conventional-commits/lib/get-changelog-config.js +++ b/core/conventional-commits/lib/get-changelog-config.js @@ -13,27 +13,33 @@ function isFunction(config) { return Object.prototype.toString.call(config) === "[object Function]"; } -function resolveConfigPromise(presetPackageName) { +function resolveConfigPromise(presetPackageName, presetConfig) { log.verbose("getChangelogConfig", "Attempting to resolve preset %j", presetPackageName); // eslint-disable-next-line global-require, import/no-dynamic-require - let config = require(presetPackageName); + const config = require(presetPackageName); log.info("getChangelogConfig", "Successfully resolved preset %j", presetPackageName); - // legacy presets export an errback function instead of Q.all() if (isFunction(config)) { - config = pify(config)(); + // try assuming config builder function first + return pify(config)(presetConfig).catch(() => { + // legacy presets export an errback function instead of Q.all() + return pify(config)(); + }); } return config; } function getChangelogConfig(changelogPreset = "conventional-changelog-angular", rootPath) { - let config = cfgCache.get(changelogPreset); + const presetName = typeof changelogPreset === "string" ? changelogPreset : changelogPreset.name; + const presetConfig = typeof changelogPreset === "object" ? changelogPreset : null; + + let config = cfgCache.get(presetName); if (!config) { - let presetPackageName = changelogPreset; + let presetPackageName = presetName; // https://github.com/npm/npm-package-arg#result-object const parsed = npa(presetPackageName, rootPath); @@ -57,15 +63,15 @@ function getChangelogConfig(changelogPreset = "conventional-changelog-angular", // Maybe it doesn't need an implicit 'conventional-changelog-' prefix? try { - config = resolveConfigPromise(presetPackageName); + config = resolveConfigPromise(presetPackageName, presetConfig); - cfgCache.set(changelogPreset, config); + cfgCache.set(presetName, config); // early exit, yay return Promise.resolve(config); } catch (err) { log.verbose("getChangelogConfig", err.message); - log.info("getChangelogConfig", "Auto-prefixing conventional-changelog preset %j", changelogPreset); + log.info("getChangelogConfig", "Auto-prefixing conventional-changelog preset %j", presetName); // probably a deep shorthand subpath :P parsed.name = parsed.raw; @@ -85,16 +91,16 @@ function getChangelogConfig(changelogPreset = "conventional-changelog-angular", } try { - config = resolveConfigPromise(presetPackageName); + config = resolveConfigPromise(presetPackageName, presetConfig); - cfgCache.set(changelogPreset, config); + cfgCache.set(presetName, config); } catch (err) { log.warn("getChangelogConfig", err.message); throw new ValidationError( "EPRESET", - `Unable to load conventional-changelog preset '${changelogPreset}'${ - changelogPreset !== presetPackageName ? ` (${presetPackageName})` : "" + `Unable to load conventional-changelog preset '${presetName}'${ + presetName !== presetPackageName ? ` (${presetPackageName})` : "" }` ); }