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..f70d002e265 100644 --- a/core/conventional-commits/lib/get-changelog-config.js +++ b/core/conventional-commits/lib/get-changelog-config.js @@ -1,39 +1,32 @@ "use strict"; +const conventionalChangelogPresetLoader = require('conventional-changelog-preset-loader') const log = require("npmlog"); const npa = require("npm-package-arg"); -const pify = require("pify"); const ValidationError = require("@lerna/validation-error"); module.exports = getChangelogConfig; const cfgCache = new Map(); -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); + let config = conventionalChangelogPresetLoader(presetConfig || 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)(); - } - return config; } function getChangelogConfig(changelogPreset = "conventional-changelog-angular", rootPath) { - let config = cfgCache.get(changelogPreset); + let presetName = typeof changelogPreset === 'string' ? changelogPreset : changelogPreset.name; + let 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 +50,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 +78,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})` : "" }` ); } diff --git a/core/conventional-commits/package.json b/core/conventional-commits/package.json index 080333079d5..8a01789de0d 100644 --- a/core/conventional-commits/package.json +++ b/core/conventional-commits/package.json @@ -35,13 +35,13 @@ "@lerna/validation-error": "file:../validation-error", "conventional-changelog-angular": "^5.0.3", "conventional-changelog-core": "^3.1.6", + "conventional-changelog-preset-loader": "^2.3.0", "conventional-recommended-bump": "^5.0.0", "fs-extra": "^8.1.0", "get-stream": "^4.0.0", "lodash.template": "^4.5.0", "npm-package-arg": "^6.1.0", "npmlog": "^4.1.2", - "pify": "^4.0.1", "semver": "^6.2.0" } }