chore(package.json): update version to 1.1.18

refactor(commit.ts): rename getStagedGitDiff to getChangedFiles and remove unused import of generateCommitMessageWithChatCompletion
refactor(commit.ts): reformat import statements for better readability

refactor(commit.ts): reformat code for better readability
feat(commit.ts): add support for selecting remote to push to when multiple remotes are available

refactor(commit.ts): remove unnecessary code and fix formatting
feat(commit.ts): add support for extraArgs parameter in generateCommitMessageFromGitDiff function

chore(checkIsLatestVersion.ts): update console message for clarity and readability
fix(git.ts): handle empty output from git command
fix(git.ts): add -- argument to git diff command to handle file names with leading hyphens
refactor(mergeStrings.ts): remove unnecessary whitespace and add missing semicolon
This commit is contained in:
di-sukharev
2023-03-21 15:35:04 +08:00
parent ff81d7e1ca
commit 54b8ba7419
6 changed files with 74 additions and 44 deletions
+60 -32
View File
@@ -3,8 +3,22 @@ import {
GenerateCommitMessageErrorEnum,
generateCommitMessageWithChatCompletion
} from '../generateCommitMessageFromGitDiff';
import { assertGitRepo, getStagedGitDiff, getDiff, getStagedFiles, gitAdd } from '../utils/git';
import { spinner, confirm, outro, isCancel, intro, multiselect, select } from '@clack/prompts';
import {
assertGitRepo,
getChangedFiles,
getDiff,
getStagedFiles,
gitAdd
} from '../utils/git';
import {
spinner,
confirm,
outro,
isCancel,
intro,
multiselect,
select
} from '@clack/prompts';
import chalk from 'chalk';
import { trytm } from '../utils/trytm';
@@ -53,44 +67,58 @@ ${chalk.grey('——————————————————')}`
});
if (isCommitConfirmedByUser && !isCancel(isCommitConfirmedByUser)) {
const { stdout } = await execa('git', ['commit', '-m', commitMessage, ...extraArgs]);
const { stdout } = await execa('git', [
'commit',
'-m',
commitMessage,
...extraArgs
]);
outro(`${chalk.green('✔')} successfully committed`);
outro(stdout);
const remotes = await getGitRemotes();
if (remotes.length === 1) {
const isPushConfirmedByUser = await confirm({
message: 'Do you want to run `git push`?'
});
if (isPushConfirmedByUser && !isCancel(isPushConfirmedByUser)) {
const pushSpinner = spinner();
pushSpinner.start(`Running \`git push ${remotes[0]}\``);
const { stdout } = await execa('git', ['push', remotes[0]]);
pushSpinner.stop(`${chalk.green('✔')} successfully pushed all commits to ${remotes[0]}`);
if (stdout) outro(stdout);
} else {
const selectedRemote = await select({
message: 'Choose a remote to push to',
choices: remotes.map((remote) => ({ title: remote, value: remote })),
const isPushConfirmedByUser = await confirm({
message: 'Do you want to run `git push`?'
});
if (isPushConfirmedByUser && !isCancel(isPushConfirmedByUser)) {
const pushSpinner = spinner();
pushSpinner.start(`Running \`git push ${remotes[0]}\``);
const { stdout } = await execa('git', ['push', remotes[0]]);
pushSpinner.stop(
`${chalk.green('✔')} successfully pushed all commits to ${remotes[0]}`
);
if (stdout) outro(stdout);
}
} else {
const selectedRemote = (await select({
message: 'Choose a remote to push to',
options: remotes.map((remote) => ({ value: remote, label: remote }))
})) as string;
if (!isCancel(selectedRemote)) {
const pushSpinner = spinner();
pushSpinner.start(`Running \`git push ${selectedRemote}\``);
const { stdout } = await execa('git', ['push', selectedRemote]);
pushSpinner.stop(`${chalk.green('✔')} successfully pushed all commits to ${selectedRemote}`);
const pushSpinner = spinner();
pushSpinner.start(`Running \`git push ${selectedRemote}\``);
const { stdout } = await execa('git', ['push', selectedRemote]);
pushSpinner.stop(
`${chalk.green(
'✔'
)} successfully pushed all commits to ${selectedRemote}`
);
if (stdout) outro(stdout);
if (stdout) outro(stdout);
} else outro(`${chalk.gray('✖')} process cancelled`);
}
} else outro(`${chalk.gray('✖')} process cancelled`);
}
};
export async function commit(extraArgs=[], isStageAllFlag = false) {
export async function commit(
extraArgs: string[] = [],
isStageAllFlag: Boolean = false
) {
if (isStageAllFlag) {
const changedFiles = await getChangedFiles();
@@ -129,7 +157,6 @@ export async function commit(extraArgs=[], isStageAllFlag = false) {
isStageAllAndCommitConfirmedByUser &&
!isCancel(isStageAllAndCommitConfirmedByUser)
) {
await commit(extraArgs, true);
process.exit(1);
}
@@ -158,10 +185,11 @@ export async function commit(extraArgs=[], isStageAllFlag = false) {
.join('\n')}`
);
await generateCommitMessageFromGitDiff(staged.diff);
}
const [, generateCommitError] = await trytm(
generateCommitMessageFromGitDiff(await getDiff({ files: stagedFiles }), extraArgs)
generateCommitMessageFromGitDiff(
await getDiff({ files: stagedFiles }),
extraArgs
)
);
if (generateCommitError) {
+3 -5
View File
@@ -11,11 +11,9 @@ export const checkIsLatestVersion = async () => {
console.warn(
chalk.yellow(
`
You are not using the latest stable version of OpenCommit!
Consider updating to the latest version to get the latest features and bug fixes.
Current version: ${currentVersion}
Latest version: ${latestVersion}
🎉 To update to the latest version, run: npm update opencommit
You are not using the latest stable version of OpenCommit with new features and bug fixes.
Current version: ${currentVersion}. Latest version: ${latestVersion}.
🚀 To update run: npm i -g opencommit@latest.
`
)
);
+6 -4
View File
@@ -20,10 +20,10 @@ export const getOpenCommitIgnore = (): Ignore => {
try {
ig.add(readFileSync('.opencommitignore').toString().split('\n'));
} catch(e) {}
} catch (e) {}
return ig;
}
};
export const getStagedFiles = async (): Promise<string[]> => {
const { stdout: files } = await execa('git', [
@@ -33,11 +33,12 @@ export const getStagedFiles = async (): Promise<string[]> => {
'--relative'
]);
if (!files) return [];
const filesList = files.split('\n');
const ig = getOpenCommitIgnore();
const allowedFiles = filesList.filter(file => !ig.ignores(file));
const allowedFiles = filesList.filter((file) => !ig.ignores(file));
if (!allowedFiles) return [];
@@ -86,6 +87,7 @@ export const getDiff = async ({ files }: { files: string[] }) => {
const { stdout: diff } = await execa('git', [
'diff',
'--staged',
'--',
...filesWithoutLocks
]);
+2
View File
@@ -9,6 +9,8 @@ export function mergeStrings(arr: string[], maxStringLength: number): string[] {
currentItem = item;
}
}
mergedArr.push(currentItem);
return mergedArr;
}