Skip to content

Commit 36ae237

Browse files
committed
cli: Move handler functions outside try/catch of yargs
1 parent b3198fd commit 36ae237

3 files changed

Lines changed: 37 additions & 52 deletions

File tree

bin/browser-sync2.js

Lines changed: 32 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,21 @@ var utils = require("../lib/utils");
99
*/
1010
if (!module.parent) {
1111
var yargs = require("yargs")
12-
.command('start [options]', 'Start the server')
12+
.command('start', 'Start the server')
1313
.command('init', 'Create a configuration file')
1414
.command('reload', 'Send a reload event over HTTP protocol')
15-
.command('recipe <command> [options]', 'Generate the files for a recipe')
15+
.command('recipe', 'Generate the files for a recipe')
1616
.version(function () {
1717
return pkg.version;
1818
})
19-
.epilogue("For help running a certain command, type <command> --help\neg: browser-sync start --help");
19+
.epilogue("For help running a certain command, type <command> --help\neg: $0 start --help");
2020

2121
var argv = yargs.argv;
2222
var command = argv._[0];
2323

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

4342
module.exports = handleCli;
4443

45-
44+
/**
45+
* @param {string} command
46+
* @param {object} yargs
47+
*/
4648
function handleIncoming(command, yargs) {
49+
var out;
4750
if (command === 'start') {
48-
var out = yargs
49-
.command('start [options]')
51+
out = yargs
52+
.usage("Usage: $0 start [options]")
5053
.options(startOpts)
51-
.example('browser-sync start -s app', '- Use the App directory to serve files')
52-
.example('browser-sync start -p www.bbc.co.uk', '- Proxy an existing website')
54+
.example('$0 start -s app', '- Use the App directory to serve files')
55+
.example('$0 start -p www.bbc.co.uk', '- Proxy an existing website')
5356
.help()
5457
.argv;
55-
if (out.help) {
56-
yargs.showHelp();
57-
} else {
58-
handleCli({cli: {flags: out, input: ["start"]}});
59-
}
6058
}
6159
if (command === 'init') {
62-
var out = yargs
63-
.command('init', 'Generate a configuration file')
64-
.example('browser-sync init')
60+
out = yargs
61+
.usage('Usage: $0 init')
62+
.example('$0 init')
6563
.help()
6664
.argv;
67-
if (out.help) {
68-
yargs.showHelp();
69-
} else {
70-
handleCli({cli: {flags: out, input: ["init"]}});
71-
}
7265
}
7366
if (command === 'reload') {
74-
var out = yargs
75-
.command('reload', 'Send a reload event over HTTP protocol')
76-
.example('browser-sync reload')
77-
.example('browser-sync reload --port 4000')
78-
.example('browser-sync reload --port 4000')
67+
out = yargs
68+
.usage("Usage: $0 reload")
69+
.options(reloadOpts)
70+
.example('$0 reload')
71+
.example('$0 reload --port 4000')
72+
.example('$0 reload --port 4000')
7973
.help()
8074
.argv;
81-
if (out.help) {
82-
yargs.showHelp();
83-
} else {
84-
handleCli({cli: {flags: out, input: ["reload"]}});
85-
}
8675
}
8776
if (command === 'recipe') {
88-
var out = yargs
89-
.command('recipe <recipe-name> [options]', 'Generate the files for a recipe', function (yargs) {
90-
console.log(yargs.argv._[1]);
91-
})
92-
.command('recipe ls', 'List all recipes', function (yargs) {
93-
console.log(yargs.argv._[1]);
94-
})
95-
.example('browser-sync recipe ls', 'list the recipes')
96-
.example('browser-sync recipe gulp.sass', 'use the gulp.sass recipe')
77+
out = yargs
78+
.usage('Usage: $0 recipe <recipe-name>')
79+
.option(recipeOpts)
80+
.example('$0 recipe ls', 'list the recipes')
81+
.example('$0 recipe gulp.sass', 'use the gulp.sass recipe')
9782
.help()
9883
.argv;
99-
if (out.help) {
100-
yargs.showHelp();
101-
} else {
102-
console.log('HERE', out);
103-
// handleCli({cli: {flags: out, input: ["recipe", out["recipe-name"]]}});
104-
}
10584
}
85+
if (out.help) {
86+
return yargs.showHelp();
87+
}
88+
89+
handleCli({cli: {flags: out, input: out._}});
10690
}

lib/cli/command.recipe.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ module.exports = function (opts) {
3131
logRecipes();
3232
return opts.cb();
3333
}
34+
35+
if (opts.cli.input[1] === 'ls') {
36+
logRecipes();
37+
return opts.cb();
38+
}
3439

3540
input = input[0];
3641
var flags = opts.cli.flags;

lib/cli/opts.recipe.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,5 @@
22
"output": {
33
"alias": "o",
44
"desc": "Specify an output directory"
5-
},
6-
"list": {
7-
"alias": "ls",
8-
"desc": "Show all available recipes"
95
}
106
}

0 commit comments

Comments
 (0)