1
+ import fs from 'fs' ;
1
2
import path from 'path' ;
2
3
import { pathToFileURL , fileURLToPath } from 'url' ;
3
4
4
- import 'resolve-global' ;
5
-
5
+ import globalDirectory from 'global-directory' ;
6
6
import { moduleResolve } from 'import-meta-resolve' ;
7
7
import mergeWith from 'lodash.mergewith' ;
8
8
import { validateConfig } from '@commitlint/config-validator' ;
9
9
import type { ParserPreset , UserConfig } from '@commitlint/types' ;
10
- import importFresh from 'import-fresh' ;
11
10
12
11
const dynamicImport = async < T > ( id : string ) : Promise < T > => {
13
12
const imported = await import (
@@ -16,21 +15,42 @@ const dynamicImport = async <T>(id: string): Promise<T> => {
16
15
return ( 'default' in imported && imported . default ) || imported ;
17
16
} ;
18
17
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
+
19
28
/**
20
29
* @see moduleResolve
21
30
*/
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
+
24
41
let resolveError : Error | undefined ;
25
42
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
+ ) ;
32
50
33
- return fileURLToPath ( resolved ) ;
51
+ for ( const suffix of specifierSuffixes ) {
52
+ try {
53
+ return fileURLToPath ( moduleResolve ( lookup + suffix , base ) ) ;
34
54
} catch ( err ) {
35
55
if ( ! resolveError ) {
36
56
resolveError = err as Error ;
@@ -90,11 +110,6 @@ export default async function resolveExtends(
90
110
) ;
91
111
}
92
112
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
-
98
113
async function loadExtends (
99
114
config : UserConfig = { } ,
100
115
context : ResolveExtendsContext = { }
@@ -117,10 +132,7 @@ async function loadExtends(
117
132
typeof c === 'object' &&
118
133
typeof c . parserPreset === 'string'
119
134
) {
120
- const resolvedParserPreset = resolveFrom (
121
- c . parserPreset ,
122
- path . join ( cwd , FAKE_FILE_NAME_FOR_RESOLVER )
123
- ) ;
135
+ const resolvedParserPreset = resolveFrom ( c . parserPreset , cwd ) ;
124
136
125
137
const parserPreset : ParserPreset = {
126
138
name : c . parserPreset ,
@@ -207,18 +219,25 @@ function resolveId(
207
219
throw Object . assign ( err , { code : 'MODULE_NOT_FOUND' } ) ;
208
220
}
209
221
210
- function resolveFromSilent ( specifier : string , parent : string ) : string | void {
222
+ export function resolveFromSilent (
223
+ specifier : string ,
224
+ parent : string
225
+ ) : string | void {
211
226
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 { }
217
229
}
218
230
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
+ }
224
243
}
0 commit comments