Skip to content

Commit 11a5825

Browse files
committed
Merge branch 'feature/backport-proxy'
2 parents ac62359 + 8572f06 commit 11a5825

44 files changed

Lines changed: 1301 additions & 687 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

bin/browser-sync.js

Lines changed: 69 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,90 @@
1-
#!/usr/bin/env node
2-
"use strict";
3-
4-
var meow = require("meow");
5-
var fs = require("fs");
6-
var path = require("path");
7-
var compile = require("eazy-logger").compile;
8-
var longest = require("longest");
9-
var utils = require("../lib/utils");
10-
var logger = require("../lib/logger").logger;
11-
var cmdWhitelist = ["start", "init", "reload", "recipe"];
12-
13-
var cli = meow({
14-
pkg: "../package.json",
15-
help: getHelpText(path.join(__dirname, "../lib/cli/help.txt"))
16-
});
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");
176

187
/**
198
* Handle cli input
209
*/
2110
if (!module.parent) {
22-
handleCli({cli: cli, whitelist: cmdWhitelist});
23-
}
24-
25-
/**
26-
* Generate & colour the help text
27-
* @param {String} filepath - relative file path to the help text
28-
* @returns {String}
29-
*/
30-
function getHelpText(filepath) {
31-
32-
/**
33-
* Help text template
34-
*/
35-
var template = fs.readFileSync(filepath, "utf8");
36-
37-
cmdWhitelist.forEach(function (command) {
38-
39-
var flags = require("../lib/cli/opts." + command + ".json");
40-
template = template.replace("%" + command + "flags%", listFlags(flags));
41-
});
42-
43-
return compile(template);
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")
16+
.version(function () {
17+
return pkg.version;
18+
})
19+
.epilogue("For help running a certain command, type <command> --help\neg: $0 start --help");
20+
21+
var argv = yargs.argv;
22+
var command = argv._[0];
23+
var valid = ["start", "init", "reload", "recipe"];
24+
25+
if (valid.indexOf(command) > -1) {
26+
handleIncoming(command, yargs.reset());
27+
} else {
28+
yargs.showHelp();
29+
}
4430
}
4531

4632
/**
4733
* @param {{cli: object, [whitelist]: array, [cb]: function}} opts
4834
* @returns {*}
4935
*/
50-
function handleCli (opts) {
36+
function handleCli(opts) {
5137

5238
opts.cb = opts.cb || utils.defaultCallback;
39+
return require("../lib/cli/command." + opts.cli.input[0])(opts);
40+
}
5341

54-
var input = opts.cli.input;
42+
module.exports = handleCli;
5543

56-
if (!opts.whitelist) {
57-
opts.whitelist = cmdWhitelist;
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;
5858
}
59-
60-
if (!input.length || opts.whitelist.indexOf(input[0]) === -1) {
61-
return console.log(opts.cli.help);
59+
if (command === "init") {
60+
out = yargs
61+
.usage("Usage: $0 init")
62+
.example("$0 init")
63+
.help()
64+
.argv;
6265
}
63-
64-
if (!require("../lib/cli/cli-utils").verifyOpts(input[0], opts.cli.flags)) {
65-
logger.info("For help, run: {cyan:browser-sync --help}");
66-
return opts.cb(new Error("Unknown flag given. Please refer to the documentation for help."));
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;
6783
}
6884

69-
return require("../lib/cli/command." + input[0])(opts);
70-
}
71-
72-
/**
73-
* @param {Object} flags
74-
*/
75-
function listFlags (flags) {
76-
77-
var flagKeys = Object.keys(flags);
78-
var maxLength = (longest(Object.keys(flags)) || "").length + 4;
79-
80-
return flagKeys.map(function (item) {
81-
var length = maxLength - item.length;
82-
return " {bold:--" + item + chars(length, " ") + "}" +
83-
flags[item];
84-
}).join("\n");
85-
}
85+
if (out.help) {
86+
return yargs.showHelp();
87+
}
8688

87-
function chars (length, char) {
88-
return new Array(length).join(char);
89+
handleCli({cli: {flags: out, input: out._}});
8990
}
90-
91-
module.exports = handleCli;
92-
module.exports.getHelpText = getHelpText;

lib/async.js

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var _ = require("lodash");
44
var Immutable = require("immutable");
55

66
var utils = require("./utils");
7+
var pluginUtils = require("./plugins");
78
var connectUtils = require("./connect-utils");
89

910
module.exports = {
@@ -110,29 +111,22 @@ module.exports = {
110111
*/
111112
resolveInlineUserPlugins: function (bs, done) {
112113

113-
bs.options.get("plugins").forEach(function (item) {
114+
var plugins = bs.options
115+
.get("plugins")
116+
.map(pluginUtils.resolvePlugin)
117+
.map(pluginUtils.requirePlugin);
114118

115-
if (_.isString(item)) {
116-
loadPlugin(item);
117-
}
118-
119-
if (Immutable.Map.isMap(item)) {
120-
if (item.has("module")) {
121-
loadPlugin(item.get("module"), item.get("options"));
122-
} else {
123-
loadPlugin(item);
119+
plugins
120+
.forEach(function (plugin) {
121+
if (plugin.get("errors").size) {
122+
return logPluginError(plugin);
124123
}
125-
}
126-
});
124+
var jsPlugin = plugin.toJS();
125+
bs.registerPlugin(jsPlugin.module, jsPlugin.options);
126+
});
127127

128-
function loadPlugin (name, opts) {
129-
opts = opts ? opts.toJS() : {};
130-
if (_.isString(name)) {
131-
opts.moduleName = name;
132-
bs.registerPlugin(require(name), opts);
133-
} else {
134-
bs.registerPlugin(name.toJS(), opts);
135-
}
128+
function logPluginError (plugin) {
129+
utils.fail(true, new Error("Plugin: " + plugin.get("name") + " not found"), bs.cb);
136130
}
137131

138132
done();
@@ -330,7 +324,10 @@ module.exports = {
330324
bs.addMiddleware(
331325
require("./config").httpProtocol.path,
332326
require("./http-protocol").middleware(bs),
333-
{override: true}
327+
{
328+
override: true,
329+
id: "Browsersync HTTP Protocol"
330+
}
334331
);
335332

336333
done();

lib/browser-sync.js

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var connectUtils = require("./connect-utils");
77
var utils = require("./utils");
88
var logger = require("./logger");
99

10-
var eachSeries = require("async-each-series");
10+
var eachSeries = utils.eachSeries;
1111
var _ = require("lodash");
1212
var EE = require("easy-extender");
1313

@@ -364,24 +364,41 @@ BrowserSync.prototype.getMiddleware = function (type) {
364364
* @param {String} path
365365
* @param {{type: string, content: string}} props
366366
*/
367+
var _serveFileCount = 0;
367368
BrowserSync.prototype.serveFile = function (path, props) {
368369

369370
var bs = this;
370-
371-
if (bs.app) {
372-
bs.app.use(path, function (req, res) {
371+
var mode = bs.options.get("mode");
372+
var entry = {
373+
handle: function (req, res) {
373374
res.setHeader("Content-Type", props.type);
374375
res.end(props.content);
375-
});
376+
},
377+
id: "Browsersync - " + _serveFileCount++,
378+
route: path
379+
};
380+
381+
if (!bs.app) {
382+
return;
376383
}
384+
385+
bs._addMiddlewareToStack(entry);
377386
};
378387

379388
/**
380389
* Add middlewares on the fly
381-
* @param route
382-
* @param handle
383-
* @param opts
390+
* @param {{route: string, handle: function, id?: string}}
384391
*/
392+
BrowserSync.prototype._addMiddlewareToStack = function (entry) {
393+
var bs = this;
394+
if (bs.options.get("mode") === "proxy") {
395+
bs.app.stack.splice(bs.app.stack.length-1, 0, entry);
396+
} else {
397+
bs.app.stack.push(entry);
398+
}
399+
};
400+
401+
var _addMiddlewareCount = 0;
385402
BrowserSync.prototype.addMiddleware = function (route, handle, opts) {
386403

387404
var bs = this;
@@ -390,31 +407,27 @@ BrowserSync.prototype.addMiddleware = function (route, handle, opts) {
390407
return;
391408
}
392409

393-
var mode = bs.options.get("mode");
394-
395410
opts = opts || {};
396411

397412
if (!opts.id) {
398-
opts.id = "bs-mw-" + Math.random();
413+
opts.id = "bs-mw-" + _addMiddlewareCount++;
399414
}
400415

401416
if (route === "*") {
402417
route = "";
403418
}
404419

405-
if (opts.override) {
406-
return bs.app.stack.unshift({id: opts.id, route: route, handle: handle});
407-
}
408-
409-
if (mode === "proxy") {
410-
return bs.app.use(route, handle, opts);
411-
}
412-
413-
return bs.app.stack.push({
420+
var entry = {
414421
id: opts.id,
415422
route: route,
416423
handle: handle
417-
}); // function + route;
424+
};
425+
426+
if (opts.override) {
427+
return bs.app.stack.unshift(entry);
428+
}
429+
430+
bs._addMiddlewareToStack(entry);
418431
};
419432

420433
/**
@@ -611,14 +624,7 @@ BrowserSync.prototype.setRewriteRules = function (rules) {
611624
var bs = this;
612625

613626
bs.rewriteRules = rules;
614-
615-
if (bs.options.get("mode") === "server") {
616-
bs.snippetMw.opts.rules = rules;
617-
}
618-
619-
if (bs.options.get("mode") === "proxy") {
620-
bs.proxy.config.rules = rules;
621-
}
627+
bs.snippetMw.opts.rules = rules;
622628
};
623629

624630
/**

lib/cli/cli-utils.js

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

lib/cli/command.recipe.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,24 @@ module.exports = function (opts) {
1919
var dir = path.dirname(resolved);
2020

2121
var logRecipes = function () {
22-
var dirs = fs.readdirSync(path.join(dir, "recipes"), function (err, output) {
23-
});
22+
var dirs = fs.readdirSync(path.join(dir, "recipes"));
2423
logger.info("Install one of the following with {cyan:browser-sync recipe <name>\n");
2524
dirs.forEach(function (name) {
2625
console.log(" " + name);
2726
});
28-
}
27+
};
2928

3029
if (!input.length) {
3130
logger.info("No recipe name provided!");
3231
logRecipes();
3332
return opts.cb();
3433
}
3534

35+
if (opts.cli.input[1] === "ls") {
36+
logRecipes();
37+
return opts.cb();
38+
}
39+
3640
input = input[0];
3741
var flags = opts.cli.flags;
3842
var output = flags.output ? path.resolve(flags.output) : path.join(process.cwd(), input);

0 commit comments

Comments
 (0)