Skip to content

Commit b3198fd

Browse files
committed
yarf
1 parent 2853d47 commit b3198fd

2 files changed

Lines changed: 124 additions & 18 deletions

File tree

bin/browser-sync2.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
var startOpts = require("../lib/cli/opts.start.json");
2+
var reloadOpts = require("../lib/cli/opts.reload.json");
3+
var recipeOpts = require("../lib/cli/opts.recipe.json");
4+
var pkg = require("../package.json");
5+
var utils = require("../lib/utils");
6+
7+
/**
8+
* Handle cli input
9+
*/
10+
if (!module.parent) {
11+
var yargs = require("yargs")
12+
.command('start [options]', 'Start the server')
13+
.command('init', 'Create a configuration file')
14+
.command('reload', 'Send a reload event over HTTP protocol')
15+
.command('recipe <command> [options]', 'Generate the files for a recipe')
16+
.version(function () {
17+
return pkg.version;
18+
})
19+
.epilogue("For help running a certain command, type <command> --help\neg: browser-sync start --help");
20+
21+
var argv = yargs.argv;
22+
var command = argv._[0];
23+
24+
var valid = ['start', 'init', 'reload', 'recipe'];
25+
if (valid.indexOf(command) > -1) {
26+
var out = handleIncoming(command, yargs.reset());
27+
// console.log(out);
28+
} else {
29+
yargs.showHelp();
30+
}
31+
}
32+
33+
/**
34+
* @param {{cli: object, [whitelist]: array, [cb]: function}} opts
35+
* @returns {*}
36+
*/
37+
function handleCli(opts) {
38+
39+
opts.cb = opts.cb || utils.defaultCallback;
40+
return require("../lib/cli/command." + opts.cli.input[0])(opts);
41+
}
42+
43+
module.exports = handleCli;
44+
45+
46+
function handleIncoming(command, yargs) {
47+
if (command === 'start') {
48+
var out = yargs
49+
.command('start [options]')
50+
.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')
53+
.help()
54+
.argv;
55+
if (out.help) {
56+
yargs.showHelp();
57+
} else {
58+
handleCli({cli: {flags: out, input: ["start"]}});
59+
}
60+
}
61+
if (command === 'init') {
62+
var out = yargs
63+
.command('init', 'Generate a configuration file')
64+
.example('browser-sync init')
65+
.help()
66+
.argv;
67+
if (out.help) {
68+
yargs.showHelp();
69+
} else {
70+
handleCli({cli: {flags: out, input: ["init"]}});
71+
}
72+
}
73+
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')
79+
.help()
80+
.argv;
81+
if (out.help) {
82+
yargs.showHelp();
83+
} else {
84+
handleCli({cli: {flags: out, input: ["reload"]}});
85+
}
86+
}
87+
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')
97+
.help()
98+
.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+
}
105+
}
106+
}

lib/cli/opts.start.json

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,26 @@
11
{
2+
"server": {
3+
"alias": "s",
4+
"desc": "Run a Local server (uses your cwd as the web root)"
5+
},
6+
"serveStatic": {
7+
"type": "array",
8+
"alias": "ss",
9+
"desc": "Directories to serve static files from"
10+
},
211
"port": {
312
"type": "number",
413
"desc": "Specify a port to use"
514
},
15+
"proxy": {
16+
"alias": "p",
17+
"desc": "Proxy an existing server",
18+
"example": "$0 shane is cool"
19+
},
20+
"ws": {
21+
"type": "boolean",
22+
"desc": "Proxy mode only - enable websocket proxying"
23+
},
624
"browser": {
725
"type": "array",
826
"alias": "b",
@@ -13,15 +31,6 @@
1331
"alias": "f",
1432
"desc": "File paths to watch"
1533
},
16-
"server": {
17-
"alias": "s",
18-
"desc": "Run a Local server (uses your cwd as the web root)"
19-
},
20-
"serveStatic": {
21-
"type": "array",
22-
"alias": "ss",
23-
"desc": "Directories to serve static files from"
24-
},
2534
"index": {
2635
"type": "string",
2736
"desc": "Specify which file should be used as the index page"
@@ -46,15 +55,6 @@
4655
"type": "boolean",
4756
"desc": "Show a directory listing for the server"
4857
},
49-
"proxy": {
50-
"alias": "p",
51-
"desc": "Proxy an existing server",
52-
"example": "$0 shane is cool"
53-
},
54-
"ws": {
55-
"type": "boolean",
56-
"desc": "Proxy mode only - enable websocket proxying"
57-
},
5858
"xip": {
5959
"type": "boolean",
6060
"desc": "Use xip.io domain routing"

0 commit comments

Comments
 (0)