1+ import fs from 'fs' ;
12import path from 'path' ;
23import { pathToFileURL , fileURLToPath } from 'url' ;
34
4- import 'resolve-global' ;
5-
5+ import globalDirectory from 'global-directory' ;
66import { moduleResolve } from 'import-meta-resolve' ;
77import mergeWith from 'lodash.mergewith' ;
88import { validateConfig } from '@commitlint/config-validator' ;
99import type { ParserPreset , UserConfig } from '@commitlint/types' ;
10- import importFresh from 'import-fresh' ;
1110
1211const dynamicImport = async < T > ( id : string ) : Promise < T > => {
1312 const imported = await import (
@@ -16,20 +15,29 @@ const dynamicImport = async <T>(id: string): Promise<T> => {
1615 return ( 'default' in imported && imported . default ) || imported ;
1716} ;
1817
18+ /**
19+ * Fake file name to provide {@link moduleResolve} a filename to resolve from the configuration cwd
20+ */
21+ const FAKE_FILE_NAME_FOR_RESOLVER = '__' ;
22+
1923/**
2024 * @see moduleResolve
2125 */
2226export const resolveFrom = ( specifier : string , parent ?: string ) : string => {
2327 let resolved : URL ;
2428 let resolveError : Error | undefined ;
2529
30+ const base = pathToFileURL (
31+ parent
32+ ? fs . statSync ( parent ) . isDirectory ( )
33+ ? path . join ( parent , FAKE_FILE_NAME_FOR_RESOLVER )
34+ : parent
35+ : import . meta. url
36+ ) ;
37+
2638 for ( const suffix of [ '' , '.js' , '.json' , '/index.js' , '/index.json' ] ) {
2739 try {
28- resolved = moduleResolve (
29- specifier + suffix ,
30- pathToFileURL ( parent ? parent : import . meta. url )
31- ) ;
32-
40+ resolved = moduleResolve ( specifier + suffix , base ) ;
3341 return fileURLToPath ( resolved ) ;
3442 } catch ( err ) {
3543 if ( ! resolveError ) {
@@ -90,11 +98,6 @@ export default async function resolveExtends(
9098 ) ;
9199}
92100
93- /**
94- * Fake file name to provide {@link moduleResolve} a filename to resolve from the configuration cwd
95- */
96- const FAKE_FILE_NAME_FOR_RESOLVER = '__' ;
97-
98101async function loadExtends (
99102 config : UserConfig = { } ,
100103 context : ResolveExtendsContext = { }
@@ -117,10 +120,7 @@ async function loadExtends(
117120 typeof c === 'object' &&
118121 typeof c . parserPreset === 'string'
119122 ) {
120- const resolvedParserPreset = resolveFrom (
121- c . parserPreset ,
122- path . join ( cwd , FAKE_FILE_NAME_FOR_RESOLVER )
123- ) ;
123+ const resolvedParserPreset = resolveFrom ( c . parserPreset , cwd ) ;
124124
125125 const parserPreset : ParserPreset = {
126126 name : c . parserPreset ,
@@ -207,18 +207,25 @@ function resolveId(
207207 throw Object . assign ( err , { code : 'MODULE_NOT_FOUND' } ) ;
208208}
209209
210- function resolveFromSilent ( specifier : string , parent : string ) : string | void {
210+ export function resolveFromSilent (
211+ specifier : string ,
212+ parent : string
213+ ) : string | void {
211214 try {
212- return resolveFrom (
213- specifier ,
214- path . join ( parent , FAKE_FILE_NAME_FOR_RESOLVER )
215- ) ;
216- } catch ( err ) { }
215+ return resolveFrom ( specifier , parent ) ;
216+ } catch { }
217217}
218218
219- function resolveGlobalSilent ( specifier : string ) : string | void {
220- try {
221- const resolveGlobal = importFresh < ( id : string ) => string > ( 'resolve-global' ) ;
222- return resolveGlobal ( specifier ) ;
223- } catch ( err ) { }
219+ /**
220+ * @see also https://github.com/sindresorhus/resolve-global/blob/682a6bb0bd8192b74a6294219bb4c536b3708b65/index.js#L7
221+ */
222+ export function resolveGlobalSilent ( specifier : string ) : string | void {
223+ for ( const globalPackages of [
224+ globalDirectory . npm . packages ,
225+ globalDirectory . yarn . packages ,
226+ ] ) {
227+ try {
228+ return resolveFrom ( specifier , globalPackages ) ;
229+ } catch { }
230+ }
224231}
0 commit comments