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