Suggest adding 'Edit' option to the Yes/No confirmation #53 (#70)

This commit is contained in:
Joshua Hamlet
2023-04-27 23:22:45 -07:00
committed by GitHub
parent a8a548ba5a
commit 70f048672c
2 changed files with 127 additions and 61 deletions
+2 -1
View File
@@ -1,4 +1,5 @@
{ {
"trailingComma": "none", "trailingComma": "none",
"singleQuote": true "singleQuote": true,
"semi": true
} }
+78 -13
View File
@@ -1,4 +1,6 @@
import { execa } from 'execa'; import { execa } from 'execa';
import fs from 'fs';
import os from 'os';
import { import {
GenerateCommitMessageErrorEnum, GenerateCommitMessageErrorEnum,
generateCommitMessageWithChatCompletion generateCommitMessageWithChatCompletion
@@ -17,7 +19,7 @@ import {
isCancel, isCancel,
intro, intro,
multiselect, multiselect,
select select,
} from '@clack/prompts'; } from '@clack/prompts';
import chalk from 'chalk'; import chalk from 'chalk';
import { trytm } from '../utils/trytm'; import { trytm } from '../utils/trytm';
@@ -61,15 +63,23 @@ ${commitMessage}
${chalk.grey('——————————————————')}` ${chalk.grey('——————————————————')}`
); );
const isCommitConfirmedByUser = await confirm({ const promptUserConfirm = async(commitText: string ) => {
message: 'Confirm the commit message?'
const isCommitConfirmedByUser = await select({
message: 'Confirm the commit message',
options: [
{value: "yes", label: "Yes"},
{value: "no", label: "No"},
{value: "edit", label: "Edit"}
]
}); });
if (isCommitConfirmedByUser && !isCancel(isCommitConfirmedByUser)) { if (isCommitConfirmedByUser == "yes" && !isCancel(isCommitConfirmedByUser)) {
const { stdout } = await execa('git', [ const { stdout } = await execa('git', [
'commit', 'commit',
'-m', '-m',
commitMessage, commitText,
...extraArgs ...extraArgs
]); ]);
@@ -106,10 +116,6 @@ ${chalk.grey('——————————————————')}`
); );
if (stdout) outro(stdout); if (stdout) outro(stdout);
} else {
outro('`git push` aborted');
process.exit(0);
}
} else { } else {
const selectedRemote = (await select({ const selectedRemote = (await select({
message: 'Choose a remote to push to', message: 'Choose a remote to push to',
@@ -118,11 +124,8 @@ ${chalk.grey('——————————————————')}`
if (!isCancel(selectedRemote)) { if (!isCancel(selectedRemote)) {
const pushSpinner = spinner(); const pushSpinner = spinner();
pushSpinner.start(`Running \`git push ${selectedRemote}\``); pushSpinner.start(`Running \`git push ${selectedRemote}\``);
const { stdout } = await execa('git', ['push', selectedRemote]); const { stdout } = await execa('git', ['push', selectedRemote]);
pushSpinner.stop( pushSpinner.stop(
`${chalk.green( `${chalk.green(
'✔' '✔'
@@ -130,9 +133,71 @@ ${chalk.grey('——————————————————')}`
); );
if (stdout) outro(stdout); if (stdout) outro(stdout);
} else outro(`${chalk.gray('✖')} process cancelled`); } 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`);
}
}
await promptUserConfirm(commitMessage)
}; };
export async function commit( export async function commit(