Skip to content

Commit

Permalink
options: switch to pipeline of transforms
Browse files Browse the repository at this point in the history
  • Loading branch information
shakyShane committed Dec 26, 2017
1 parent 2c744c8 commit 393309b
Show file tree
Hide file tree
Showing 21 changed files with 529 additions and 537 deletions.
411 changes: 0 additions & 411 deletions lib/cli/cli-options.js

This file was deleted.

82 changes: 82 additions & 0 deletions lib/cli/cli-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { Map, List, fromJS } from "immutable";
import { addToFilesOption } from "./transforms/addToFilesOption";
import { addDefaultIgnorePatterns } from "./transforms/addDefaultIgnorePatterns";
import { copyCLIIgnoreToWatchOptions } from "./transforms/copyCLIIgnoreToWatchOptions";
import { handleExtensionsOption } from "./transforms/handleExtensionsOption";
import { handleFilesOption } from "./transforms/handleFilesOption";
import { handleGhostModeOption } from "./transforms/handleGhostModeOption";
import { handlePortsOption } from "./transforms/handlePortsOption";
import { handleProxyOption } from "./transforms/handleProxyOption";
import { handleServerOption } from "./transforms/handleServerOption";
import { appendServerIndexOption } from "./transforms/appendServerIndexOption";
import { appendServerDirectoryOption } from "./transforms/appendServerDirectoryOption";

const _ = require("../lodash.custom");
const defaultConfig = require("../default-config");
const immDefs = fromJS(defaultConfig);

/**
* @param {Object} input
* @returns {Map}
*/
export function merge(input) {
const merged = immDefs.mergeDeep(input);
const transforms = [
addToFilesOption,
addDefaultIgnorePatterns,
copyCLIIgnoreToWatchOptions,
handleServerOption,
appendServerIndexOption,
appendServerDirectoryOption,
handleProxyOption,
handlePortsOption,
handleGhostModeOption,
handleFilesOption,
handleExtensionsOption
];

const output = transforms.reduce((acc, item) => {
return item.call(null, acc);
}, merged);

// console.log(output.toJSON());

return output;
}

/**
* @param string
*/
export function explodeFilesArg(string): string {
return string.split(",").map(item => item.trim());
}

/**
* @param value
* @returns {{globs: Array, objs: Array}}
*/
export function makeFilesArg(value) {
let globs = [];
let objs = [];

if (_.isString(value)) {
globs = globs.concat(explodeFilesArg(value));
}

if (List.isList(value) && value.size) {
value.forEach(function(value) {
if (_.isString(value)) {
globs.push(value);
} else {
if (Map.isMap(value)) {
objs.push(value);
}
}
});
}

return {
globs: globs,
objs: objs
};
}
30 changes: 30 additions & 0 deletions lib/cli/transforms/addDefaultIgnorePatterns.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {List} from "immutable";

export function addDefaultIgnorePatterns(incoming) {
if (!incoming.get("watch")) {
return incoming;
}

return incoming.update("watchOptions", watchOptions => {
const userIgnored = List([])
.concat(watchOptions.get("ignored"))
.filter(Boolean)
.toSet();

const merged = userIgnored.merge([
/node_modules/,
/bower_components/,
/\.sass-cache/,
/\.vscode/,
/\.git/,
/\.idea/,
"!node_modules/**/*",
"!**/node_modules/**"
]);

return watchOptions.merge({
ignored: merged.toList(),
cwd: incoming.get("cwd")
});
});
}
53 changes: 53 additions & 0 deletions lib/cli/transforms/addToFilesOption.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import {List, Map} from "immutable";

export function addToFilesOption(incoming) {
if (!incoming.get("watch")) {
return incoming;
}

let serverPaths = [];

const fromServeStatic = incoming
.get("serveStatic", List([]))
.toArray();
const ssPaths = fromServeStatic
.reduce((acc, ss) => {
if (typeof ss === "string") {
return acc.concat(ss);
}
if (ss.dir && typeof ss.dir === "string") {
return acc.concat(ss);
}
return acc;
}, []);

ssPaths.forEach(p => serverPaths.push(p));

const server = incoming.get("server");
if (server) {
if (server === true) {
serverPaths.push(".");
}
if (typeof server === "string") {
serverPaths.push(server);
}
if (
List.isList(server) &&
server.every(x => typeof x === "string")
) {
server.forEach(s => serverPaths.push(s));
}
if (Map.isMap(server)) {
const baseDirProp = server.get("baseDir");
const baseDirs = List([]).concat(baseDirProp).filter(Boolean);
baseDirs.forEach(s => serverPaths.push(s));
}
}

const output = incoming.update("files", files => {
return List([])
.concat(files, serverPaths)
.filter(Boolean);
});
return output;
}
7 changes: 7 additions & 0 deletions lib/cli/transforms/appendServerDirectoryOption.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function appendServerDirectoryOption(incoming) {
if (!incoming.get('server')) return incoming;
if (incoming.get('directory')) {
return incoming.setIn(['server', 'directory'], incoming.has('directory'));
}
return incoming;
}
10 changes: 10 additions & 0 deletions lib/cli/transforms/appendServerIndexOption.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function appendServerIndexOption(incoming) {
if (!incoming.get('server')) return incoming;
const value = incoming.get('index');

if (value) {
return incoming.setIn(['server', 'index'], value);
}

return incoming;
}
11 changes: 11 additions & 0 deletions lib/cli/transforms/copyCLIIgnoreToWatchOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {List} from "immutable";

export function copyCLIIgnoreToWatchOptions(incoming) {
if (!incoming.get("ignore")) {
return incoming;
}
return incoming.updateIn(["watchOptions", "ignored"], ignored => {
const userIgnore = List([]).concat(incoming.get("ignore"));
return ignored.concat(userIgnore);
});
}
18 changes: 18 additions & 0 deletions lib/cli/transforms/handleExtensionsOption.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {List} from "immutable";
import {explodeFilesArg} from "../cli-options";

const _ = require("../../lodash.custom");

export function handleExtensionsOption(incoming) {
const value = incoming.get('extensions');
if (_.isString(value)) {
const split = explodeFilesArg(value);
if (split.length) {
return incoming.set('extensions', List(split));
}
}
if (List.isList(value)) {
return incoming.set('extensions', value);
}
return incoming;
}
25 changes: 25 additions & 0 deletions lib/cli/transforms/handleFilesOption.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {fromJS} from "immutable";
import {makeFilesArg} from "../cli-options";
import {FilesNamespaces} from "../../types";

export function handleFilesOption(incoming) {
const value = incoming.get('files');
const namespaces: FilesNamespaces = {
core: {
globs: [],
objs: []
}
};

const processed = makeFilesArg(value);

if (processed.globs.length) {
namespaces.core.globs = processed.globs;
}

if (processed.objs.length) {
namespaces.core.objs = processed.objs;
}

return incoming.set('files', fromJS(namespaces));
}
66 changes: 66 additions & 0 deletions lib/cli/transforms/handleGhostModeOption.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import {fromJS} from "immutable";

export function handleGhostModeOption(incoming) {
const value = incoming.get('ghostMode');
var trueAll = {
clicks: true,
scroll: true,
forms: {
submit: true,
inputs: true,
toggles: true
}
};

var falseAll = {
clicks: false,
scroll: false,
forms: {
submit: false,
inputs: false,
toggles: false
}
};

if (
value === false ||
value === "false"
) {
return incoming.set('ghostMode', fromJS(falseAll));
}

if (
value === true ||
value === "true"
) {
return incoming.set('ghostMode', fromJS(trueAll));
}

if (value.get("forms") === false) {
return incoming.set('ghostMode', value.withMutations(function (map) {
map.set(
"forms",
fromJS({
submit: false,
inputs: false,
toggles: false
})
);
}));
}

if (value.get("forms") === true) {
return incoming.set('ghostMode', value.withMutations(function (map) {
map.set(
"forms",
fromJS({
submit: true,
inputs: true,
toggles: true
})
);
}));
}

return incoming;
}
25 changes: 25 additions & 0 deletions lib/cli/transforms/handlePortsOption.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {Map} from 'immutable';
import {PortsOption} from "../../types";

export function handlePortsOption(incoming) {
const value = incoming.get('ports');
if (!value) return incoming;

const obj: PortsOption = {min: null, max: null};

if (typeof value === "string") {
if (~value.indexOf(",")) {
const segs = value.split(",");
obj.min = parseInt(segs[0], 10);
obj.max = parseInt(segs[1], 10);
} else {
obj.min = parseInt(value, 10);
obj.max = null;
}
} else {
obj.min = value.get("min");
obj.max = value.get("max") || null;
}

return incoming.set('ports', Map(obj));
}
44 changes: 44 additions & 0 deletions lib/cli/transforms/handleProxyOption.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import * as url from 'url';
import {Map} from 'immutable';
import {BrowsersyncProxy} from "../../types";

export function handleProxyOption(incoming) {
let value = incoming.get('proxy');
let mw;
let target;

if (!value || value === true) {
return incoming;
}

if (typeof value !== "string") {
target = value.get("target");
mw = value.get("middleware");
} else {
target = value;
value = Map({});
}

if (!target.match(/^(https?):\/\//)) {
target = "http://" + target;
}

const parsedUrl = url.parse(target);

if (!parsedUrl.port) {
parsedUrl.port = "80";
}

const out: BrowsersyncProxy = {
target: parsedUrl.protocol + "//" + parsedUrl.host,
url: Map(parsedUrl)
};

if (mw) {
out.middleware = mw;
}

const proxyOutput = value.mergeDeep(out);

return incoming.set('proxy', proxyOutput);
}

0 comments on commit 393309b

Please sign in to comment.