Skip to content

Commit

Permalink
fix(conventional-commits): Support legacy callback presets
Browse files Browse the repository at this point in the history
Fixes #1896
  • Loading branch information
evocateur committed Jan 31, 2019
1 parent c0a750e commit 60647b4
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 4 deletions.
@@ -0,0 +1,17 @@
"use strict";

// https://git.io/vx5iq (conventional-changelog-angular/conventional-recommended-bump.js, etc)
const parserOpts = require("./parser-opts");
const writerOpts = require("./writer-opts");
const whatBump = require("./what-bump");

// https://git.io/fhyKK
module.exports = presetOpts;

function presetOpts(cb) {
process.nextTick(cb, null, {
parserOpts,
writerOpts,
whatBump,
});
}
22 changes: 22 additions & 0 deletions core/conventional-commits/__tests__/conventional-commits.test.js
Expand Up @@ -331,6 +331,28 @@ describe("conventional-commits", () => {
expect(leafChangelogContent).toMatchSnapshot();
});

it("supports legacy callback presets", async () => {
const cwd = await initFixture("fixed");

await gitTag(cwd, "v1.0.0");

const [, pkg2] = await getPackages(cwd);

// make a change in package-2
await pkg2.set("changed", 1).serialize();
await gitAdd(cwd, pkg2.manifestLocation);
await gitCommit(cwd, "fix(pkg2): A commit using a legacy callback preset");

// update version
await pkg2.set("version", "1.0.1").serialize();

const leafChangelogContent = await updateChangelog(pkg2, "fixed", {
changelogPreset: "./scripts/legacy-callback-preset",
}).then(getFileContent);

expect(leafChangelogContent).toContain("fix(pkg2): A commit using a legacy callback preset");
});

it("updates independent changelogs", async () => {
const cwd = await initFixture("independent");

Expand Down
23 changes: 19 additions & 4 deletions core/conventional-commits/lib/get-changelog-config.js
Expand Up @@ -2,12 +2,29 @@

const log = require("libnpm/log");
const npa = require("libnpm/parse-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) {
// eslint-disable-next-line global-require, import/no-dynamic-require
let config = require(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);

Expand All @@ -33,8 +50,7 @@ function getChangelogConfig(changelogPreset = "conventional-changelog-angular",

// Maybe it doesn't need an implicit 'conventional-changelog-' prefix?
try {
// eslint-disable-next-line global-require, import/no-dynamic-require
config = require(presetPackageName);
config = resolveConfigPromise(presetPackageName);

cfgCache.set(changelogPreset, config);

Expand Down Expand Up @@ -62,8 +78,7 @@ function getChangelogConfig(changelogPreset = "conventional-changelog-angular",
}

try {
// eslint-disable-next-line global-require, import/no-dynamic-require
config = require(presetPackageName);
config = resolveConfigPromise(presetPackageName);

cfgCache.set(changelogPreset, config);
} catch (err) {
Expand Down
1 change: 1 addition & 0 deletions core/conventional-commits/package.json
Expand Up @@ -38,6 +38,7 @@
"fs-extra": "^7.0.0",
"get-stream": "^4.0.0",
"libnpm": "^2.0.1",
"pify": "^3.0.0",
"semver": "^5.5.0"
}
}
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 60647b4

Please sign in to comment.