11import fs from 'fs/promises' ;
22import path from 'path' ;
33
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+
422const 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 ) ;
1025 const packageJson = JSON . parse ( await fs . readFile ( packageJsonPath , 'utf8' ) ) ;
26+
1127 if ( ! packageJson ) {
1228 throw new Error ( `Failed to parse ${ packageFile } ` ) ;
1329 }
@@ -19,44 +35,39 @@ const getCommitLintModuleType = async (): Promise<'cjs' | 'esm'> => {
1935 * QualifiedConfig from any version of @commitlint/types
2036 * @see https://github.com/conventional-changelog/commitlint/blob/master/@commitlint/types/src/load.ts
2137 */
22- type QualifiedConfigOnAnyVersion = { [ key :string ] : unknown } ;
38+ type QualifiedConfigOnAnyVersion = { [ key : string ] : unknown } ;
2339
2440/**
2541 * This code is loading the configuration for the `@commitlint` package from the current working
2642 * directory (`process.env.PWD`) by requiring the `load` module from the `@commitlint` package.
2743 *
2844 * @returns
2945 */
30- export const getCommitLintPWDConfig = async ( ) : Promise < QualifiedConfigOnAnyVersion | null > => {
31- let load , nodeModulesPath ;
32- switch ( await getCommitLintModuleType ( ) ) {
33- case 'cjs' :
34- /**
35- * CommonJS (<= commitlint@v18.x.x.)
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 (commitlint@v19.x.x. <= )
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 (<= commitlint@v18.x.x.)
53+ */
54+ modulePath = findModulePath ( '@commitlint/load' ) ;
55+ load = require ( modulePath ) . default ;
56+ break ;
57+ case 'esm' :
58+ /**
59+ * ES Module (commitlint@v19.x.x. <= )
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+ }
5566
56- if ( load && typeof load === 'function' ) {
57- return await load ( ) ;
58- }
67+ if ( load && typeof load === 'function' ) {
68+ return await load ( ) ;
69+ }
5970
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