Skip to content

Commit 393309b

Browse files
committed
options: switch to pipeline of transforms
1 parent 2c744c8 commit 393309b

21 files changed

Lines changed: 529 additions & 537 deletions

lib/cli/cli-options.js

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

lib/cli/cli-options.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { Map, List, fromJS } from "immutable";
2+
import { addToFilesOption } from "./transforms/addToFilesOption";
3+
import { addDefaultIgnorePatterns } from "./transforms/addDefaultIgnorePatterns";
4+
import { copyCLIIgnoreToWatchOptions } from "./transforms/copyCLIIgnoreToWatchOptions";
5+
import { handleExtensionsOption } from "./transforms/handleExtensionsOption";
6+
import { handleFilesOption } from "./transforms/handleFilesOption";
7+
import { handleGhostModeOption } from "./transforms/handleGhostModeOption";
8+
import { handlePortsOption } from "./transforms/handlePortsOption";
9+
import { handleProxyOption } from "./transforms/handleProxyOption";
10+
import { handleServerOption } from "./transforms/handleServerOption";
11+
import { appendServerIndexOption } from "./transforms/appendServerIndexOption";
12+
import { appendServerDirectoryOption } from "./transforms/appendServerDirectoryOption";
13+
14+
const _ = require("../lodash.custom");
15+
const defaultConfig = require("../default-config");
16+
const immDefs = fromJS(defaultConfig);
17+
18+
/**
19+
* @param {Object} input
20+
* @returns {Map}
21+
*/
22+
export function merge(input) {
23+
const merged = immDefs.mergeDeep(input);
24+
const transforms = [
25+
addToFilesOption,
26+
addDefaultIgnorePatterns,
27+
copyCLIIgnoreToWatchOptions,
28+
handleServerOption,
29+
appendServerIndexOption,
30+
appendServerDirectoryOption,
31+
handleProxyOption,
32+
handlePortsOption,
33+
handleGhostModeOption,
34+
handleFilesOption,
35+
handleExtensionsOption
36+
];
37+
38+
const output = transforms.reduce((acc, item) => {
39+
return item.call(null, acc);
40+
}, merged);
41+
42+
// console.log(output.toJSON());
43+
44+
return output;
45+
}
46+
47+
/**
48+
* @param string
49+
*/
50+
export function explodeFilesArg(string): string {
51+
return string.split(",").map(item => item.trim());
52+
}
53+
54+
/**
55+
* @param value
56+
* @returns {{globs: Array, objs: Array}}
57+
*/
58+
export function makeFilesArg(value) {
59+
let globs = [];
60+
let objs = [];
61+
62+
if (_.isString(value)) {
63+
globs = globs.concat(explodeFilesArg(value));
64+
}
65+
66+
if (List.isList(value) && value.size) {
67+
value.forEach(function(value) {
68+
if (_.isString(value)) {
69+
globs.push(value);
70+
} else {
71+
if (Map.isMap(value)) {
72+
objs.push(value);
73+
}
74+
}
75+
});
76+
}
77+
78+
return {
79+
globs: globs,
80+
objs: objs
81+
};
82+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import {List} from "immutable";
2+
3+
export function addDefaultIgnorePatterns(incoming) {
4+
if (!incoming.get("watch")) {
5+
return incoming;
6+
}
7+
8+
return incoming.update("watchOptions", watchOptions => {
9+
const userIgnored = List([])
10+
.concat(watchOptions.get("ignored"))
11+
.filter(Boolean)
12+
.toSet();
13+
14+
const merged = userIgnored.merge([
15+
/node_modules/,
16+
/bower_components/,
17+
/\.sass-cache/,
18+
/\.vscode/,
19+
/\.git/,
20+
/\.idea/,
21+
"!node_modules/**/*",
22+
"!**/node_modules/**"
23+
]);
24+
25+
return watchOptions.merge({
26+
ignored: merged.toList(),
27+
cwd: incoming.get("cwd")
28+
});
29+
});
30+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import {List, Map} from "immutable";
2+
3+
export function addToFilesOption(incoming) {
4+
if (!incoming.get("watch")) {
5+
return incoming;
6+
}
7+
8+
let serverPaths = [];
9+
10+
const fromServeStatic = incoming
11+
.get("serveStatic", List([]))
12+
.toArray();
13+
const ssPaths = fromServeStatic
14+
.reduce((acc, ss) => {
15+
if (typeof ss === "string") {
16+
return acc.concat(ss);
17+
}
18+
if (ss.dir && typeof ss.dir === "string") {
19+
return acc.concat(ss);
20+
}
21+
return acc;
22+
}, []);
23+
24+
ssPaths.forEach(p => serverPaths.push(p));
25+
26+
const server = incoming.get("server");
27+
if (server) {
28+
if (server === true) {
29+
serverPaths.push(".");
30+
}
31+
if (typeof server === "string") {
32+
serverPaths.push(server);
33+
}
34+
if (
35+
List.isList(server) &&
36+
server.every(x => typeof x === "string")
37+
) {
38+
server.forEach(s => serverPaths.push(s));
39+
}
40+
if (Map.isMap(server)) {
41+
const baseDirProp = server.get("baseDir");
42+
const baseDirs = List([]).concat(baseDirProp).filter(Boolean);
43+
baseDirs.forEach(s => serverPaths.push(s));
44+
}
45+
}
46+
47+
const output = incoming.update("files", files => {
48+
return List([])
49+
.concat(files, serverPaths)
50+
.filter(Boolean);
51+
});
52+
return output;
53+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export function appendServerDirectoryOption(incoming) {
2+
if (!incoming.get('server')) return incoming;
3+
if (incoming.get('directory')) {
4+
return incoming.setIn(['server', 'directory'], incoming.has('directory'));
5+
}
6+
return incoming;
7+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export function appendServerIndexOption(incoming) {
2+
if (!incoming.get('server')) return incoming;
3+
const value = incoming.get('index');
4+
5+
if (value) {
6+
return incoming.setIn(['server', 'index'], value);
7+
}
8+
9+
return incoming;
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {List} from "immutable";
2+
3+
export function copyCLIIgnoreToWatchOptions(incoming) {
4+
if (!incoming.get("ignore")) {
5+
return incoming;
6+
}
7+
return incoming.updateIn(["watchOptions", "ignored"], ignored => {
8+
const userIgnore = List([]).concat(incoming.get("ignore"));
9+
return ignored.concat(userIgnore);
10+
});
11+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {List} from "immutable";
2+
import {explodeFilesArg} from "../cli-options";
3+
4+
const _ = require("../../lodash.custom");
5+
6+
export function handleExtensionsOption(incoming) {
7+
const value = incoming.get('extensions');
8+
if (_.isString(value)) {
9+
const split = explodeFilesArg(value);
10+
if (split.length) {
11+
return incoming.set('extensions', List(split));
12+
}
13+
}
14+
if (List.isList(value)) {
15+
return incoming.set('extensions', value);
16+
}
17+
return incoming;
18+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import {fromJS} from "immutable";
2+
import {makeFilesArg} from "../cli-options";
3+
import {FilesNamespaces} from "../../types";
4+
5+
export function handleFilesOption(incoming) {
6+
const value = incoming.get('files');
7+
const namespaces: FilesNamespaces = {
8+
core: {
9+
globs: [],
10+
objs: []
11+
}
12+
};
13+
14+
const processed = makeFilesArg(value);
15+
16+
if (processed.globs.length) {
17+
namespaces.core.globs = processed.globs;
18+
}
19+
20+
if (processed.objs.length) {
21+
namespaces.core.objs = processed.objs;
22+
}
23+
24+
return incoming.set('files', fromJS(namespaces));
25+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import {fromJS} from "immutable";
2+
3+
export function handleGhostModeOption(incoming) {
4+
const value = incoming.get('ghostMode');
5+
var trueAll = {
6+
clicks: true,
7+
scroll: true,
8+
forms: {
9+
submit: true,
10+
inputs: true,
11+
toggles: true
12+
}
13+
};
14+
15+
var falseAll = {
16+
clicks: false,
17+
scroll: false,
18+
forms: {
19+
submit: false,
20+
inputs: false,
21+
toggles: false
22+
}
23+
};
24+
25+
if (
26+
value === false ||
27+
value === "false"
28+
) {
29+
return incoming.set('ghostMode', fromJS(falseAll));
30+
}
31+
32+
if (
33+
value === true ||
34+
value === "true"
35+
) {
36+
return incoming.set('ghostMode', fromJS(trueAll));
37+
}
38+
39+
if (value.get("forms") === false) {
40+
return incoming.set('ghostMode', value.withMutations(function (map) {
41+
map.set(
42+
"forms",
43+
fromJS({
44+
submit: false,
45+
inputs: false,
46+
toggles: false
47+
})
48+
);
49+
}));
50+
}
51+
52+
if (value.get("forms") === true) {
53+
return incoming.set('ghostMode', value.withMutations(function (map) {
54+
map.set(
55+
"forms",
56+
fromJS({
57+
submit: true,
58+
inputs: true,
59+
toggles: true
60+
})
61+
);
62+
}));
63+
}
64+
65+
return incoming;
66+
}

0 commit comments

Comments
 (0)