Skip to content

Commit

Permalink
refactor: restore importFrom strategy using ESM port
Browse files Browse the repository at this point in the history
  • Loading branch information
sheerlox committed Oct 11, 2023
1 parent 94c9fb1 commit c9a8044
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 29 deletions.
11 changes: 8 additions & 3 deletions lib/load-parser-config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { dirname } from "node:path";
import { fileURLToPath } from "node:url";
import importFrom from "import-from-esm";
import conventionalChangelogAngular from "conventional-changelog-angular";
import { importModule } from "./module-loader.js";

/**
* Load `conventional-changelog-parser` options. Handle presets that return either a `Promise<Array>` or a `Promise<Function>`.
Expand All @@ -15,12 +17,15 @@ import { importModule } from "./module-loader.js";
*/
export default async ({ preset, config, parserOpts, presetConfig }, { cwd }) => {
let loadedConfig;
const __dirname = dirname(fileURLToPath(import.meta.url));

if (preset) {
const presetPackage = `conventional-changelog-${preset.toLowerCase()}`;
loadedConfig = await (await importModule(cwd, presetPackage))(presetConfig);
loadedConfig = await (
(await importFrom.silent(__dirname, presetPackage)) || (await importFrom(cwd, presetPackage))
)(presetConfig);
} else if (config) {
loadedConfig = await (await importModule(cwd, config))();
loadedConfig = await ((await importFrom.silent(__dirname, config)) || (await importFrom(cwd, config)))();
} else {
loadedConfig = await conventionalChangelogAngular();
}
Expand Down
10 changes: 8 additions & 2 deletions lib/load-release-rules.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { dirname } from "node:path";
import { fileURLToPath } from "node:url";
import { isUndefined } from "lodash-es";
import importFrom from "import-from-esm";
import RELEASE_TYPES from "./default-release-types.js";
import { importModule } from "./module-loader.js";

/**
* Load and validate the `releaseRules` rules.
Expand All @@ -17,9 +19,13 @@ import { importModule } from "./module-loader.js";
*/
export default async ({ releaseRules }, { cwd }) => {
let loadedReleaseRules;
const __dirname = dirname(fileURLToPath(import.meta.url));

if (releaseRules) {
loadedReleaseRules = typeof releaseRules === "string" ? await importModule(cwd, releaseRules) : releaseRules;
loadedReleaseRules =
typeof releaseRules === "string"
? (await importFrom.silent(__dirname, releaseRules)) || (await importFrom(cwd, releaseRules))
: releaseRules;

if (!Array.isArray(loadedReleaseRules)) {
throw new TypeError('Error in commit-analyzer configuration: "releaseRules" must be an array of rules');
Expand Down
24 changes: 0 additions & 24 deletions lib/module-loader.js

This file was deleted.

9 changes: 9 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"conventional-commits-filter": "^4.0.0",
"conventional-commits-parser": "^5.0.0",
"debug": "^4.0.0",
"import-from-esm": "^1.0.0",
"lodash-es": "^4.17.21",
"micromatch": "^4.0.2"
},
Expand Down

0 comments on commit c9a8044

Please sign in to comment.