Skip to content

Commit

Permalink
feat: support pnpm (#394)
Browse files Browse the repository at this point in the history
* 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
a1ooha authored Sep 1, 2024
1 parent 8702c17 commit 89d2aa6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/commands/commitlint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const commitlintConfigCommand = command(
if (mode === CONFIG_MODES.get) {
const commitLintConfig = await getCommitlintLLMConfig();

outro(commitLintConfig.toString());
outro(JSON.stringify(commitLintConfig, null, 2));

return;
}
Expand Down
85 changes: 48 additions & 37 deletions src/modules/commitlint/pwd-commitlint.ts
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}`);
}
Expand All @@ -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;
};
4 changes: 1 addition & 3 deletions test/e2e/prompt-module/commitlint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,7 @@ describe('cli flow to generate commit message using @commitlint prompt-module',
[],
{ cwd: gitDir }
);
expect(
await commitlintGet.findByText('[object Object]')
).toBeInTheConsole();
expect(await commitlintGet.findByText('consistency')).toBeInTheConsole();

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

0 comments on commit 89d2aa6

Please sign in to comment.