-
Notifications
You must be signed in to change notification settings - Fork 754
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
options: switch to pipeline of transforms
- Loading branch information
1 parent
2c744c8
commit 393309b
Showing
21 changed files
with
529 additions
and
537 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.