1
1
import fs from 'fs/promises' ;
2
2
import path from 'path' ;
3
3
4
+ const findModulePath = ( moduleName : string ) => {
5
+ const searchPaths = [
6
+ path . join ( 'node_modules' , moduleName ) ,
7
+ path . join ( 'node_modules' , '.pnpm' )
8
+ ] ;
9
+
10
+ for ( const basePath of searchPaths ) {
11
+ try {
12
+ const resolvedPath = require . resolve ( moduleName , { paths : [ basePath ] } ) ;
13
+ return resolvedPath ;
14
+ } catch {
15
+ // Continue to the next search path if the module is not found
16
+ }
17
+ }
18
+
19
+ throw new Error ( `Cannot find module ${ moduleName } ` ) ;
20
+ } ;
21
+
4
22
const getCommitLintModuleType = async ( ) : Promise < 'cjs' | 'esm' > => {
5
- const packageFile = 'node_modules/@commitlint/load/package.json' ;
6
- const packageJsonPath = path . join (
7
- process . env . PWD || process . cwd ( ) ,
8
- packageFile ,
9
- ) ;
23
+ const packageFile = '@commitlint/load/package.json' ;
24
+ const packageJsonPath = findModulePath ( packageFile ) ;
10
25
const packageJson = JSON . parse ( await fs . readFile ( packageJsonPath , 'utf8' ) ) ;
26
+
11
27
if ( ! packageJson ) {
12
28
throw new Error ( `Failed to parse ${ packageFile } ` ) ;
13
29
}
@@ -19,44 +35,39 @@ const getCommitLintModuleType = async (): Promise<'cjs' | 'esm'> => {
19
35
* QualifiedConfig from any version of @commitlint/types
20
36
* @see https://github.com/conventional-changelog/commitlint/blob/master/@commitlint/types/src/load.ts
21
37
*/
22
- type QualifiedConfigOnAnyVersion = { [ key :string ] : unknown } ;
38
+ type QualifiedConfigOnAnyVersion = { [ key : string ] : unknown } ;
23
39
24
40
/**
25
41
* This code is loading the configuration for the `@commitlint` package from the current working
26
42
* directory (`process.env.PWD`) by requiring the `load` module from the `@commitlint` package.
27
43
*
28
44
* @returns
29
45
*/
30
- export const getCommitLintPWDConfig = async ( ) : Promise < QualifiedConfigOnAnyVersion | null > => {
31
- let load , nodeModulesPath ;
32
- switch ( await getCommitLintModuleType ( ) ) {
33
- case 'cjs' :
34
- /**
35
- * CommonJS (<= [email protected] .)
36
- */
37
- nodeModulesPath = path . join (
38
- process . env . PWD || process . cwd ( ) ,
39
- 'node_modules/@commitlint/load' ,
40
- ) ;
41
- load = require ( nodeModulesPath ) . default ;
42
- break ;
43
- case 'esm' :
44
- /**
45
- * ES Module ([email protected] . <= )
46
- * Directory import is not supported in ES Module resolution, so import the file directly
47
- */
48
- nodeModulesPath = path . join (
49
- process . env . PWD || process . cwd ( ) ,
50
- 'node_modules/@commitlint/load/lib/load.js' ,
51
- ) ;
52
- load = ( await import ( nodeModulesPath ) ) . default ;
53
- break ;
54
- }
46
+ export const getCommitLintPWDConfig =
47
+ async ( ) : Promise < QualifiedConfigOnAnyVersion | null > => {
48
+ let load : Function , modulePath : string ;
49
+ switch ( await getCommitLintModuleType ( ) ) {
50
+ case 'cjs' :
51
+ /**
52
+ * CommonJS (<= [email protected] .)
53
+ */
54
+ modulePath = findModulePath ( '@commitlint/load' ) ;
55
+ load = require ( modulePath ) . default ;
56
+ break ;
57
+ case 'esm' :
58
+ /**
59
+ * ES Module ([email protected] . <= )
60
+ * Directory import is not supported in ES Module resolution, so import the file directly
61
+ */
62
+ modulePath = await findModulePath ( '@commitlint/load/lib/load.js' ) ;
63
+ load = ( await import ( modulePath ) ) . default ;
64
+ break ;
65
+ }
55
66
56
- if ( load && typeof load === 'function' ) {
57
- return await load ( ) ;
58
- }
67
+ if ( load && typeof load === 'function' ) {
68
+ return await load ( ) ;
69
+ }
59
70
60
- // @commitlint /load is not a function
61
- return null ;
62
- } ;
71
+ // @commitlint /load is not a function
72
+ return null ;
73
+ } ;
0 commit comments