Skip to content

Commit

Permalink
feat(file-watcher): Add .watch() to public api
Browse files Browse the repository at this point in the history
  • Loading branch information
shakyShane committed Apr 12, 2015
1 parent cdbda2f commit 6a2609f
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
10 changes: 9 additions & 1 deletion index.js
Expand Up @@ -83,6 +83,13 @@ module.exports.notify = noop("notify");
*/
module.exports.exit = noop("exit");

/**
* File watcher
*
* @method watch
*/
module.exports.watch = noop("watch");

/**
* Method to pause file change events
*
Expand Down Expand Up @@ -261,7 +268,8 @@ function create(name, emitter) {
cleanup: browserSync.cleanup.bind(browserSync),
use: browserSync.registerPlugin.bind(browserSync),
getOption: browserSync.getOption.bind(browserSync),
emitter: browserSync.events
emitter: browserSync.events,
watch: require("./lib/file-watcher").watch
};

Object.defineProperty(instance, "active", {
Expand Down
62 changes: 62 additions & 0 deletions test/specs/api/watch.js
@@ -0,0 +1,62 @@
"use strict";

var browserSync = require("../../../");
var path = require("path");
var outpath = path.join(__dirname, "../../fixtures");
var fs = require("graceful-fs");

var tempFileContent = "A test generated this file and it is safe to delete";
var writeTimeout = 150; // Wait for it to get to the filesystem
var assert = require("assert");

var writeFileWait = function (name, content, cb) {
if (!cb) {
cb = function () {
};
}
setTimeout(function () {
fs.writeFile(name, content, cb);
}, writeTimeout);
};

describe("API: .watch() - Public watch method", function () {

it("Should allow arbitrary watchers with callback fn when not connected to running instance", function (done) {

browserSync.reset();

var tempFile = path.join(outpath, "watch-func.txt");
var bs = browserSync.create("test");

fs.writeFile(tempFile, tempFileContent, function () {

var watcher = bs.watch(tempFile, {ignoreInitial: true}, function (event, file) {
assert.equal(event, "change");
assert.equal(file, tempFile);
watcher.close();
done();
});

writeFileWait(tempFile, tempFileContent + " changed");
});
});

it("Should allow arbitrary watchers with event emitter fn when not connected to running instance", function (done) {

browserSync.reset();

var tempFile = path.join(outpath, "watch-func.txt");
var bs = browserSync.create("test");

fs.writeFile(tempFile, tempFileContent, function () {

var watcher = bs.watch(tempFile, {ignoreInitial: true}).on("change", function (file) {
assert.equal(file, tempFile);
watcher.close();
done();
});

writeFileWait(tempFile, tempFileContent + " changed");
});
});
});

0 comments on commit 6a2609f

Please sign in to comment.