Compare commits

...

1 Commits

Author SHA1 Message Date
Sukharev
78456c4431 Revert "Suggest adding 'Edit' option to the Yes/No confirmation #53 (#70)"
This reverts commit 70f048672c.
2023-04-28 14:23:59 +08:00
2 changed files with 61 additions and 127 deletions

View File

@@ -1,5 +1,4 @@
{ {
"trailingComma": "none", "trailingComma": "none",
"singleQuote": true, "singleQuote": true
"semi": true
} }

View File

@@ -1,6 +1,4 @@
import { execa } from 'execa'; import { execa } from 'execa';
import fs from 'fs';
import os from 'os';
import { import {
GenerateCommitMessageErrorEnum, GenerateCommitMessageErrorEnum,
generateCommitMessageWithChatCompletion generateCommitMessageWithChatCompletion
@@ -19,7 +17,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';
@@ -63,23 +61,15 @@ ${commitMessage}
${chalk.grey('——————————————————')}` ${chalk.grey('——————————————————')}`
); );
const promptUserConfirm = async(commitText: string ) => { const isCommitConfirmedByUser = await confirm({
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 == "yes" && !isCancel(isCommitConfirmedByUser)) { if (isCommitConfirmedByUser && !isCancel(isCommitConfirmedByUser)) {
const { stdout } = await execa('git', [ const { stdout } = await execa('git', [
'commit', 'commit',
'-m', '-m',
commitText, commitMessage,
...extraArgs ...extraArgs
]); ]);
@@ -116,6 +106,10 @@ ${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',
@@ -124,8 +118,11 @@ ${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(
'✔' '✔'
@@ -133,71 +130,9 @@ ${chalk.grey('——————————————————')}`
); );
if (stdout) outro(stdout); if (stdout) outro(stdout);
} else { } else outro(`${chalk.gray('✖')} process cancelled`);
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(