Skip to content

Commit

Permalink
refactor: use resolve-from package
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Feb 28, 2024
1 parent 1cfe15c commit eff775e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
3 changes: 2 additions & 1 deletion @commitlint/resolve-extends/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
"@commitlint/types": "^19.0.0",
"global-directory": "^4.0.1",
"import-meta-resolve": "^4.0.0",
"lodash.mergewith": "^4.6.2"
"lodash.mergewith": "^4.6.2",
"resolve-from": "^5.0.0"
},
"gitHead": "d829bf6260304ca8d6811f329fcdd1b6c50e9749"
}
18 changes: 7 additions & 11 deletions @commitlint/resolve-extends/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import fs from 'fs';
import {createRequire} from 'module';
import path from 'path';
import {pathToFileURL, fileURLToPath} from 'url';

import globalDirectory from 'global-directory';
import {moduleResolve} from 'import-meta-resolve';
import mergeWith from 'lodash.mergewith';
import resolveFrom_ from 'resolve-from';
import {validateConfig} from '@commitlint/config-validator';
import type {ParserPreset, UserConfig} from '@commitlint/types';

const require = createRequire(import.meta.url);

const dynamicImport = async <T>(id: string): Promise<T> => {
const imported = await import(
path.isAbsolute(id) ? pathToFileURL(id).toString() : id
Expand Down Expand Up @@ -41,14 +39,14 @@ export const resolveFrom = (lookup: string, parent?: string): string => {
}
}

const parentDir =
parent &&
(fs.statSync(parent).isDirectory() ? parent : path.dirname(parent));

let resolveError: Error | undefined;

const base = pathToFileURL(
parentDir ? path.join(parentDir, 'noop.js') : import.meta.url
parent
? fs.statSync(parent).isDirectory()
? path.join(parent, 'noop.js')
: parent
: import.meta.url
);

for (const suffix of specifierSuffixes) {
Expand All @@ -66,9 +64,7 @@ export const resolveFrom = (lookup: string, parent?: string): string => {
* Yarn P'n'P does not support pure ESM well, this is only a workaround for
* @see https://github.com/conventional-changelog/commitlint/issues/3936
*/
return require.resolve(lookup, {
paths: parentDir ? [parentDir] : undefined,
});
return resolveFrom_(path.dirname(fileURLToPath(base)), lookup);
} catch {
throw resolveError;
}
Expand Down

0 comments on commit eff775e

Please sign in to comment.