Skip to content

Commit

Permalink
chore: move all option mods to single pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
shakyShane committed Dec 29, 2017
1 parent dbb1267 commit 0f8c240
Show file tree
Hide file tree
Showing 11 changed files with 175 additions and 128 deletions.
2 changes: 1 addition & 1 deletion lib/browser-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ BrowserSync.prototype.init = function(options, cb) {
* user may of provided
* @type {Map}
*/
bs.options = require("./options").update(options);
bs.options = options;

/**
* Kick off default plugins.
Expand Down
32 changes: 30 additions & 2 deletions lib/cli/cli-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,22 @@ import { handleProxyOption } from "./transforms/handleProxyOption";
import { handleServerOption } from "./transforms/handleServerOption";
import { appendServerIndexOption } from "./transforms/appendServerIndexOption";
import { appendServerDirectoryOption } from "./transforms/appendServerDirectoryOption";
import {addCwdToWatchOptions} from "./transforms/addCwdToWatchOptions";
import { addCwdToWatchOptions } from "./transforms/addCwdToWatchOptions";
import {
setMode,
setScheme,
setStartPath,
setProxyWs,
setServerOpts,
liftExtensionsOptionFromCli,
setNamespace,
fixSnippetIgnorePaths,
fixSnippetIncludePaths,
fixRewriteRules,
setMiddleware,
setOpen,
setUiPort
} from "../options";

const _ = require("../lodash.custom");
const defaultConfig = require("../default-config");
Expand All @@ -34,7 +49,20 @@ export function merge(input) {
handlePortsOption,
handleGhostModeOption,
handleFilesOption,
handleExtensionsOption
handleExtensionsOption,
setMode,
setScheme,
setStartPath,
setProxyWs,
setServerOpts,
liftExtensionsOptionFromCli,
setNamespace,
fixSnippetIgnorePaths,
fixSnippetIncludePaths,
fixRewriteRules,
setMiddleware,
setOpen,
setUiPort
];

const output = transforms.reduce((acc, item) => {
Expand Down
170 changes: 87 additions & 83 deletions lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,64 +4,44 @@ var _ = require("./lodash.custom");
var Immutable = require("immutable");
var defaultConfig = require("./default-config");

/**
* @param {Map} options
* @returns {Map}
*/
module.exports.update = function(options) {
return options.withMutations(function(item) {
setMode(item);
setScheme(item);
setStartPath(item);
setProxyWs(item);
setServerOpts(item);
setNamespace(item);
fixSnippetOptions(item);
fixRewriteRules(item);
setMiddleware(item);
setOpen(item);

if (item.get("uiPort")) {
item.setIn(["ui", "port"], item.get("uiPort"));
}
});
};

/**
* Move top-level ws options to proxy.ws
* This is to allow it to be set from the CLI
* @param item
* @param incoming
*/
function setProxyWs(item) {
if (item.get("ws") && item.get("mode") === "proxy") {
item.setIn(["proxy", "ws"], true);
export function setProxyWs(incoming) {
if (incoming.get("ws") && incoming.get("mode") === "proxy") {
return incoming.setIn(["proxy", "ws"], true);
}
return incoming;
}

/**
* @param item
*/
function setOpen(item) {
var open = item.get("open");
if (item.get("mode") === "snippet") {
if (open !== "ui" && open !== "ui-external") {
item.set("open", false);
export function setOpen(item) {
return item.update('open', function(open) {
if (item.get("mode") === "snippet") {
if (open !== "ui" && open !== "ui-external") {
return false;
}
}
}
return open;
});
}

/**
* Set the running mode
* @param item
* @param incoming
*/
function setMode(item) {
item.set(
export function setMode(incoming) {
return incoming.set(
"mode",
(function() {
if (item.get("server")) {
if (incoming.get("server")) {
return "server";
}
if (item.get("proxy")) {
if (incoming.get("proxy")) {
return "proxy";
}
return "snippet";
Expand All @@ -70,118 +50,132 @@ function setMode(item) {
}

/**
* @param item
* @param incoming
*/
function setScheme(item) {
export function setScheme(incoming) {
var scheme = "http";

if (item.getIn(["server", "https"])) {
if (incoming.getIn(["server", "https"])) {
scheme = "https";
}

if (item.get("https")) {
if (incoming.get("https")) {
scheme = "https";
}

if (item.getIn(["proxy", "url", "protocol"])) {
if (item.getIn(["proxy", "url", "protocol"]) === "https:") {
if (incoming.getIn(["proxy", "url", "protocol"])) {
if (incoming.getIn(["proxy", "url", "protocol"]) === "https:") {
scheme = "https";
}
}

item.set("scheme", scheme);
return incoming.set("scheme", scheme);
}

/**
* @param item
* @param incoming
*/
function setStartPath(item) {
if (item.get("proxy")) {
var path = item.getIn(["proxy", "url", "path"]);
export function setStartPath(incoming) {
if (incoming.get("proxy")) {
var path = incoming.getIn(["proxy", "url", "path"]);
if (path !== "/") {
item.set("startPath", path);
return incoming.set("startPath", path);
}
}
return incoming;
}

/**
* @param item
*/
function setNamespace(item) {
export function setNamespace(item) {
var namespace = item.getIn(["socket", "namespace"]);

if (_.isFunction(namespace)) {
item.setIn(
return item.setIn(
["socket", "namespace"],
namespace(defaultConfig.socket.namespace)
);
}
return item;
}

/**
* @param item
*/
function setServerOpts(item) {
if (item.get("server")) {
var indexarg =
item.getIn(["server", "index"]) ||
"index.html";
var optPath = ["server", "serveStaticOptions"];

if (!item.getIn(optPath)) {
item.setIn(
optPath,
Immutable.Map({
index: indexarg
})
);
} else {
if (!item.hasIn(optPath.concat(["index"]))) {
item.setIn(optPath.concat(["index"]), indexarg);
}
}
export function setServerOpts(item) {
if (!item.get("server")) {
return item;
}
var indexarg =
item.getIn(["server", "index"]) ||
"index.html";
var optPath = ["server", "serveStaticOptions"];

if (!item.getIn(optPath)) {
return item.setIn(
optPath,
Immutable.Map({
index: indexarg
})
);
}
if (!item.hasIn(optPath.concat(["index"]))) {
return item.setIn(optPath.concat(["index"]), indexarg);
}

// cli extensions
if (item.get("extensions")) {
item.setIn(optPath.concat(["extensions"]), item.get("extensions"));
}
return item;
}

export function liftExtensionsOptionFromCli(item) {
// cli extensions
var optPath = ["server", "serveStaticOptions"];
if (item.get("extensions")) {
return item.setIn(optPath.concat(["extensions"]), item.get("extensions"));
}
return item;
}

/**
* Back-compat fixes for rewriteRules being set to a boolean
*/
function fixRewriteRules(item) {
export function fixRewriteRules(item) {
return item.update("rewriteRules", function(rr) {
return Immutable.List([])
.concat(rr)
.filter(Boolean);
});
}

function fixSnippetOptions(item) {
export function fixSnippetIgnorePaths(item) {
var ignorePaths = item.getIn(["snippetOptions", "ignorePaths"]);
var includePaths = item.getIn(["snippetOptions", "whitelist"]);

if (ignorePaths) {
if (_.isString(ignorePaths)) {
ignorePaths = [ignorePaths];
}
ignorePaths = ignorePaths.map(ensureSlash);
item.setIn(
return item.setIn(
["snippetOptions", "blacklist"],
Immutable.List(ignorePaths)
);
}
return item;
}

export function fixSnippetIncludePaths(item) {
var includePaths = item.getIn(["snippetOptions", "whitelist"]);
if (includePaths) {
includePaths = includePaths.map(ensureSlash);
item.setIn(
return item.setIn(
["snippetOptions", "whitelist"],
Immutable.List(includePaths)
);
}
return item;
}


/**
* Enforce paths to begin with a forward slash
*/
Expand All @@ -195,10 +189,9 @@ function ensureSlash(item) {
/**
*
*/
function setMiddleware(item) {
export function setMiddleware(item) {
var mw = getMiddlwares(item);

item.set("middleware", mw);
return item.set("middleware", mw);
}

/**
Expand Down Expand Up @@ -252,3 +245,14 @@ function listMerge(list, item) {

return list;
}

/**
* @param item
* @returns {*}
*/
export function setUiPort(item) {
if (item.get("uiPort")) {
return item.setIn(["ui", "port"], item.get("uiPort"));
}
return item;
}
2 changes: 1 addition & 1 deletion lib/server/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ var serverUtils = {
defaultMiddlewares.unshift({
id: "Browsersync SPA support",
route: "",
handle: require('connect-history-api-fallback')()
handle: require("connect-history-api-fallback")()
});
}

Expand Down
11 changes: 8 additions & 3 deletions test/specs/cli/cli.options.ignore.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ var assert = require("chai").assert;

describe("CLI: Options: dealing with 'ignore' option", function() {
it("watches in server mode (no files given)", function() {
var cwd = '/Users/shakyshane/app';
var input = { server: true, files: ['**/*'], ignore: ['**/*.php'], cwd: cwd };
var cwd = "/Users/shakyshane/app";
var input = {
server: true,
files: ["**/*"],
ignore: ["**/*.php"],
cwd: cwd
};
var config = merge(input).toJS();
assert.deepEqual(config.files, { core: { globs: ["**/*"], objs: [] } });
assert.ok(config.watchOptions.ignored.indexOf('**/*.php') > -1);
assert.ok(config.watchOptions.ignored.indexOf("**/*.php") > -1);
});
});
Loading

0 comments on commit 0f8c240

Please sign in to comment.