Skip to content

Commit

Permalink
watcher: adding cwd + ignore defaults to all
Browse files Browse the repository at this point in the history
  • Loading branch information
shakyShane committed Dec 29, 2017
1 parent f72a6ad commit dbb1267
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 7 deletions.
20 changes: 20 additions & 0 deletions examples/server.watch.js
@@ -0,0 +1,20 @@
/**
*
* Install:
* npm install browser-sync
*
* Run:
* node <yourfile.js>
*
* This example will create a server using https using the default information & use the `app` directory as the root
*
*/

"use strict";

var browserSync = require("browser-sync").create();

browserSync.init({
server: "test/fixtures",
watch: true
});
2 changes: 2 additions & 0 deletions lib/cli/cli-options.ts
Expand Up @@ -10,6 +10,7 @@ import { handleProxyOption } from "./transforms/handleProxyOption";
import { handleServerOption } from "./transforms/handleServerOption";
import { appendServerIndexOption } from "./transforms/appendServerIndexOption";
import { appendServerDirectoryOption } from "./transforms/appendServerDirectoryOption";
import {addCwdToWatchOptions} from "./transforms/addCwdToWatchOptions";

const _ = require("../lodash.custom");
const defaultConfig = require("../default-config");
Expand All @@ -23,6 +24,7 @@ export function merge(input) {
const merged = immDefs.mergeDeep(input);
const transforms = [
addToFilesOption,
addCwdToWatchOptions,
addDefaultIgnorePatterns,
copyCLIIgnoreToWatchOptions,
handleServerOption,
Expand Down
5 changes: 5 additions & 0 deletions lib/cli/transforms/addCwdToWatchOptions.ts
@@ -0,0 +1,5 @@
export function addCwdToWatchOptions(incoming) {
return incoming.updateIn(['watchOptions', 'cwd'], (watchCwd) => {
return watchCwd || incoming.get('cwd');
})
}
9 changes: 2 additions & 7 deletions lib/cli/transforms/addDefaultIgnorePatterns.ts
@@ -1,6 +1,6 @@
import {List} from "immutable";

const defaultIgorePatterns = [
const defaultIgnorePatterns = [
/node_modules/,
/bower_components/,
/\.sass-cache/,
Expand All @@ -10,21 +10,16 @@ const defaultIgorePatterns = [
];

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(defaultIgorePatterns);
const merged = userIgnored.merge(defaultIgnorePatterns);

return watchOptions.merge({
ignored: merged.toList(),
cwd: incoming.get("cwd")
});
});
}
4 changes: 4 additions & 0 deletions lib/file-watcher.js
Expand Up @@ -79,6 +79,10 @@ function watch(patterns, opts, cb) {
watcher.on("all", cb);
}

// watcher.on('ready', () => {
// console.log(watcher.getWatched());
// });

return watcher;
}

Expand Down
13 changes: 13 additions & 0 deletions test/specs/cli/cli.options.ignore.js
@@ -0,0 +1,13 @@
var cli = require("../../../dist/cli/cli-options");
var merge = cli.merge;
var assert = require("chai").assert;

describe("CLI: Options: dealing with 'ignore' option", function() {
it("watches in server mode (no files given)", function() {
var cwd = '/Users/shakyshane/app';
var input = { server: true, files: ['**/*'], ignore: ['**/*.php'], cwd: cwd };
var config = merge(input).toJS();
assert.deepEqual(config.files, { core: { globs: ["**/*"], objs: [] } });
assert.ok(config.watchOptions.ignored.indexOf('**/*.php') > -1);
});
});
12 changes: 12 additions & 0 deletions test/specs/cli/cli.options.watchOptions.cwd.js
@@ -0,0 +1,12 @@
var cli = require("../../../dist/cli/cli-options");
var merge = cli.merge;
var assert = require("chai").assert;

describe("ensures the CWD is transferred to the watchOptions.cwd ", function() {
it("add cwd with files option", function() {
var cwd = '/Users/shakyshane/app';
var input = { server: true, files: "**/*.html", cwd: cwd };
var config = merge(input).toJS();
assert.deepEqual(config.watchOptions.cwd, '/Users/shakyshane/app');
});
});

0 comments on commit dbb1267

Please sign in to comment.