Skip to content

Instantly share code, notes, and snippets.

@liolocs
Last active March 5, 2020 20:01
Show Gist options
  • Save liolocs/3876aa116759b3ccb73ec3de112650ea to your computer and use it in GitHub Desktop.
Save liolocs/3876aa116759b3ccb73ec3de112650ea to your computer and use it in GitHub Desktop.
Commit current branch quick
#!/usr/bin/env node
const {exec} = require('child_process')
const inquirer = require('inquirer');
//inspired from https://gist.github.com/zkat/6b453b0bbb2a752aaa9ece702cb5def2
if (process.mainModule === module) setImmediate(() => main(process.argv).catch(e => console.log(e.stack) && process.exit(1)))
async function main(argv) {
const args = argv.slice(2)
if(args[0]){
let currentBranch;
try {
currentBranch = await getCurrentBranch(args[0]);
} catch (error) {
console.log('error: ', error);
process.exit(1);
}
const commitQuestion = [
{
type: 'input',
name: 'commitMessage',
message: 'Enter your commit message:',
default: 'Just another commit'
}
];
const { commitMessage } = await inquirer.prompt(commitQuestion);
const confirmCommit = [
{
type: 'confirm',
name: 'result',
message: `Commit to branch ${currentBranch} with message: ${commitMessage}?`,
default: false
}
];
const { result } = await inquirer.prompt(confirmCommit);
if (result) {
await commitAndPush(args[0], commitMessage, currentBranch);
}
}else {
console.log('Must include 1 argument containing your full directory path')
}
}
function getCurrentBranch(parentDirectory) {
return new Promise((resolve, reject) => {
exec(`git rev-parse --abbrev-ref HEAD`, { cwd: parentDirectory }, (err, stdout, stderr) => {
if (!err) {
resolve(stdout);
} else {
reject(err);
}
});
});
}
function commitAndPush(parentDirectory, message, branch) {
return new Promise((resolve, reject) => {
exec(
`sudo git add . && git commit -m "${message}" && git push origin ${branch}`,
{ cwd: parentDirectory },
(err, stdout, stderr) => {
if (!err) {
console.log(stdout);
console.log('pushed!');
} else {
reject(err);
}
}
);
});
}
{"name": "commit-quick", "version": "0.0.0", "bin": "./index.js", "dependencies": {"inquirer": "^6.0.0"}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment