From 9ffcdbdb3b6f1ab6c86d1460508a49d737d0f190 Mon Sep 17 00:00:00 2001 From: Jethro Yu Date: Tue, 1 Apr 2025 15:14:09 +0800 Subject: [PATCH] refactor(commitlint): update commitlint configuration and prompts for improved clarity and consistency The commitlint configuration and prompts have been refactored to enhance clarity and maintain consistency throughout the codebase. The type assertion for commitLintConfig is updated to use 'as any' for better type handling. Additionally, formatting adjustments are made in the prompts to ensure proper readability and alignment with the defined conventions. These changes aim to streamline the commit message generation process and improve overall code maintainability. --- src/modules/commitlint/config.ts | 2 +- src/modules/commitlint/prompts.ts | 87 +++++++++++------------- src/modules/commitlint/pwd-commitlint.ts | 2 +- 3 files changed, 42 insertions(+), 49 deletions(-) diff --git a/src/modules/commitlint/config.ts b/src/modules/commitlint/config.ts index 1c509f8..96a6f16 100644 --- a/src/modules/commitlint/config.ts +++ b/src/modules/commitlint/config.ts @@ -53,7 +53,7 @@ export const configureCommitlintIntegration = async (force = false) => { spin.start('Generating consistency with given @commitlint rules'); - const prompts = inferPromptsFromCommitlintConfig(commitLintConfig); + const prompts = inferPromptsFromCommitlintConfig(commitLintConfig as any); const consistencyPrompts = commitlintPrompts.GEN_COMMITLINT_CONSISTENCY_PROMPT(prompts); diff --git a/src/modules/commitlint/prompts.ts b/src/modules/commitlint/prompts.ts index 445e09d..8dd81c5 100644 --- a/src/modules/commitlint/prompts.ts +++ b/src/modules/commitlint/prompts.ts @@ -56,30 +56,28 @@ const llmReadableRules: { blankline: (key, applicable) => `There should ${applicable} be a blank line at the beginning of the ${key}.`, caseRule: (key, applicable, value: string | Array) => - `The ${key} should ${applicable} be in ${ - Array.isArray(value) - ? `one of the following case: + `The ${key} should ${applicable} be in ${Array.isArray(value) + ? `one of the following case: - ${value.join('\n - ')}.` - : `${value} case.` + : `${value} case.` }`, emptyRule: (key, applicable) => `The ${key} should ${applicable} be empty.`, enumRule: (key, applicable, value: string | Array) => - `The ${key} should ${applicable} be one of the following values: + `The ${key} should ${applicable} be one of the following values: - ${Array.isArray(value) ? value.join('\n - ') : value}.`, enumTypeRule: (key, applicable, value: string | Array, prompt) => - `The ${key} should ${applicable} be one of the following values: - - ${ - Array.isArray(value) + `The ${key} should ${applicable} be one of the following values: + - ${Array.isArray(value) ? value - .map((v) => { - const description = getTypeRuleExtraDescription(v, prompt); - if (description) { - return `${v} (${description})`; - } else return v; - }) - .join('\n - ') + .map((v) => { + const description = getTypeRuleExtraDescription(v, prompt); + if (description) { + return `${v} (${description})`; + } else return v; + }) + .join('\n - ') : value - }.`, + }.`, fullStopRule: (key, applicable, value: string) => `The ${key} should ${applicable} end with '${value}'.`, maxLengthRule: (key, applicable, value: string) => @@ -216,15 +214,15 @@ const STRUCTURE_OF_COMMIT = config.OCO_OMIT_SCOPE const GEN_COMMITLINT_CONSISTENCY_PROMPT = ( prompts: string[] ): OpenAI.Chat.Completions.ChatCompletionMessageParam[] => [ - { - role: 'system', - content: `${IDENTITY} Your mission is to create clean and comprehensive commit messages for two different changes in a single codebase and output them in the provided JSON format: one for a bug fix and another for a new feature. + { + role: 'system', + content: `${IDENTITY} Your mission is to create clean and comprehensive commit messages for two different changes in a single codebase and output them in the provided JSON format: one for a bug fix and another for a new feature. Here are the specific requirements and conventions that should be strictly followed: Commit Message Conventions: - The commit message consists of three parts: Header, Body, and Footer. -- Header: +- Header: - Format: ${config.OCO_OMIT_SCOPE ? '`: `' : '`(): `'} - ${prompts.join('\n- ')} @@ -240,7 +238,7 @@ JSON Output Format: "commitDescription": "" } \`\`\` -- The "commitDescription" should not include the commit message’s header, only the description. +- The "commitDescription" should not include the commit message's header, only the description. - Description should not be more than 74 characters. Additional Details: @@ -248,9 +246,9 @@ Additional Details: - Allowing the server to listen on a port specified through the environment variable is considered a new feature. Example Git Diff is to follow:` - }, - INIT_DIFF_PROMPT -]; + }, + INIT_DIFF_PROMPT + ]; /** * Prompt to have LLM generate a message using @commitlint rules. @@ -264,30 +262,25 @@ const INIT_MAIN_PROMPT = ( prompts: string[] ): OpenAI.Chat.Completions.ChatCompletionMessageParam => ({ role: 'system', - content: `${IDENTITY} Your mission is to create clean and comprehensive commit messages in the given @commitlint convention and explain WHAT were the changes ${ - config.OCO_WHY ? 'and WHY the changes were done' : '' - }. I'll send you an output of 'git diff --staged' command, and you convert it into a commit message. -${ - config.OCO_EMOJI - ? 'Use GitMoji convention to preface the commit.' - : 'Do not preface the commit with anything.' -} -${ - config.OCO_DESCRIPTION - ? 'Add a short description of WHY the changes are done after the commit message. Don\'t start it with "This commit", just describe the changes.' - : "Don't add any descriptions to the commit, only commit message." -} + content: `${IDENTITY} Your mission is to create clean and comprehensive commit messages in the given @commitlint convention and explain WHAT were the changes ${config.OCO_WHY ? 'and WHY the changes were done' : '' + }. I'll send you an output of 'git diff --staged' command, and you convert it into a commit message. +${config.OCO_EMOJI + ? 'Use GitMoji convention to preface the commit.' + : 'Do not preface the commit with anything.' + } +${config.OCO_DESCRIPTION + ? 'Add a short description of WHY the changes are done after the commit message. Don\'t start it with "This commit", just describe the changes.' + : "Don't add any descriptions to the commit, only commit message." + } Use the present tense. Use ${language} to answer. -${ - config.OCO_ONE_LINE_COMMIT - ? 'Craft a concise commit message that encapsulates all changes made, with an emphasis on the primary updates. If the modifications share a common theme or scope, mention it succinctly; otherwise, leave the scope out to maintain focus. The goal is to provide a clear and unified overview of the changes in a one single message, without diverging into a list of commit per file change.' - : '' -} -${ - config.OCO_OMIT_SCOPE - ? 'Do not include a scope in the commit message format. Use the format: : ' - : '' -} +${config.OCO_ONE_LINE_COMMIT + ? 'Craft a concise commit message that encapsulates all changes made, with an emphasis on the primary updates. If the modifications share a common theme or scope, mention it succinctly; otherwise, leave the scope out to maintain focus. The goal is to provide a clear and unified overview of the changes in a one single message, without diverging into a list of commit per file change.' + : '' + } +${config.OCO_OMIT_SCOPE + ? 'Do not include a scope in the commit message format. Use the format: : ' + : '' + } You will strictly follow the following conventions to generate the content of the commit message: - ${prompts.join('\n- ')} diff --git a/src/modules/commitlint/pwd-commitlint.ts b/src/modules/commitlint/pwd-commitlint.ts index e01a4a6..1a6d8ed 100644 --- a/src/modules/commitlint/pwd-commitlint.ts +++ b/src/modules/commitlint/pwd-commitlint.ts @@ -60,7 +60,7 @@ export const getCommitLintPWDConfig = * 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'); + modulePath = findModulePath('@commitlint/load/lib/load.js'); load = (await import(modulePath)).default; break; }