Skip to content

Commit

Permalink
Refactory to extract functions
Browse files Browse the repository at this point in the history
  • Loading branch information
robsonbittencourt committed Nov 15, 2016
1 parent c933759 commit fea769c
Showing 1 changed file with 43 additions and 31 deletions.
74 changes: 43 additions & 31 deletions src/cli/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,54 @@ const STOP_COMMAND = 'stop';
const RESTART_COMMAND = 'restart';
const CONFIGURE_COMMAND = 'configure';

const argv = yargs.usage(`Hubot.js \n
Usage: hubot <command> [options]`)
.command(START_COMMAND, 'Start the hubot')
.command(STOP_COMMAND, 'Stop the hubot')
.command(RESTART_COMMAND, 'Restart the hubot')
.command(CONFIGURE_COMMAND, 'Restart the hubot')
.alias('t', 'token')
.alias('n', 'name')
.describe('t', 'Inform Slack API Token')
.describe('n', 'Inform the bot name')
.argv;

const command = argv._[0];

switch (command) {
case START_COMMAND:
executeProcessManager(start, argv);
break;
case STOP_COMMAND:
executeProcessManager(stop);
break;
case RESTART_COMMAND:
executeProcessManager(restart);
break;
case CONFIGURE_COMMAND:
configure.configure(argv);
break;
default:
yargs.showHelp();
process.exit(0);
break;
createParameters();
createHelpMessage();
chooseCommand();

function chooseCommand() {
const argv = yargs.argv;
const command = argv._[0];

switch (command) {
case START_COMMAND:
executeProcessManager(start, argv);
break;
case STOP_COMMAND:
executeProcessManager(stop);
break;
case RESTART_COMMAND:
executeProcessManager(restart);
break;
case CONFIGURE_COMMAND:
configure.configure(argv);
break;
default:
yargs.showHelp();
process.exit(0);
break;
}
}

function createParameters() {
yargs.alias('t', 'token')
.alias('n', 'name');
}


function createHelpMessage() {
yargs.usage(`Hubot.js \nUsage: hubot <command> [options]`)
.command(START_COMMAND, 'Start the hubot')
.command(STOP_COMMAND, 'Stop the hubot')
.command(RESTART_COMMAND, 'Restart the hubot')
.command(CONFIGURE_COMMAND, 'Restart the hubot')
.describe('t', 'Inform Slack API Token')
.describe('n', 'Inform the bot name');
}

function start(args) {
const argsArray = buildArgsArray(args);
const config = { script: 'src/cli/init-core.js', name: 'hubot', args: argsArray, maxRestarts: 2 };

pm2.start(config, () => pm2.disconnect());
}

Expand Down

0 comments on commit fea769c

Please sign in to comment.