diff --git a/.prettierrc b/.prettierrc index 45ad4d7..e9c0f50 100644 --- a/.prettierrc +++ b/.prettierrc @@ -1,5 +1,4 @@ { "trailingComma": "none", - "singleQuote": true, - "semi": true + "singleQuote": true } diff --git a/src/commands/commit.ts b/src/commands/commit.ts index 7b21b03..4ca9709 100644 --- a/src/commands/commit.ts +++ b/src/commands/commit.ts @@ -1,6 +1,4 @@ import { execa } from 'execa'; -import fs from 'fs'; -import os from 'os'; import { GenerateCommitMessageErrorEnum, generateCommitMessageWithChatCompletion @@ -19,7 +17,7 @@ import { isCancel, intro, multiselect, - select, + select } from '@clack/prompts'; import chalk from 'chalk'; import { trytm } from '../utils/trytm'; @@ -62,142 +60,79 @@ ${chalk.grey('——————————————————')} ${commitMessage} ${chalk.grey('——————————————————')}` ); - - const promptUserConfirm = async(commitText: string ) => { - const isCommitConfirmedByUser = await select({ - message: 'Confirm the commit message', - options: [ - {value: "yes", label: "Yes"}, - {value: "no", label: "No"}, - {value: "edit", label: "Edit"} - ] - - }); + const isCommitConfirmedByUser = await confirm({ + message: 'Confirm the commit message?' + }); - if (isCommitConfirmedByUser == "yes" && !isCancel(isCommitConfirmedByUser)) { - const { stdout } = await execa('git', [ - 'commit', - '-m', - commitText, - ...extraArgs - ]); + if (isCommitConfirmedByUser && !isCancel(isCommitConfirmedByUser)) { + const { stdout } = await execa('git', [ + 'commit', + '-m', + commitMessage, + ...extraArgs + ]); - outro(`${chalk.green('✔')} successfully committed`); + outro(`${chalk.green('✔')} successfully committed`); - outro(stdout); - - const remotes = await getGitRemotes(); + outro(stdout); + + const remotes = await getGitRemotes(); + + if (!remotes.length) { + const { stdout } = await execa('git', ['push']); + if (stdout) outro(stdout); + process.exit(0); + } + + 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', + '--verbose', + remotes[0] + ]); + + pushSpinner.stop( + `${chalk.green('✔')} successfully pushed all commits to ${remotes[0]}` + ); - if (!remotes.length) { - const { stdout } = await execa('git', ['push']); if (stdout) outro(stdout); + } else { + outro('`git push` aborted'); process.exit(0); } + } else { + const selectedRemote = (await select({ + message: 'Choose a remote to push to', + options: remotes.map((remote) => ({ value: remote, label: remote })) + })) as string; - if (remotes.length === 1) { - const isPushConfirmedByUser = await confirm({ - message: 'Do you want to run `git push`?' - }); + if (!isCancel(selectedRemote)) { + const pushSpinner = spinner(); - if (isPushConfirmedByUser && !isCancel(isPushConfirmedByUser)) { - const pushSpinner = spinner(); + pushSpinner.start(`Running \`git push ${selectedRemote}\``); - pushSpinner.start(`Running \`git push ${remotes[0]}\``); + const { stdout } = await execa('git', ['push', selectedRemote]); - const { stdout } = await execa('git', [ - 'push', - '--verbose', - remotes[0] - ]); + pushSpinner.stop( + `${chalk.green( + '✔' + )} successfully pushed all commits to ${selectedRemote}` + ); - 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}` - ); - - if (stdout) outro(stdout); - } else { - outro('`git push` aborted'); - process.exit(0); - } - } - } - } else if (isCommitConfirmedByUser == "edit" && !isCancel(isCommitConfirmedByUser)) { - - let defaultEditor = process.env.EDITOR || (process.platform === 'win32' ? 'notepad.exe' : 'vi'); - let defaultOpenCommand - let linuxTermFlag = '' - - switch (os.platform()) { - case 'darwin': - defaultOpenCommand = 'open' - break - case 'win32': - defaultOpenCommand = 'start' - break - case 'linux': - if ( - defaultEditor == 'vi' || - defaultEditor == 'vim' || - defaultEditor == 'nvim' || - defaultEditor == 'nano' || - defaultEditor == 'micro' || - defaultEditor == 'emacs' - ) { - defaultOpenCommand = 'x-terminal-emulator' - linuxTermFlag = '-e' - break - } else { - defaultOpenCommand = 'xdg-open' - break - } - } - - fs.writeFileSync('tmp_commit.txt', commitText); - - outro('🙏 Please close the file when you are done editing it.') - - const { } = await execa(`${defaultOpenCommand}`, [linuxTermFlag, defaultEditor, 'tmp_commit.txt']); - - process.stdin.resume(); - - const updatedCommitMessage = fs.readFileSync('tmp_commit.txt', 'utf-8'); - const updatedCommitMessageTrimmed = updatedCommitMessage.trim() - - fs.unlinkSync('tmp_commit.txt'); - - outro( - `Commit message: -${chalk.grey('——————————————————')} -${updatedCommitMessageTrimmed} -${chalk.grey('——————————————————')}` - ) - - await promptUserConfirm(updatedCommitMessage) - - } else if (isCommitConfirmedByUser == "no" && !isCancel(isCommitConfirmedByUser)) { - outro(`👋 exiting`); - } + if (stdout) outro(stdout); + } else outro(`${chalk.gray('✖')} process cancelled`); + } } - - await promptUserConfirm(commitMessage) }; export async function commit(