-
Notifications
You must be signed in to change notification settings - Fork 317
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: support pnpm * fix(commitlint.ts): format commitlint config output as JSON for better readability * test(commitlint.test.ts): update expected console output for commit message consistency
- Loading branch information
Showing
3 changed files
with
50 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,29 @@ | ||
import fs from 'fs/promises'; | ||
import path from 'path'; | ||
|
||
const findModulePath = (moduleName: string) => { | ||
const searchPaths = [ | ||
path.join('node_modules', moduleName), | ||
path.join('node_modules', '.pnpm') | ||
]; | ||
|
||
for (const basePath of searchPaths) { | ||
try { | ||
const resolvedPath = require.resolve(moduleName, { paths: [basePath] }); | ||
return resolvedPath; | ||
} catch { | ||
// Continue to the next search path if the module is not found | ||
} | ||
} | ||
|
||
throw new Error(`Cannot find module ${moduleName}`); | ||
}; | ||
|
||
const getCommitLintModuleType = async (): Promise<'cjs' | 'esm'> => { | ||
const packageFile = 'node_modules/@commitlint/load/package.json'; | ||
const packageJsonPath = path.join( | ||
process.env.PWD || process.cwd(), | ||
packageFile, | ||
); | ||
const packageFile = '@commitlint/load/package.json'; | ||
const packageJsonPath = findModulePath(packageFile); | ||
const packageJson = JSON.parse(await fs.readFile(packageJsonPath, 'utf8')); | ||
|
||
if (!packageJson) { | ||
throw new Error(`Failed to parse ${packageFile}`); | ||
} | ||
|
@@ -19,44 +35,39 @@ const getCommitLintModuleType = async (): Promise<'cjs' | 'esm'> => { | |
* QualifiedConfig from any version of @commitlint/types | ||
* @see https://github.com/conventional-changelog/commitlint/blob/master/@commitlint/types/src/load.ts | ||
*/ | ||
type QualifiedConfigOnAnyVersion = { [key:string]: unknown }; | ||
type QualifiedConfigOnAnyVersion = { [key: string]: unknown }; | ||
|
||
/** | ||
* This code is loading the configuration for the `@commitlint` package from the current working | ||
* directory (`process.env.PWD`) by requiring the `load` module from the `@commitlint` package. | ||
* | ||
* @returns | ||
*/ | ||
export const getCommitLintPWDConfig = async (): Promise<QualifiedConfigOnAnyVersion | null> => { | ||
let load, nodeModulesPath; | ||
switch (await getCommitLintModuleType()) { | ||
case 'cjs': | ||
/** | ||
* CommonJS (<= [email protected].) | ||
*/ | ||
nodeModulesPath = path.join( | ||
process.env.PWD || process.cwd(), | ||
'node_modules/@commitlint/load', | ||
); | ||
load = require(nodeModulesPath).default; | ||
break; | ||
case 'esm': | ||
/** | ||
* ES Module ([email protected]. <= ) | ||
* Directory import is not supported in ES Module resolution, so import the file directly | ||
*/ | ||
nodeModulesPath = path.join( | ||
process.env.PWD || process.cwd(), | ||
'node_modules/@commitlint/load/lib/load.js', | ||
); | ||
load = (await import(nodeModulesPath)).default; | ||
break; | ||
} | ||
export const getCommitLintPWDConfig = | ||
async (): Promise<QualifiedConfigOnAnyVersion | null> => { | ||
let load: Function, modulePath: string; | ||
switch (await getCommitLintModuleType()) { | ||
case 'cjs': | ||
/** | ||
* CommonJS (<= [email protected].) | ||
*/ | ||
modulePath = findModulePath('@commitlint/load'); | ||
load = require(modulePath).default; | ||
break; | ||
case 'esm': | ||
/** | ||
* ES Module ([email protected]. <= ) | ||
* Directory import is not supported in ES Module resolution, so import the file directly | ||
*/ | ||
modulePath = await findModulePath('@commitlint/load/lib/load.js'); | ||
load = (await import(modulePath)).default; | ||
break; | ||
} | ||
|
||
if (load && typeof load === 'function') { | ||
return await load(); | ||
} | ||
if (load && typeof load === 'function') { | ||
return await load(); | ||
} | ||
|
||
// @commitlint/load is not a function | ||
return null; | ||
}; | ||
// @commitlint/load is not a function | ||
return null; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters