Skip to content

Commit

Permalink
cli: Move handler functions outside try/catch of yargs
Browse files Browse the repository at this point in the history
  • Loading branch information
shakyShane committed Apr 6, 2016
1 parent b3198fd commit 36ae237
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 52 deletions.
80 changes: 32 additions & 48 deletions bin/browser-sync2.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,21 @@ var utils = require("../lib/utils");
*/
if (!module.parent) {
var yargs = require("yargs")
.command('start [options]', 'Start the server')
.command('start', 'Start the server')
.command('init', 'Create a configuration file')
.command('reload', 'Send a reload event over HTTP protocol')
.command('recipe <command> [options]', 'Generate the files for a recipe')
.command('recipe', 'Generate the files for a recipe')
.version(function () {
return pkg.version;
})
.epilogue("For help running a certain command, type <command> --help\neg: browser-sync start --help");
.epilogue("For help running a certain command, type <command> --help\neg: $0 start --help");

var argv = yargs.argv;
var command = argv._[0];

var valid = ['start', 'init', 'reload', 'recipe'];
if (valid.indexOf(command) > -1) {
var out = handleIncoming(command, yargs.reset());
// console.log(out);
handleIncoming(command, yargs.reset());
} else {
yargs.showHelp();
}
Expand All @@ -42,65 +41,50 @@ function handleCli(opts) {

module.exports = handleCli;


/**
* @param {string} command
* @param {object} yargs
*/
function handleIncoming(command, yargs) {
var out;
if (command === 'start') {
var out = yargs
.command('start [options]')
out = yargs
.usage("Usage: $0 start [options]")
.options(startOpts)
.example('browser-sync start -s app', '- Use the App directory to serve files')
.example('browser-sync start -p www.bbc.co.uk', '- Proxy an existing website')
.example('$0 start -s app', '- Use the App directory to serve files')
.example('$0 start -p www.bbc.co.uk', '- Proxy an existing website')
.help()
.argv;
if (out.help) {
yargs.showHelp();
} else {
handleCli({cli: {flags: out, input: ["start"]}});
}
}
if (command === 'init') {
var out = yargs
.command('init', 'Generate a configuration file')
.example('browser-sync init')
out = yargs
.usage('Usage: $0 init')
.example('$0 init')
.help()
.argv;
if (out.help) {
yargs.showHelp();
} else {
handleCli({cli: {flags: out, input: ["init"]}});
}
}
if (command === 'reload') {
var out = yargs
.command('reload', 'Send a reload event over HTTP protocol')
.example('browser-sync reload')
.example('browser-sync reload --port 4000')
.example('browser-sync reload --port 4000')
out = yargs
.usage("Usage: $0 reload")
.options(reloadOpts)
.example('$0 reload')
.example('$0 reload --port 4000')
.example('$0 reload --port 4000')
.help()
.argv;
if (out.help) {
yargs.showHelp();
} else {
handleCli({cli: {flags: out, input: ["reload"]}});
}
}
if (command === 'recipe') {
var out = yargs
.command('recipe <recipe-name> [options]', 'Generate the files for a recipe', function (yargs) {
console.log(yargs.argv._[1]);
})
.command('recipe ls', 'List all recipes', function (yargs) {
console.log(yargs.argv._[1]);
})
.example('browser-sync recipe ls', 'list the recipes')
.example('browser-sync recipe gulp.sass', 'use the gulp.sass recipe')
out = yargs
.usage('Usage: $0 recipe <recipe-name>')
.option(recipeOpts)
.example('$0 recipe ls', 'list the recipes')
.example('$0 recipe gulp.sass', 'use the gulp.sass recipe')
.help()
.argv;
if (out.help) {
yargs.showHelp();
} else {
console.log('HERE', out);
// handleCli({cli: {flags: out, input: ["recipe", out["recipe-name"]]}});
}
}
if (out.help) {
return yargs.showHelp();
}

handleCli({cli: {flags: out, input: out._}});
}
5 changes: 5 additions & 0 deletions lib/cli/command.recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ module.exports = function (opts) {
logRecipes();
return opts.cb();
}

if (opts.cli.input[1] === 'ls') {
logRecipes();
return opts.cb();
}

input = input[0];
var flags = opts.cli.flags;
Expand Down
4 changes: 0 additions & 4 deletions lib/cli/opts.recipe.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,5 @@
"output": {
"alias": "o",
"desc": "Specify an output directory"
},
"list": {
"alias": "ls",
"desc": "Show all available recipes"
}
}

0 comments on commit 36ae237

Please sign in to comment.