Skip to content

Commit 2a2d881

Browse files
committed
cli: add cwd, watch and ignore flags
1 parent be1a477 commit 2a2d881

6 files changed

Lines changed: 131 additions & 15 deletions

File tree

cli-options/opts.start.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
"alias": "s",
44
"desc": "Run a Local server (uses your cwd as the web root)"
55
},
6+
"cwd": {
7+
"type": "string",
8+
"desc": "Working directory"
9+
},
610
"serveStatic": {
711
"type": "array",
812
"alias": "ss",
@@ -26,6 +30,15 @@
2630
"alias": "b",
2731
"desc": "Choose which browser should be auto-opened"
2832
},
33+
"watch": {
34+
"type": "boolean",
35+
"alias": "w",
36+
"desc": "Watch files"
37+
},
38+
"ignore": {
39+
"type": "array",
40+
"desc": "Ignore patterns for file watchers"
41+
},
2942
"files": {
3043
"type": "array",
3144
"alias": "f",

lib/bin.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@ function runFromCli() {
5555
const command = input[0];
5656
const valid = ["start", "init", "reload", "recipe"];
5757

58-
if (argv.help) {
59-
return yargs.showHelp();
60-
}
61-
6258
if (valid.indexOf(command) > -1) {
6359
return handleIncoming(command, yargs.reset());
6460
}
@@ -169,11 +165,14 @@ function processStart(yargs) {
169165
.options(startOpts)
170166
.example("$0 start -s app", "- Use the App directory to serve files")
171167
.example("$0 start -p www.bbc.co.uk", "- Proxy an existing website")
168+
.default('cwd', () => process.cwd())
172169
.help().argv;
173170
}
171+
174172
/**
175173
* @param {string} command
176174
* @param {object} yargs
175+
* @param cwd
177176
*/
178177
function handleIncoming(command, yargs) {
179178
let out;
@@ -184,6 +183,7 @@ function handleIncoming(command, yargs) {
184183
out = yargs
185184
.usage("Usage: $0 init")
186185
.example("$0 init")
186+
.default('cwd', () => process.cwd())
187187
.help().argv;
188188
}
189189
if (command === "reload") {
@@ -192,6 +192,7 @@ function handleIncoming(command, yargs) {
192192
.options(reloadOpts)
193193
.example("$0 reload")
194194
.example("$0 reload --port 4000")
195+
.default('cwd', () => process.cwd())
195196
.help().argv;
196197
}
197198
if (command === "recipe") {
@@ -200,6 +201,7 @@ function handleIncoming(command, yargs) {
200201
.option(recipeOpts)
201202
.example("$0 recipe ls", "list the recipes")
202203
.example("$0 recipe gulp.sass", "use the gulp.sass recipe")
204+
.default('cwd', () => process.cwd())
203205
.help().argv;
204206
}
205207

lib/cli/cli-options.js

Lines changed: 101 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { List, Map } from "immutable";
2+
13
var path = require("path");
24
var url = require("url");
35
var _ = require("../lodash.custom");
@@ -274,12 +276,106 @@ opts.callbacks = {
274276
* @returns {Map}
275277
*/
276278
opts.merge = function(values, argv) {
277-
return immDefs.mergeDeep(values).withMutations(function(item) {
278-
item.map(function(value, key) {
279-
if (opts.callbacks[key]) {
280-
item.set(key, opts.callbacks[key](value, argv));
279+
const merged = immDefs.mergeDeep(values);
280+
const transforms = [
281+
function addToFilesOption(incoming) {
282+
if (!incoming.get("watch")) {
283+
return incoming;
281284
}
282-
});
285+
286+
let serverPaths = [];
287+
288+
const fromServeStatic = incoming
289+
.get("serveStatic", List([]))
290+
.toArray();
291+
const ssPaths = fromServeStatic
292+
.reduce((acc, ss) => {
293+
if (typeof ss === "string") {
294+
return acc.concat(ss);
295+
}
296+
if (ss.dir && typeof ss.dir === "string") {
297+
return acc.concat(ss);
298+
}
299+
return acc;
300+
}, [])
301+
.map(p => path.resolve(p));
302+
303+
ssPaths.forEach(p => serverPaths.push(p));
304+
305+
const server = incoming.get("server");
306+
if (server) {
307+
if (server === true) {
308+
serverPaths.push(".");
309+
}
310+
if (typeof server === "string") {
311+
serverPaths.push(server);
312+
}
313+
if (
314+
List.isList(server) &&
315+
server.every(x => typeof x === "string")
316+
) {
317+
server.forEach(s => serverPaths.push(s));
318+
}
319+
if (Map.isMap(server)) {
320+
const baseDirs = server.get("baseDir", List([])).toArray();
321+
baseDirs.forEach(s => serverPaths.push(s));
322+
}
323+
}
324+
325+
const output = incoming.update("files", files => {
326+
return List([])
327+
.concat(files, serverPaths)
328+
.filter(Boolean);
329+
});
330+
return output;
331+
},
332+
function addDefaultIgnorePatterns(incoming) {
333+
if (!incoming.get("watch")) {
334+
return incoming;
335+
}
336+
337+
return incoming.update("watchOptions", watchOptions => {
338+
const userIgnored = List([])
339+
.concat(watchOptions.get("ignored"))
340+
.filter(Boolean)
341+
.toSet();
342+
343+
const merged = userIgnored.merge([
344+
/node_modules/,
345+
/bower_components/,
346+
/\.sass-cache/,
347+
/\.vscode/,
348+
/\.git/,
349+
/\.idea/,
350+
"!node_modules/**/*",
351+
"!**/node_modules/**"
352+
]);
353+
354+
return watchOptions.merge({
355+
ignored: merged.toList(),
356+
cwd: incoming.get("cwd")
357+
});
358+
});
359+
},
360+
function copyCLIIgnoreToWatchOptions(incoming) {
361+
if (!incoming.has("ignore")) {
362+
return incoming;
363+
}
364+
return incoming.updateIn(["watchOptions", "ignored"], ignored => {
365+
return ignored.merge(incoming.get("ignore"));
366+
});
367+
}
368+
];
369+
370+
const transformed = transforms.reduce((acc, item) => {
371+
return item.call(null, acc);
372+
}, merged);
373+
374+
return transformed.map(function(value, key) {
375+
if (opts.callbacks[key]) {
376+
return opts.callbacks[key](value, argv);
377+
}
378+
return value;
283379
});
284380
};
285381

lib/cli/command.start.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ const _ = require("../lodash.custom");
1515
*/
1616
export default function(opts) {
1717
const flags = preprocessFlags(opts.cli.flags);
18-
const maybepkg = path.resolve(process.cwd(), "package.json");
18+
const cwd = flags.cwd || process.cwd();
19+
const maybepkg = path.resolve(cwd, "package.json");
1920
let input = flags;
2021

2122
if (flags.config) {
22-
const maybeconf = path.resolve(process.cwd(), flags.config);
23+
const maybeconf = path.resolve(cwd, flags.config);
2324
if (existsSync(maybeconf)) {
2425
const conf = require(maybeconf);
2526
input = _.merge({}, conf, flags);

lib/public/init.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ module.exports = function(browserSync, name, pjson) {
2020
* If the current instance is already running, just return an error
2121
*/
2222
if (browserSync.active) {
23-
return args.cb(
24-
new Error("Instance: " + name + " is already running!")
25-
);
23+
return args.cb(new Error(`Instance: ${name} is already running!`));
2624
}
2725

26+
// Env specific items
2827
args.config.version = pjson.version;
28+
args.config.cwd = args.config.cwd || process.cwd();
2929

3030
return browserSync.init(merge(args.config), args.cb);
3131
};

lib/server/utils.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,11 @@ var serverUtils = {
112112
* Note, this could throw, but let that happen as
113113
* the error message good enough.
114114
*/
115-
var maybe = path.resolve(process.cwd(), "node_modules", httpModule);
115+
var maybe = path.resolve(
116+
options.get("cwd"),
117+
"node_modules",
118+
httpModule
119+
);
116120
return require(maybe);
117121
}
118122

0 commit comments

Comments
 (0)