Skip to content

Commit

Permalink
feat: add support for ESM presets (#544)
Browse files Browse the repository at this point in the history
Co-authored-by: Matt Travi <[email protected]>
  • Loading branch information
sheerlox and travi authored Nov 6, 2023
1 parent e6781fa commit 53c18ce
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 7 deletions.
10 changes: 5 additions & 5 deletions lib/load-changelog-config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { dirname } from "node:path";
import { fileURLToPath } from "node:url";
import { promisify } from "node:util";
import { isPlainObject } from "lodash-es";
import importFrom from "import-from";
import importFrom from "import-from-esm";
import conventionalChangelogAngular from "conventional-changelog-angular";

/**
Expand All @@ -25,9 +23,11 @@ export default async ({ preset, config, parserOpts, writerOpts, presetConfig },

if (preset) {
const presetPackage = `conventional-changelog-${preset.toLowerCase()}`;
loadedConfig = await (importFrom.silent(__dirname, presetPackage) || importFrom(cwd, presetPackage))(presetConfig);
loadedConfig = await (
(await importFrom.silent(__dirname, presetPackage)) || (await importFrom(cwd, presetPackage))
)(presetConfig);
} else if (config) {
loadedConfig = await (importFrom.silent(__dirname, config) || importFrom(cwd, config))();
loadedConfig = await ((await importFrom.silent(__dirname, config)) || (await importFrom(cwd, config)))();
} else {
loadedConfig = await conventionalChangelogAngular();
}
Expand Down
141 changes: 140 additions & 1 deletion package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"conventional-commits-parser": "^5.0.0",
"debug": "^4.0.0",
"get-stream": "^7.0.0",
"import-from": "^4.0.0",
"import-from-esm": "^1.0.3",
"into-stream": "^7.0.0",
"lodash-es": "^4.17.21",
"read-pkg-up": "^11.0.0"
Expand All @@ -35,6 +35,7 @@
"fs-extra": "11.1.1",
"prettier": "3.0.3",
"semantic-release": "22.0.7",
"sinon": "16.1.0",
"stream-buffers": "3.0.2",
"tempy": "3.1.0",
"testdouble": "3.20.0"
Expand Down
12 changes: 12 additions & 0 deletions test/load-changelog-config.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import test from "ava";
import importFrom from "import-from-esm";
import sinon from "sinon";

import conventionalChangelogAngular from "conventional-changelog-angular";
import loadChangelogConfig from "../lib/load-changelog-config.js";

Expand Down Expand Up @@ -173,3 +176,12 @@ test('Throw error if "config" doesn`t exist', async (t) => {
test('Throw error if "preset" doesn`t exist', async (t) => {
await t.throwsAsync(loadChangelogConfig({ preset: "unknown-preset" }, { cwd }), { code: "MODULE_NOT_FOUND" });
});

test.serial("Load preset and config correctly when importFrom.silent fails", async (t) => {
sinon.stub(importFrom, "silent").returns(undefined);

await loadPreset(t, "angular");
await loadConfig(t, "angular");

sinon.restore();
});

0 comments on commit 53c18ce

Please sign in to comment.