-
Notifications
You must be signed in to change notification settings - Fork 756
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(file-watcher): Add
.watch()
to public api
- Loading branch information
1 parent
cdbda2f
commit 6a2609f
Showing
2 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
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
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,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"); | ||
}); | ||
}); | ||
}); |