From 89d2aa603b45e4b2a10e238547e8d1e0b935cb1b Mon Sep 17 00:00:00 2001 From: Aloha <13401668+a1ooha@users.noreply.github.com> Date: Sun, 1 Sep 2024 21:11:26 +0800 Subject: [PATCH] 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 --- src/commands/commitlint.ts | 2 +- src/modules/commitlint/pwd-commitlint.ts | 85 +++++++++++++---------- test/e2e/prompt-module/commitlint.test.ts | 4 +- 3 files changed, 50 insertions(+), 41 deletions(-) diff --git a/src/commands/commitlint.ts b/src/commands/commitlint.ts index 48d086c9..77fbb99f 100644 --- a/src/commands/commitlint.ts +++ b/src/commands/commitlint.ts @@ -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; } diff --git a/src/modules/commitlint/pwd-commitlint.ts b/src/modules/commitlint/pwd-commitlint.ts index 76e74e98..02d58556 100644 --- a/src/modules/commitlint/pwd-commitlint.ts +++ b/src/modules/commitlint/pwd-commitlint.ts @@ -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,7 +35,7 @@ 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 @@ -27,36 +43,31 @@ type QualifiedConfigOnAnyVersion = { [key:string]: unknown }; * * @returns */ -export const getCommitLintPWDConfig = async (): Promise => { - let load, nodeModulesPath; - switch (await getCommitLintModuleType()) { - case 'cjs': - /** - * CommonJS (<= commitlint@v18.x.x.) - */ - nodeModulesPath = path.join( - process.env.PWD || process.cwd(), - 'node_modules/@commitlint/load', - ); - load = require(nodeModulesPath).default; - break; - case 'esm': - /** - * ES Module (commitlint@v19.x.x. <= ) - * 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 => { + let load: Function, modulePath: string; + switch (await getCommitLintModuleType()) { + case 'cjs': + /** + * CommonJS (<= commitlint@v18.x.x.) + */ + modulePath = findModulePath('@commitlint/load'); + load = require(modulePath).default; + break; + case 'esm': + /** + * 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; + 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; + }; diff --git a/test/e2e/prompt-module/commitlint.test.ts b/test/e2e/prompt-module/commitlint.test.ts index b7303e43..a78ebb61 100644 --- a/test/e2e/prompt-module/commitlint.test.ts +++ b/test/e2e/prompt-module/commitlint.test.ts @@ -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`], {