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,141 +61,78 @@ ${commitMessage}
${chalk.grey('——————————————————')}` ${chalk.grey('——————————————————')}`
); );
const promptUserConfirm = async(commitText: string ) => { const isCommitConfirmedByUser = await confirm({
message: 'Confirm the commit message?'
});
const isCommitConfirmedByUser = await select({ if (isCommitConfirmedByUser && !isCancel(isCommitConfirmedByUser)) {
message: 'Confirm the commit message', const { stdout } = await execa('git', [
options: [ 'commit',
{value: "yes", label: "Yes"}, '-m',
{value: "no", label: "No"}, commitMessage,
{value: "edit", label: "Edit"} ...extraArgs
] ]);
}); outro(`${chalk.green('✔')} successfully committed`);
if (isCommitConfirmedByUser == "yes" && !isCancel(isCommitConfirmedByUser)) { outro(stdout);
const { stdout } = await execa('git', [
'commit',
'-m',
commitText,
...extraArgs
]);
outro(`${chalk.green('✔')} successfully committed`); const remotes = await getGitRemotes();
outro(stdout); if (!remotes.length) {
const { stdout } = await execa('git', ['push']);
if (stdout) outro(stdout);
process.exit(0);
}
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',
'--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); if (stdout) outro(stdout);
} else {
outro('`git push` aborted');
process.exit(0); 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) { if (!isCancel(selectedRemote)) {
const isPushConfirmedByUser = await confirm({ const pushSpinner = spinner();
message: 'Do you want to run `git push`?'
});
if (isPushConfirmedByUser && !isCancel(isPushConfirmedByUser)) { pushSpinner.start(`Running \`git push ${selectedRemote}\``);
const pushSpinner = spinner();
pushSpinner.start(`Running \`git push ${remotes[0]}\``); const { stdout } = await execa('git', ['push', selectedRemote]);
const { stdout } = await execa('git', [ pushSpinner.stop(
'push', `${chalk.green(
'--verbose', '✔'
remotes[0] )} successfully pushed all commits to ${selectedRemote}`
]); );
pushSpinner.stop( if (stdout) outro(stdout);
`${chalk.green('')} successfully pushed all commits to ${remotes[0]}` } else outro(`${chalk.gray('')} process cancelled`);
);
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`);
} }
} }
await promptUserConfirm(commitMessage)
}; };
export async function commit( export async function commit(