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.
This commit is contained in:
di-sukharev
2023-03-29 10:43:27 +08:00
parent ea864d18f4
commit 4cc73208cd
3 changed files with 16 additions and 9 deletions
+12 -6
View File
@@ -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);
@@ -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)) {