diff --git a/package-lock.json b/package-lock.json index 47eac80..3416978 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "opencommit", - "version": "1.1.16", + "version": "1.1.18", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "opencommit", - "version": "1.1.16", + "version": "1.1.18", "license": "ISC", "dependencies": { "@clack/prompts": "^0.6.1", diff --git a/package.json b/package.json index 22522d3..e50b496 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "opencommit", - "version": "1.1.16", + "version": "1.1.18", "description": "GPT CLI to auto-generate impressive commits in 1 second. Killing lame commits with AI 🤯🔫", "keywords": [ "git", diff --git a/src/commands/commit.ts b/src/commands/commit.ts index ae144a7..af6759f 100644 --- a/src/commands/commit.ts +++ b/src/commands/commit.ts @@ -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) { diff --git a/src/utils/checkIsLatestVersion.ts b/src/utils/checkIsLatestVersion.ts index 555fc69..89f2751 100644 --- a/src/utils/checkIsLatestVersion.ts +++ b/src/utils/checkIsLatestVersion.ts @@ -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. ` ) ); diff --git a/src/utils/git.ts b/src/utils/git.ts index d971eb6..bba4948 100644 --- a/src/utils/git.ts +++ b/src/utils/git.ts @@ -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 => { const { stdout: files } = await execa('git', [ @@ -33,11 +33,12 @@ export const getStagedFiles = async (): Promise => { '--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 ]); diff --git a/src/utils/mergeStrings.ts b/src/utils/mergeStrings.ts index a8beb37..7b55a99 100644 --- a/src/utils/mergeStrings.ts +++ b/src/utils/mergeStrings.ts @@ -9,6 +9,8 @@ export function mergeStrings(arr: string[], maxStringLength: number): string[] { currentItem = item; } } + mergedArr.push(currentItem); + return mergedArr; }