From 4cc73208cd0e8a69c05e6aa0e094057516c37351 Mon Sep 17 00:00:00 2001 From: di-sukharev Date: Wed, 29 Mar 2023 10:43:27 +0800 Subject: [PATCH] chore(.gitignore): add test.ts to the list of ignored files fix(prepare-commit-msg-hook.ts): add missing await keyword to getStagedFiles() function call feat(prepare-commit-msg-hook.ts): add spinner to indicate commit message generation progress feat(utils/mergeDiffs.ts): add mergeDiffs function to merge array of strings into an array of strings with a maximum length The test.ts file is now ignored by git. The missing await keyword has been added to the getStagedFiles() function call. A spinner has been added to indicate the progress of commit message generation. The mergeDiffs function has been added to merge an array of strings into an array of strings with a maximum length. --- .gitignore | 3 ++- src/commands/prepare-commit-msg-hook.ts | 18 ++++++++++++------ src/utils/{mergeStrings.ts => mergeDiffs.ts} | 4 ++-- 3 files changed, 16 insertions(+), 9 deletions(-) rename src/utils/{mergeStrings.ts => mergeDiffs.ts} (73%) diff --git a/.gitignore b/.gitignore index 89dcc5c..e95a4db 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,5 @@ logfile.log uncaughtExceptions.log .vscode src/*.json -.idea \ No newline at end of file +.idea +test.ts \ No newline at end of file diff --git a/src/commands/prepare-commit-msg-hook.ts b/src/commands/prepare-commit-msg-hook.ts index 68547dc..afde485 100644 --- a/src/commands/prepare-commit-msg-hook.ts +++ b/src/commands/prepare-commit-msg-hook.ts @@ -1,6 +1,6 @@ import fs from 'fs/promises'; import chalk from 'chalk'; -import { intro, outro } from '@clack/prompts'; +import { intro, outro, spinner } from '@clack/prompts'; import { getChangedFiles, getDiff, getStagedFiles, gitAdd } from '../utils/git'; import { getConfig } from './config'; import { generateCommitMessageWithChatCompletion } from '../generateCommitMessageFromGitDiff'; @@ -17,11 +17,13 @@ export const prepareCommitMessageHook = async () => { if (commitSource) return; + const stagedFiles = await getStagedFiles(); const changedFiles = await getChangedFiles(); - if (changedFiles) await gitAdd({ files: changedFiles }); + + if (!stagedFiles && changedFiles) await gitAdd({ files: changedFiles }); else { - outro("No changes detected, write some code and run `oc` again"); - process.exit(1); + outro('No changes detected, write some code and run `oc` again'); + process.exit(1); } const staged = await getStagedFiles(); @@ -38,11 +40,15 @@ export const prepareCommitMessageHook = async () => { ); } + const spin = spinner(); + spin.start('Generating commit message'); const commitMessage = await generateCommitMessageWithChatCompletion( await getDiff({ files: staged }) ); - - if (typeof commitMessage !== 'string') throw new Error(commitMessage.error); + if (typeof commitMessage !== 'string') { + spin.stop('Error'); + throw new Error(commitMessage.error); + } else spin.stop('Done'); const fileContent = await fs.readFile(messageFilePath); diff --git a/src/utils/mergeStrings.ts b/src/utils/mergeDiffs.ts similarity index 73% rename from src/utils/mergeStrings.ts rename to src/utils/mergeDiffs.ts index ee35f50..a7b6aa0 100644 --- a/src/utils/mergeStrings.ts +++ b/src/utils/mergeDiffs.ts @@ -1,5 +1,5 @@ -import { tokenCount } from './tokenCount' -export function mergeStrings(arr: string[], maxStringLength: number): string[] { +import { tokenCount } from './tokenCount'; +export function mergeDiffs(arr: string[], maxStringLength: number): string[] { const mergedArr: string[] = []; let currentItem: string = arr[0]; for (const item of arr.slice(1)) {