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,20 +15,29 @@ const dynamicImport = async <T>(id: string): Promise<T> => {
16
15
return ( 'default' in imported && imported . default ) || imported ;
17
16
} ;
18
17
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
+
19
23
/**
20
24
* @see moduleResolve
21
25
*/
22
26
export const resolveFrom = ( specifier : string , parent ?: string ) : string => {
23
27
let resolved : URL ;
24
28
let resolveError : Error | undefined ;
25
29
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
+
26
38
for ( const suffix of [ '' , '.js' , '.json' , '/index.js' , '/index.json' ] ) {
27
39
try {
28
- resolved = moduleResolve (
29
- specifier + suffix ,
30
- pathToFileURL ( parent ? parent : import . meta. url )
31
- ) ;
32
-
40
+ resolved = moduleResolve ( specifier + suffix , base ) ;
33
41
return fileURLToPath ( resolved ) ;
34
42
} catch ( err ) {
35
43
if ( ! resolveError ) {
@@ -90,11 +98,6 @@ export default async function resolveExtends(
90
98
) ;
91
99
}
92
100
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
101
async function loadExtends (
99
102
config : UserConfig = { } ,
100
103
context : ResolveExtendsContext = { }
@@ -117,10 +120,7 @@ async function loadExtends(
117
120
typeof c === 'object' &&
118
121
typeof c . parserPreset === 'string'
119
122
) {
120
- const resolvedParserPreset = resolveFrom (
121
- c . parserPreset ,
122
- path . join ( cwd , FAKE_FILE_NAME_FOR_RESOLVER )
123
- ) ;
123
+ const resolvedParserPreset = resolveFrom ( c . parserPreset , cwd ) ;
124
124
125
125
const parserPreset : ParserPreset = {
126
126
name : c . parserPreset ,
@@ -207,18 +207,25 @@ function resolveId(
207
207
throw Object . assign ( err , { code : 'MODULE_NOT_FOUND' } ) ;
208
208
}
209
209
210
- function resolveFromSilent ( specifier : string , parent : string ) : string | void {
210
+ export function resolveFromSilent (
211
+ specifier : string ,
212
+ parent : string
213
+ ) : string | void {
211
214
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 { }
217
217
}
218
218
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
+ }
224
231
}
0 commit comments