Skip to content

Commit 55749b0

Browse files
committed
Simplify cli input
1 parent 36ae237 commit 55749b0

3 files changed

Lines changed: 60 additions & 149 deletions

File tree

bin/browser-sync.js

Lines changed: 58 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,29 @@
11
var startOpts = require("../lib/cli/opts.start.json");
22
var reloadOpts = require("../lib/cli/opts.reload.json");
33
var recipeOpts = require("../lib/cli/opts.recipe.json");
4-
var pkg = require("../package.json");
5-
var utils = require("../lib/utils");
6-
7-
var commands = {
8-
"start": {
9-
command: "start [options]",
10-
description: "Start Browsersync",
11-
builder: startOpts,
12-
handler: function (argv) {
13-
handleCli({cli: {flags: argv, input: ["start"]}});
14-
}
15-
},
16-
"reload": {
17-
command: "reload [options]",
18-
description: "Send a reload event over HTTP protocol",
19-
builder: reloadOpts,
20-
handler: function (argv) {
21-
handleCli({cli: {flags: argv, input: ["reload"]}});
22-
}
23-
},
24-
"init": {
25-
command: "init",
26-
description: "Creates a default config file",
27-
builder: {},
28-
handler: function (argv) {
29-
handleCli({cli: {flags: argv, input: ["init"]}});
30-
}
31-
},
32-
"recipe": {
33-
command: "recipe <recipe-name> [options]",
34-
description: "Generate the files for a recipe",
35-
builder: recipeOpts,
36-
handler: function (argv) {
37-
handleCli({cli: {flags: argv, input: ["recipe", argv["recipe-name"]]}});
38-
}
39-
}
40-
};
4+
var pkg = require("../package.json");
5+
var utils = require("../lib/utils");
416

427
/**
438
* Handle cli input
449
*/
4510
if (!module.parent) {
46-
var yargs = attachCommands(require("yargs"), commands)
47-
.demand(1)
11+
var yargs = require("yargs")
12+
.command("start", "Start the server")
13+
.command("init", "Create a configuration file")
14+
.command("reload", "Send a reload event over HTTP protocol")
15+
.command("recipe", "Generate the files for a recipe")
4816
.version(function () {
4917
return pkg.version;
5018
})
51-
.epilogue("For help running a certain command, type <command> --help\neg: browser-sync start --help");
52-
var argv = yargs.argv;
19+
.epilogue("For help running a certain command, type <command> --help\neg: $0 start --help");
20+
21+
var argv = yargs.argv;
5322
var command = argv._[0];
5423

55-
if (Object.keys(commands).indexOf(command) > -1) {
56-
handleIncoming(commands[command]);
24+
var valid = ["start", "init", "reload", "recipe"];
25+
if (valid.indexOf(command) > -1) {
26+
handleIncoming(command, yargs.reset());
5727
} else {
5828
yargs.showHelp();
5929
}
@@ -71,19 +41,50 @@ function handleCli(opts) {
7141

7242
module.exports = handleCli;
7343

74-
function attachCommands(yargs, commands) {
75-
Object.keys(commands).forEach(function (key) {
76-
yargs.command(key, commands[key].description);
77-
});
78-
return yargs
79-
}
44+
/**
45+
* @param {string} command
46+
* @param {object} yargs
47+
*/
48+
function handleIncoming(command, yargs) {
49+
var out;
50+
if (command === "start") {
51+
out = yargs
52+
.usage("Usage: $0 start [options]")
53+
.options(startOpts)
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")
56+
.help()
57+
.argv;
58+
}
59+
if (command === "init") {
60+
out = yargs
61+
.usage("Usage: $0 init")
62+
.example("$0 init")
63+
.help()
64+
.argv;
65+
}
66+
if (command === "reload") {
67+
out = yargs
68+
.usage("Usage: $0 reload")
69+
.options(reloadOpts)
70+
.example("$0 reload")
71+
.example("$0 reload --port 4000")
72+
.help()
73+
.argv;
74+
}
75+
if (command === "recipe") {
76+
out = yargs
77+
.usage("Usage: $0 recipe <recipe-name>")
78+
.option(recipeOpts)
79+
.example("$0 recipe ls", "list the recipes")
80+
.example("$0 recipe gulp.sass", "use the gulp.sass recipe")
81+
.help()
82+
.argv;
83+
}
8084

81-
function handleIncoming(obj) {
82-
return yargs
83-
.command(obj.command, obj.description, {
84-
builder: obj.builder,
85-
handler: obj.handler
86-
})
87-
.help()
88-
.argv;
85+
if (out.help) {
86+
return yargs.showHelp();
87+
}
88+
89+
handleCli({cli: {flags: out, input: out._}});
8990
}

bin/browser-sync2.js

Lines changed: 0 additions & 90 deletions
This file was deleted.

lib/cli/command.recipe.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ module.exports = function (opts) {
3131
logRecipes();
3232
return opts.cb();
3333
}
34-
35-
if (opts.cli.input[1] === 'ls') {
34+
35+
if (opts.cli.input[1] === "ls") {
3636
logRecipes();
3737
return opts.cb();
3838
}

0 commit comments

Comments
 (0)