From 6c82fefc1522db8fa1923612daaa30d96d0d5087 Mon Sep 17 00:00:00 2001 From: Pierre Cavin Date: Thu, 12 Oct 2023 01:07:21 +0200 Subject: [PATCH] refactor: restore importFrom strategy using ESM port --- lib/load-parser-config.js | 11 ++++++++--- lib/load-release-rules.js | 10 ++++++++-- lib/module-loader.js | 24 ------------------------ package-lock.json | 9 +++++++++ package.json | 1 + 5 files changed, 26 insertions(+), 29 deletions(-) delete mode 100644 lib/module-loader.js diff --git a/lib/load-parser-config.js b/lib/load-parser-config.js index bfea8051..fecbec0f 100644 --- a/lib/load-parser-config.js +++ b/lib/load-parser-config.js @@ -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` or a `Promise`. @@ -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(); } diff --git a/lib/load-release-rules.js b/lib/load-release-rules.js index 312f0a72..dcd3e601 100644 --- a/lib/load-release-rules.js +++ b/lib/load-release-rules.js @@ -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. @@ -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'); diff --git a/lib/module-loader.js b/lib/module-loader.js deleted file mode 100644 index 494e77bf..00000000 --- a/lib/module-loader.js +++ /dev/null @@ -1,24 +0,0 @@ -import { join } from "node:path"; - -/** - * Import a module from node_modules or current working directory. - * - * @param {string} cwd the current working directory. - * @param {string} moduleName npm package name or path relative to cwd. - * - * @return {Promise} the loaded module's default export. - */ -export const importModule = async (cwd, moduleName) => { - const localModulePath = join(cwd, moduleName); - try { - return (await import(moduleName)).default; - } catch (e) { - try { - return (await import(localModulePath)).default; - } catch (e) { - const error = new Error(`Cannot find module "${moduleName}" or "${localModulePath}".`); - error.code = "MODULE_NOT_FOUND"; - throw error; - } - } -}; diff --git a/package-lock.json b/package-lock.json index 7859db8c..f0afc177 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,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" }, @@ -2221,6 +2222,14 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/import-from-esm": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/import-from-esm/-/import-from-esm-1.0.0.tgz", + "integrity": "sha512-9Rg+eQ3qC2sonLssXHTjmVmN3LBkh1PvWe7H3rCAFjR4vW/2mfiLQIwhwO9vvQvJUhM8DymRt0AneYFI6Fty2g==", + "engines": { + "node": ">=16.20" + } + }, "node_modules/imurmurhash": { "version": "0.1.4", "resolved": "https://registry.npmjs.org/imurmurhash/-/imurmurhash-0.1.4.tgz", diff --git a/package.json b/package.json index 8825b22b..0a5fb790 100644 --- a/package.json +++ b/package.json @@ -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" },