Skip to content

Commit

Permalink
fix(conventional-commits): support conventional-changelog-conventiona…
Browse files Browse the repository at this point in the history
…lcommits (lerna#2138)
  • Loading branch information
MunifTanjim committed Apr 17, 2020
1 parent 6c4ee52 commit 1ea7403
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
19 changes: 19 additions & 0 deletions commands/version/README.md
Expand Up @@ -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
Expand Down
32 changes: 19 additions & 13 deletions core/conventional-commits/lib/get-changelog-config.js
Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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})` : ""
}`
);
}
Expand Down

0 comments on commit 1ea7403

Please sign in to comment.