Skip to content

Commit

Permalink
check git repository
Browse files Browse the repository at this point in the history
  • Loading branch information
vivaxy committed Nov 22, 2016
1 parent 5465775 commit ff0585a
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 23 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 1.0.0

- check git repository
- optimize help message

# 0.0.0

first version
8 changes: 4 additions & 4 deletions commands/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

export default () => {
console.log(`
Usage: gacp
usage: gacp
gacp
gacp message
gacp help
gacp
gacp <message>
gacp help
`)
};
33 changes: 14 additions & 19 deletions commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,25 @@
*/

import inquirer from 'inquirer';
import chark from 'chalk';
import sh from 'shelljs';

const shellSilentConfig = {
silent: true,
};

const getStdoutFromShell = (command) => {
let result = null;
const shellExec = sh.exec(command, shellSilentConfig);
if (shellExec.code === 0) {
result = shellExec.stdout.split(`\n`)[0];
}
return result;
};
import getInfoFromShell from '../lib/getInfoFromShell';
import isAGitRepository from '../lib/isAGitRepository';
import * as console from '../lib/console';

const getRemote = () => {
return getStdoutFromShell(`git remote`);
return getInfoFromShell(`git remote`);
};

const getBranch = () => {
return getStdoutFromShell(`git symbolic-ref --short HEAD`);
return getInfoFromShell(`git symbolic-ref --short HEAD`);
};

export default async(...restArgs) => {
if (!isAGitRepository()) {
console.error(`not a git repository`);
process.exit(1);
}
let commitMessage = null;
if (restArgs.length === 0) {
const answers = await inquirer.prompt([{
Expand All @@ -37,7 +31,7 @@ export default async(...restArgs) => {
message: `please enter commit message:`,
validate: (msg) => {
if (!msg.length) {
return `commit message is required`
return `commit message is required`;
}
return true;
},
Expand All @@ -47,12 +41,13 @@ export default async(...restArgs) => {
commitMessage = restArgs.join(` `);
}
const addCommand = `git add .`;
console.log(chark.green(addCommand));
console.info(addCommand);
sh.exec(addCommand);
const commitCommand = `git commit -m "${commitMessage}"`;
console.log(chark.green(commitCommand));
console.info(commitCommand);
sh.exec(commitCommand);
const pushCommand = `git push ${getRemote()} ${getBranch()}`;
console.log(chark.green(pushCommand));
console.info(pushCommand);
sh.exec(pushCommand);
return true;
};
14 changes: 14 additions & 0 deletions lib/console.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @since 2016-11-22 16:16
* @author vivaxy
*/

import chalk from 'chalk';

export const info = (...args) => {
console.log(chalk.blue(...args));
};

export const error = (...args) => {
console.log(chalk.red(...args));
};
19 changes: 19 additions & 0 deletions lib/getInfoFromShell.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @since 2016-11-22 16:04
* @author vivaxy
*/

import sh from 'shelljs';

const shellSilentConfig = {
silent: true,
};

export default (command) => {
let result = null;
const shellExec = sh.exec(command, shellSilentConfig);
if (shellExec.code === 0) {
result = shellExec.stdout.split(`\n`)[0];
}
return result;
};
20 changes: 20 additions & 0 deletions lib/isAGitRepository.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @since 2016-11-22 16:05
* @author vivaxy
*/

import path from 'path';
import sh from 'shelljs';

import getInfoFromShell from './getInfoFromShell';

const cwd = process.cwd();

export default () => {
if (sh.test(`-d`, path.join(cwd, `.git`))) {
if (getInfoFromShell(`git rev-parse --is-inside-work-tree`) === `true`) {
return true;
}
}
return false;
};

0 comments on commit ff0585a

Please sign in to comment.