Skip to content

Commit 89d2aa6

Browse files
authored
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
1 parent 8702c17 commit 89d2aa6

File tree

3 files changed

+50
-41
lines changed

3 files changed

+50
-41
lines changed

src/commands/commitlint.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export const commitlintConfigCommand = command(
2323
if (mode === CONFIG_MODES.get) {
2424
const commitLintConfig = await getCommitlintLLMConfig();
2525

26-
outro(commitLintConfig.toString());
26+
outro(JSON.stringify(commitLintConfig, null, 2));
2727

2828
return;
2929
}
Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
11
import fs from 'fs/promises';
22
import 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+
422
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);
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 (<= [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+
}
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+
};

test/e2e/prompt-module/commitlint.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,7 @@ describe('cli flow to generate commit message using @commitlint prompt-module',
181181
[],
182182
{ cwd: gitDir }
183183
);
184-
expect(
185-
await commitlintGet.findByText('[object Object]')
186-
).toBeInTheConsole();
184+
expect(await commitlintGet.findByText('consistency')).toBeInTheConsole();
187185

188186
// Run 'oco' using .opencommit-commitlint
189187
await render('echo', [`'console.log("Hello World");' > index.ts`], {

0 commit comments

Comments
 (0)