feat: support pnpm (#394)

* 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
This commit is contained in:
Aloha
2024-09-01 21:11:26 +08:00
committed by GitHub
parent 8702c17758
commit 89d2aa603b
3 changed files with 50 additions and 41 deletions
+1 -1
View File
@@ -23,7 +23,7 @@ export const commitlintConfigCommand = command(
if (mode === CONFIG_MODES.get) { if (mode === CONFIG_MODES.get) {
const commitLintConfig = await getCommitlintLLMConfig(); const commitLintConfig = await getCommitlintLLMConfig();
outro(commitLintConfig.toString()); outro(JSON.stringify(commitLintConfig, null, 2));
return; return;
} }
+48 -37
View File
@@ -1,13 +1,29 @@
import fs from 'fs/promises'; import fs from 'fs/promises';
import path from 'path'; 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 getCommitLintModuleType = async (): Promise<'cjs' | 'esm'> => {
const packageFile = 'node_modules/@commitlint/load/package.json'; const packageFile = '@commitlint/load/package.json';
const packageJsonPath = path.join( const packageJsonPath = findModulePath(packageFile);
process.env.PWD || process.cwd(),
packageFile,
);
const packageJson = JSON.parse(await fs.readFile(packageJsonPath, 'utf8')); const packageJson = JSON.parse(await fs.readFile(packageJsonPath, 'utf8'));
if (!packageJson) { if (!packageJson) {
throw new Error(`Failed to parse ${packageFile}`); throw new Error(`Failed to parse ${packageFile}`);
} }
@@ -19,7 +35,7 @@ const getCommitLintModuleType = async (): Promise<'cjs' | 'esm'> => {
* QualifiedConfig from any version of @commitlint/types * QualifiedConfig from any version of @commitlint/types
* @see https://github.com/conventional-changelog/commitlint/blob/master/@commitlint/types/src/load.ts * @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 * This code is loading the configuration for the `@commitlint` package from the current working
@@ -27,36 +43,31 @@ type QualifiedConfigOnAnyVersion = { [key:string]: unknown };
* *
* @returns * @returns
*/ */
export const getCommitLintPWDConfig = async (): Promise<QualifiedConfigOnAnyVersion | null> => { export const getCommitLintPWDConfig =
let load, nodeModulesPath; async (): Promise<QualifiedConfigOnAnyVersion | null> => {
switch (await getCommitLintModuleType()) { let load: Function, modulePath: string;
case 'cjs': switch (await getCommitLintModuleType()) {
/** case 'cjs':
* CommonJS (<= commitlint@v18.x.x.) /**
*/ * CommonJS (<= commitlint@v18.x.x.)
nodeModulesPath = path.join( */
process.env.PWD || process.cwd(), modulePath = findModulePath('@commitlint/load');
'node_modules/@commitlint/load', load = require(modulePath).default;
); break;
load = require(nodeModulesPath).default; case 'esm':
break; /**
case 'esm': * ES Module (commitlint@v19.x.x. <= )
/** * Directory import is not supported in ES Module resolution, so import the file directly
* ES Module (commitlint@v19.x.x. <= ) */
* 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;
nodeModulesPath = path.join( break;
process.env.PWD || process.cwd(), }
'node_modules/@commitlint/load/lib/load.js',
);
load = (await import(nodeModulesPath)).default;
break;
}
if (load && typeof load === 'function') { if (load && typeof load === 'function') {
return await load(); return await load();
} }
// @commitlint/load is not a function // @commitlint/load is not a function
return null; return null;
}; };
+1 -3
View File
@@ -181,9 +181,7 @@ describe('cli flow to generate commit message using @commitlint prompt-module',
[], [],
{ cwd: gitDir } { cwd: gitDir }
); );
expect( expect(await commitlintGet.findByText('consistency')).toBeInTheConsole();
await commitlintGet.findByText('[object Object]')
).toBeInTheConsole();
// Run 'oco' using .opencommit-commitlint // Run 'oco' using .opencommit-commitlint
await render('echo', [`'console.log("Hello World");' > index.ts`], { await render('echo', [`'console.log("Hello World");' > index.ts`], {