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,21 +15,42 @@ const dynamicImport = async <T>(id: string): Promise<T> => {
1615 return ( 'default' in imported && imported . default ) || imported ;
1716} ;
1817
18+ const pathSuffixes = [
19+ '' ,
20+ '.js' ,
21+ '.json' ,
22+ `${ path . sep } index.js` ,
23+ `${ path . sep } index.json` ,
24+ ] ;
25+
26+ const specifierSuffixes = [ '' , '.js' , '.json' , '/index.js' , '/index.json' ] ;
27+
1928/**
2029 * @see moduleResolve
2130 */
22- export const resolveFrom = ( specifier : string , parent ?: string ) : string => {
23- let resolved : URL ;
31+ export const resolveFrom = ( lookup : string , parent ?: string ) : string => {
32+ if ( path . isAbsolute ( lookup ) ) {
33+ for ( const suffix of pathSuffixes ) {
34+ const filename = lookup + suffix ;
35+ if ( fs . existsSync ( filename ) ) {
36+ return filename ;
37+ }
38+ }
39+ }
40+
2441 let resolveError : Error | undefined ;
2542
26- for ( const suffix of [ '' , '.js' , '.json' , '/index.js' , '/index.json' ] ) {
27- try {
28- resolved = moduleResolve (
29- specifier + suffix ,
30- pathToFileURL ( parent ? parent : import . meta. url )
31- ) ;
43+ const base = pathToFileURL (
44+ parent
45+ ? fs . statSync ( parent ) . isDirectory ( )
46+ ? path . join ( parent , 'noop.js' )
47+ : parent
48+ : import . meta. url
49+ ) ;
3250
33- return fileURLToPath ( resolved ) ;
51+ for ( const suffix of specifierSuffixes ) {
52+ try {
53+ return fileURLToPath ( moduleResolve ( lookup + suffix , base ) ) ;
3454 } catch ( err ) {
3555 if ( ! resolveError ) {
3656 resolveError = err as Error ;
@@ -90,11 +110,6 @@ export default async function resolveExtends(
90110 ) ;
91111}
92112
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-
98113async function loadExtends (
99114 config : UserConfig = { } ,
100115 context : ResolveExtendsContext = { }
@@ -117,10 +132,7 @@ async function loadExtends(
117132 typeof c === 'object' &&
118133 typeof c . parserPreset === 'string'
119134 ) {
120- const resolvedParserPreset = resolveFrom (
121- c . parserPreset ,
122- path . join ( cwd , FAKE_FILE_NAME_FOR_RESOLVER )
123- ) ;
135+ const resolvedParserPreset = resolveFrom ( c . parserPreset , cwd ) ;
124136
125137 const parserPreset : ParserPreset = {
126138 name : c . parserPreset ,
@@ -207,18 +219,25 @@ function resolveId(
207219 throw Object . assign ( err , { code : 'MODULE_NOT_FOUND' } ) ;
208220}
209221
210- function resolveFromSilent ( specifier : string , parent : string ) : string | void {
222+ export function resolveFromSilent (
223+ specifier : string ,
224+ parent : string
225+ ) : string | void {
211226 try {
212- return resolveFrom (
213- specifier ,
214- path . join ( parent , FAKE_FILE_NAME_FOR_RESOLVER )
215- ) ;
216- } catch ( err ) { }
227+ return resolveFrom ( specifier , parent ) ;
228+ } catch { }
217229}
218230
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 ) { }
231+ /**
232+ * @see https://github.com/sindresorhus/resolve-global/blob/682a6bb0bd8192b74a6294219bb4c536b3708b65/index.js#L7
233+ */
234+ export function resolveGlobalSilent ( specifier : string ) : string | void {
235+ for ( const globalPackages of [
236+ globalDirectory . npm . packages ,
237+ globalDirectory . yarn . packages ,
238+ ] ) {
239+ try {
240+ return resolveFrom ( specifier , globalPackages ) ;
241+ } catch { }
242+ }
224243}
0 commit comments