From e2e613207864fa1dfd7bd8f5190e11b58240cf52 Mon Sep 17 00:00:00 2001 From: Stanley Horwood Date: Sat, 27 Apr 2024 03:56:49 +0200 Subject: [PATCH] chore(scripts): enhance commit message generation with additional file data Included the reading of file contents directly in the `commit` function to provide a more comprehensive context for commit message generation. This addition will help in producing more accurate and context-aware commit messages by utilizing the contents of the files that are staged for commit. The implementation leverages the `readFileSync` method to gather the necessary data, ensuring all relevant file information is available during the commit message generation process. --- scripts/commit.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/scripts/commit.ts b/scripts/commit.ts index cd1ade03..868d991a 100644 --- a/scripts/commit.ts +++ b/scripts/commit.ts @@ -2,6 +2,7 @@ import { OpenAI } from 'openai' // eslint-disable-next-line n/no-unsupported-features/node-builtins import { createInterface } from 'node:readline/promises' import { readFile } from 'node:fs/promises' +import { readFileSync } from 'node:fs' import { execFileSync, spawn } from 'node:child_process' import { load as parseYaml } from 'js-yaml' import 'dotenv/config' @@ -24,9 +25,11 @@ export async function commit() { const input = process.argv.slice(2).join(' ') const diff = execFileSync('git', ['diff', '--cached', '--staged'], { encoding: 'utf8' }) + const diffPaths = execFileSync('git', ['diff', '--name-only', '--cached', '--staged'], { encoding: 'utf8' }) const diffStat = execFileSync('git', ['diff', '--stat', '--cached', '--staged'], { encoding: 'utf8' }) const lastCommits = execFileSync('git', ['log', '-2', '--pretty=%B'], { encoding: 'utf8' }) const branchName = execFileSync('git', ['branch', '--show-current'], { encoding: 'utf8' }) + const fileContents = diffPaths.split('\n').filter(Boolean).map(path => readFileSync(path, 'utf8')) const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY }) const response = await openai.chat.completions.create({ @@ -36,6 +39,7 @@ export async function commit() { { content: `[LAST_COMMITS]\n${lastCommits}\n\n`, role: 'user' }, { content: `[BRANCH_NAME]\n${branchName}\n\n`, role: 'user' }, { content: `[DIFF_STAGED_STATS]\n${diffStat}\n\n`, role: 'user' }, + { content: `[FILE_CONTENTS]\n${fileContents.join('\n')}\n\n`, role: 'user' }, { content: `[DIFF}]\n${diff}`, role: 'user' }, { content: `[INPUT]\n${input}`, role: 'user' }, ],