Skip to content

Commit

Permalink
implement git shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
kahlil committed Jul 16, 2018
1 parent f7817a3 commit 82af319
Show file tree
Hide file tree
Showing 12 changed files with 217 additions and 31 deletions.
27 changes: 14 additions & 13 deletions cli.js
@@ -1,19 +1,20 @@
#!/usr/bin/env node
'use strict';
const meow = require('meow');

const cli = meow(`
Usage
$ g [input]
const meow = require('meow');
const gx = require('./lib/belly');
const {cliHelp} = require('./lib/texts');

Options
--foo Lorem ipsum [Default: false]
const config = {
flags: {
commitMessage: {
type: 'string',
default: `gx auto-commit`,
alias: 'm'
}
}
};

Examples
$ g
unicorns & rainbows
$ g ponies
ponies & rainbows
`);
const cli = meow(cliHelp, config);

console.log(cli.input[0] || 'unicorns');
gx(cli);
25 changes: 25 additions & 0 deletions lib/addAllCommitAndPush.js
@@ -0,0 +1,25 @@
const ora = require('ora');
const git = require('./git');
const texts = require('./texts');
const handleGitError = require('./handleGitError');

const commitSpinner = ora(`Staging and committing all latest changes`);
const pushSpinner = ora(`Pushing latest changes to origin`);

async function addAllCommitAndPush(commitMessage) {
commitSpinner.start();
try {
const branchSummary = await git.branchLocal();
const currentBranch = branchSummary.current;
await git.add('.');
await git.commit(commitMessage);
commitSpinner.succeed(texts.commitSuccess);
pushSpinner.start();
await git.push('origin', currentBranch);
pushSpinner.succeed();
} catch (err) {
handleGitError(err);
}
}

module.exports = addAllCommitAndPush;
29 changes: 29 additions & 0 deletions lib/belly.js
@@ -0,0 +1,29 @@
const addAllCommitAndPush = require('./addAllCommitAndPush');
const switchToBranch = require('./switchToBranch');
const tagAndPushTags = require('./tagAndPushTags');
const handleGitError = require('./handleGitError');

module.exports = async cli => {
const {commitMessage} = cli.flags;
if (cli.input[0] === 'c') {
try {
await addAllCommitAndPush(commitMessage);
} catch (error) {
handleGitError(error);
}
} else if (cli.input[0] === 's') {
try {
await switchToBranch(cli.input[1]);
} catch (error) {
handleGitError(error);
}
} else if (cli.input[0] === 't') {
if (cli.input[1]) {
tagAndPushTags(cli.input[1]);
} else {
console.error('\nNo version specified');
process.exit(1);
}
}
process.exit(0);
};
5 changes: 5 additions & 0 deletions lib/git.js
@@ -0,0 +1,5 @@
const git = require('simple-git/promise')();

git.silent(true);

module.exports = git;
6 changes: 6 additions & 0 deletions lib/handleGitError.js
@@ -0,0 +1,6 @@
function handleGitError(err) {
console.log('\n\n' + err);
process.exit(1);
}

module.exports = handleGitError;
8 changes: 8 additions & 0 deletions lib/helpers.js
@@ -0,0 +1,8 @@
module.exports = {
isEmptyArray(arr) {
if (Array.isArray(arr)) {
return arr.length === 0;
}
throw new TypeError('Input must be of type Array');
}
};
27 changes: 27 additions & 0 deletions lib/switchToBranch.js
@@ -0,0 +1,27 @@
const ora = require('ora');
const git = require('./git');
const texts = require('./texts');
const handleGitError = require('./handleGitError');

const switchSpinner = ora(`Checking out branch`);

async function switchToBranch(branchName = '-') {
switchSpinner.start();
const branchSummary = await git.branchLocal();
try {
if (branchSummary.all.includes(branchName)) {
await git.checkout(branchName);
switchSpinner.succeed('Switched to branch: ' + branchName);
} else if (branchName === '-') {
await git.checkout(branchName);
switchSpinner.succeed(texts.switchSuccess);
} else {
await git.checkoutLocalBranch(branchName);
switchSpinner.succeed('Checked out new branch: ' + branchName);
}
} catch (err) {
handleGitError(err);
}
}

module.exports = switchToBranch;
20 changes: 20 additions & 0 deletions lib/tagAndPushTags.js
@@ -0,0 +1,20 @@
const ora = require('ora');
const git = require('./git');
const handleGitError = require('./handleGitError');

async function tagAndPushTags(tag) {
const tagSpinner = ora('Tagging and annotating latest commit with ' + tag);
const pushTagsSpinner = ora('Push tags to origin');
tagSpinner.start();
try {
await git.addAnnotatedTag(tag, tag);
tagSpinner.succeed('Tagged and annotated latest commit wiith ' + tag);
pushTagsSpinner.start();
await git.pushTags('origin');
pushTagsSpinner.succeed('Pushed tags to origin');
} catch (err) {
handleGitError(err);
}
}

module.exports = tagAndPushTags;
36 changes: 36 additions & 0 deletions lib/texts.js
@@ -0,0 +1,36 @@
module.exports = {
genericError: `💥 Something went wrong.`,
commitSuccess: `Everything was committed`,
pushSuccess: `Latest commit was pushed to origin`,
switchSuccess: `Switched to last branch`,
cliHelp: `
Usage
$ belly [c | s | t | r | n | q]
Options
--help Display this message
--message -m "<commit message>" Add a custom commit message
Examples
Commit all staged and unstaged changes with a generic
commit message and push the commit to origin
$ belly c
Commit all staged and unstaged changes with a custom
commit message and push the commit to origin
$ belly c -m "Made some awesome changes"
Switch to last branch or switch to / create a branch with a specific name
$ belly s [<branch-name>]
Tag and annotate the current commit with a version number
and push the tag to origin
$ belly t <version-number>
Rename the current branch locally and on origin
$ belly n <new-branch-name>
Squash all commits since master
$ belly q -m "<commit-message>"
`
};
18 changes: 15 additions & 3 deletions package.json
@@ -1,16 +1,18 @@
{
"name": "@kahlil/gx",
"name": "belly",
"version": "1.0.0",
"description": "",
"license": "MIT",
"repository": "kahlil/g",
"repository": "kahlil/belly",
"author": {
"name": "Kahlil Lechelt",
"email": "hello@kahlil.info",
"url": "https://www.kahlillechelt.com"
},
"bin": {
"gx": "cli.js"
"belly": "cli.js",
"gut": "cli.js",
"b": "cli.js"
},
"engines": {
"node": ">=6"
Expand All @@ -27,12 +29,22 @@
"git"
],
"dependencies": {
"chalk": "^2.4.1",
"meow": "^5.0.0",
"ora": "^2.1.0",
"simple-git": "^1.96.0"
},
"devDependencies": {
"ava": "^0.25.0",
"execa": "^0.10.0",
"xo": "^0.21.0"
},
"xo": {
"space": true,
"rules": {
"unicorn/no-process-exit": 0,
"unicorn/filename-case": 0,
"no-warning-comments": 0
}
}
}
37 changes: 27 additions & 10 deletions readme.md
@@ -1,31 +1,48 @@
# g
# belly

> A simplified Git CLI.
> Git shortcuts for common tasks.

## Install

```
$ npm install --global g
$ npm install --global belly
```


## Usage

```
$ g --help
$ belly --help
Usage
g [input]
$ belly [c | s | t | n | q]
Options
--foo Lorem ipsum [Default: false]
--help Display this message
--message -m "<commit message>" Add a custom commit message
Examples
$ g
unicorns & rainbows
$ g ponies
ponies & rainbows
Commit all staged and unstaged changes with a generic
commit message and push the commit to origin
$ belly c
Commit all staged and unstaged changes with a custom
commit message and push the commit to origin
$ belly c -m "Made some awesome changes"
Switch to last branch or switch to/create a branch with a specific name
$ belly s [some-branch]
Tag and annotate the current commit with a version number
and push the tag to origin
$ belly t 1.4.2
Rename the current branch locally and on origin
$ belly n some-branch
Squash all commits since master
$ belly q -m "Made some awesome changes"
```


Expand Down
10 changes: 5 additions & 5 deletions test.js
@@ -1,6 +1,6 @@
import test from 'ava';
import execa from 'execa';
// import test from 'ava';
// import execa from 'execa';

test('title', async t => {
t.is(await execa.stdout('./cli.js', ['ponies']), 'ponies & rainbows');
});
// test('title', async t => {
// t.is(await execa.stdout('./cli.js', ['ponies']), 'ponies & rainbows');
// });

0 comments on commit 82af319

Please sign in to comment.