Skip to content

Commit 0f8c240

Browse files
committed
chore: move all option mods to single pipeline
1 parent dbb1267 commit 0f8c240

File tree

11 files changed

+175
-128
lines changed

11 files changed

+175
-128
lines changed

lib/browser-sync.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ BrowserSync.prototype.init = function(options, cb) {
100100
* user may of provided
101101
* @type {Map}
102102
*/
103-
bs.options = require("./options").update(options);
103+
bs.options = options;
104104

105105
/**
106106
* Kick off default plugins.

lib/cli/cli-options.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,22 @@ import { handleProxyOption } from "./transforms/handleProxyOption";
1010
import { handleServerOption } from "./transforms/handleServerOption";
1111
import { appendServerIndexOption } from "./transforms/appendServerIndexOption";
1212
import { appendServerDirectoryOption } from "./transforms/appendServerDirectoryOption";
13-
import {addCwdToWatchOptions} from "./transforms/addCwdToWatchOptions";
13+
import { addCwdToWatchOptions } from "./transforms/addCwdToWatchOptions";
14+
import {
15+
setMode,
16+
setScheme,
17+
setStartPath,
18+
setProxyWs,
19+
setServerOpts,
20+
liftExtensionsOptionFromCli,
21+
setNamespace,
22+
fixSnippetIgnorePaths,
23+
fixSnippetIncludePaths,
24+
fixRewriteRules,
25+
setMiddleware,
26+
setOpen,
27+
setUiPort
28+
} from "../options";
1429

1530
const _ = require("../lodash.custom");
1631
const defaultConfig = require("../default-config");
@@ -34,7 +49,20 @@ export function merge(input) {
3449
handlePortsOption,
3550
handleGhostModeOption,
3651
handleFilesOption,
37-
handleExtensionsOption
52+
handleExtensionsOption,
53+
setMode,
54+
setScheme,
55+
setStartPath,
56+
setProxyWs,
57+
setServerOpts,
58+
liftExtensionsOptionFromCli,
59+
setNamespace,
60+
fixSnippetIgnorePaths,
61+
fixSnippetIncludePaths,
62+
fixRewriteRules,
63+
setMiddleware,
64+
setOpen,
65+
setUiPort
3866
];
3967

4068
const output = transforms.reduce((acc, item) => {

lib/options.js

Lines changed: 87 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -4,64 +4,44 @@ var _ = require("./lodash.custom");
44
var Immutable = require("immutable");
55
var defaultConfig = require("./default-config");
66

7-
/**
8-
* @param {Map} options
9-
* @returns {Map}
10-
*/
11-
module.exports.update = function(options) {
12-
return options.withMutations(function(item) {
13-
setMode(item);
14-
setScheme(item);
15-
setStartPath(item);
16-
setProxyWs(item);
17-
setServerOpts(item);
18-
setNamespace(item);
19-
fixSnippetOptions(item);
20-
fixRewriteRules(item);
21-
setMiddleware(item);
22-
setOpen(item);
23-
24-
if (item.get("uiPort")) {
25-
item.setIn(["ui", "port"], item.get("uiPort"));
26-
}
27-
});
28-
};
29-
307
/**
318
* Move top-level ws options to proxy.ws
329
* This is to allow it to be set from the CLI
33-
* @param item
10+
* @param incoming
3411
*/
35-
function setProxyWs(item) {
36-
if (item.get("ws") && item.get("mode") === "proxy") {
37-
item.setIn(["proxy", "ws"], true);
12+
export function setProxyWs(incoming) {
13+
if (incoming.get("ws") && incoming.get("mode") === "proxy") {
14+
return incoming.setIn(["proxy", "ws"], true);
3815
}
16+
return incoming;
3917
}
4018

4119
/**
4220
* @param item
4321
*/
44-
function setOpen(item) {
45-
var open = item.get("open");
46-
if (item.get("mode") === "snippet") {
47-
if (open !== "ui" && open !== "ui-external") {
48-
item.set("open", false);
22+
export function setOpen(item) {
23+
return item.update('open', function(open) {
24+
if (item.get("mode") === "snippet") {
25+
if (open !== "ui" && open !== "ui-external") {
26+
return false;
27+
}
4928
}
50-
}
29+
return open;
30+
});
5131
}
5232

5333
/**
5434
* Set the running mode
55-
* @param item
35+
* @param incoming
5636
*/
57-
function setMode(item) {
58-
item.set(
37+
export function setMode(incoming) {
38+
return incoming.set(
5939
"mode",
6040
(function() {
61-
if (item.get("server")) {
41+
if (incoming.get("server")) {
6242
return "server";
6343
}
64-
if (item.get("proxy")) {
44+
if (incoming.get("proxy")) {
6545
return "proxy";
6646
}
6747
return "snippet";
@@ -70,118 +50,132 @@ function setMode(item) {
7050
}
7151

7252
/**
73-
* @param item
53+
* @param incoming
7454
*/
75-
function setScheme(item) {
55+
export function setScheme(incoming) {
7656
var scheme = "http";
7757

78-
if (item.getIn(["server", "https"])) {
58+
if (incoming.getIn(["server", "https"])) {
7959
scheme = "https";
8060
}
8161

82-
if (item.get("https")) {
62+
if (incoming.get("https")) {
8363
scheme = "https";
8464
}
8565

86-
if (item.getIn(["proxy", "url", "protocol"])) {
87-
if (item.getIn(["proxy", "url", "protocol"]) === "https:") {
66+
if (incoming.getIn(["proxy", "url", "protocol"])) {
67+
if (incoming.getIn(["proxy", "url", "protocol"]) === "https:") {
8868
scheme = "https";
8969
}
9070
}
9171

92-
item.set("scheme", scheme);
72+
return incoming.set("scheme", scheme);
9373
}
9474

9575
/**
96-
* @param item
76+
* @param incoming
9777
*/
98-
function setStartPath(item) {
99-
if (item.get("proxy")) {
100-
var path = item.getIn(["proxy", "url", "path"]);
78+
export function setStartPath(incoming) {
79+
if (incoming.get("proxy")) {
80+
var path = incoming.getIn(["proxy", "url", "path"]);
10181
if (path !== "/") {
102-
item.set("startPath", path);
82+
return incoming.set("startPath", path);
10383
}
10484
}
85+
return incoming;
10586
}
10687

10788
/**
10889
* @param item
10990
*/
110-
function setNamespace(item) {
91+
export function setNamespace(item) {
11192
var namespace = item.getIn(["socket", "namespace"]);
11293

11394
if (_.isFunction(namespace)) {
114-
item.setIn(
95+
return item.setIn(
11596
["socket", "namespace"],
11697
namespace(defaultConfig.socket.namespace)
11798
);
11899
}
100+
return item;
119101
}
120102

121103
/**
122104
* @param item
123105
*/
124-
function setServerOpts(item) {
125-
if (item.get("server")) {
126-
var indexarg =
127-
item.getIn(["server", "index"]) ||
128-
"index.html";
129-
var optPath = ["server", "serveStaticOptions"];
130-
131-
if (!item.getIn(optPath)) {
132-
item.setIn(
133-
optPath,
134-
Immutable.Map({
135-
index: indexarg
136-
})
137-
);
138-
} else {
139-
if (!item.hasIn(optPath.concat(["index"]))) {
140-
item.setIn(optPath.concat(["index"]), indexarg);
141-
}
142-
}
106+
export function setServerOpts(item) {
107+
if (!item.get("server")) {
108+
return item;
109+
}
110+
var indexarg =
111+
item.getIn(["server", "index"]) ||
112+
"index.html";
113+
var optPath = ["server", "serveStaticOptions"];
114+
115+
if (!item.getIn(optPath)) {
116+
return item.setIn(
117+
optPath,
118+
Immutable.Map({
119+
index: indexarg
120+
})
121+
);
122+
}
123+
if (!item.hasIn(optPath.concat(["index"]))) {
124+
return item.setIn(optPath.concat(["index"]), indexarg);
125+
}
143126

144-
// cli extensions
145-
if (item.get("extensions")) {
146-
item.setIn(optPath.concat(["extensions"]), item.get("extensions"));
147-
}
127+
return item;
128+
}
129+
130+
export function liftExtensionsOptionFromCli(item) {
131+
// cli extensions
132+
var optPath = ["server", "serveStaticOptions"];
133+
if (item.get("extensions")) {
134+
return item.setIn(optPath.concat(["extensions"]), item.get("extensions"));
148135
}
136+
return item;
149137
}
150138

151139
/**
152140
* Back-compat fixes for rewriteRules being set to a boolean
153141
*/
154-
function fixRewriteRules(item) {
142+
export function fixRewriteRules(item) {
155143
return item.update("rewriteRules", function(rr) {
156144
return Immutable.List([])
157145
.concat(rr)
158146
.filter(Boolean);
159147
});
160148
}
161149

162-
function fixSnippetOptions(item) {
150+
export function fixSnippetIgnorePaths(item) {
163151
var ignorePaths = item.getIn(["snippetOptions", "ignorePaths"]);
164-
var includePaths = item.getIn(["snippetOptions", "whitelist"]);
165152

166153
if (ignorePaths) {
167154
if (_.isString(ignorePaths)) {
168155
ignorePaths = [ignorePaths];
169156
}
170157
ignorePaths = ignorePaths.map(ensureSlash);
171-
item.setIn(
158+
return item.setIn(
172159
["snippetOptions", "blacklist"],
173160
Immutable.List(ignorePaths)
174161
);
175162
}
163+
return item;
164+
}
165+
166+
export function fixSnippetIncludePaths(item) {
167+
var includePaths = item.getIn(["snippetOptions", "whitelist"]);
176168
if (includePaths) {
177169
includePaths = includePaths.map(ensureSlash);
178-
item.setIn(
170+
return item.setIn(
179171
["snippetOptions", "whitelist"],
180172
Immutable.List(includePaths)
181173
);
182174
}
175+
return item;
183176
}
184177

178+
185179
/**
186180
* Enforce paths to begin with a forward slash
187181
*/
@@ -195,10 +189,9 @@ function ensureSlash(item) {
195189
/**
196190
*
197191
*/
198-
function setMiddleware(item) {
192+
export function setMiddleware(item) {
199193
var mw = getMiddlwares(item);
200-
201-
item.set("middleware", mw);
194+
return item.set("middleware", mw);
202195
}
203196

204197
/**
@@ -252,3 +245,14 @@ function listMerge(list, item) {
252245

253246
return list;
254247
}
248+
249+
/**
250+
* @param item
251+
* @returns {*}
252+
*/
253+
export function setUiPort(item) {
254+
if (item.get("uiPort")) {
255+
return item.setIn(["ui", "port"], item.get("uiPort"));
256+
}
257+
return item;
258+
}

lib/server/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ var serverUtils = {
188188
defaultMiddlewares.unshift({
189189
id: "Browsersync SPA support",
190190
route: "",
191-
handle: require('connect-history-api-fallback')()
191+
handle: require("connect-history-api-fallback")()
192192
});
193193
}
194194

test/specs/cli/cli.options.ignore.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@ var assert = require("chai").assert;
44

55
describe("CLI: Options: dealing with 'ignore' option", function() {
66
it("watches in server mode (no files given)", function() {
7-
var cwd = '/Users/shakyshane/app';
8-
var input = { server: true, files: ['**/*'], ignore: ['**/*.php'], cwd: cwd };
7+
var cwd = "/Users/shakyshane/app";
8+
var input = {
9+
server: true,
10+
files: ["**/*"],
11+
ignore: ["**/*.php"],
12+
cwd: cwd
13+
};
914
var config = merge(input).toJS();
1015
assert.deepEqual(config.files, { core: { globs: ["**/*"], objs: [] } });
11-
assert.ok(config.watchOptions.ignored.indexOf('**/*.php') > -1);
16+
assert.ok(config.watchOptions.ignored.indexOf("**/*.php") > -1);
1217
});
1318
});

0 commit comments

Comments
 (0)