From 58a3a30a6b9221d146a23594e71442cce5c19fcd Mon Sep 17 00:00:00 2001 From: Andrew Koung Date: Tue, 19 Mar 2019 21:49:10 -0400 Subject: [PATCH] Issue #654 working on adding feature fs.watchFile() --- package-lock.json | 2 +- src/filesystem/interface.js | 37 ++++++++++++++++++++++++ tests/index.js | 1 + tests/spec/fs.watchFile.spec.js | 50 +++++++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 tests/spec/fs.watchFile.spec.js diff --git a/package-lock.json b/package-lock.json index f699b11..595abf4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "filer", - "version": "1.1.0", + "version": "1.1.2", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/src/filesystem/interface.js b/src/filesystem/interface.js index 824128b..840ff88 100644 --- a/src/filesystem/interface.js +++ b/src/filesystem/interface.js @@ -206,6 +206,43 @@ function FileSystem(options, callback) { return watcher; }; + //Object that uses filenames as keys + const statWatchers = new Map(); + + this.watchFile = function(filename, options, listener) { + const prevStat, currStat; + + if (Path.isNull(filename)) { + throw new Error('Path must be a string without null bytes.'); + } + //Checks to see if there were options passed in and if not, the callback function will be set here + if (typeof options === 'function') { + listener = options; + options = {}; + } + //default 5007ms interval, persistent is not used this project + const interval = options.interval || 5007; + listener = listener || nop; + + //stores interval return values + statWatchers.set(filename, value); + + var value = setInterval(function() { + fs.stat(filename, function(err, stats) { + if(err) { + //console.log(err); + } + //record file curr + //compare curr-prev + //if theres a difference file change + }); + }, + interval + ); + + return watcher; + }; + // Deal with various approaches to node ID creation function wrappedGuidFn(context) { return function (callback) { diff --git a/tests/index.js b/tests/index.js index beb517f..6243245 100644 --- a/tests/index.js +++ b/tests/index.js @@ -41,6 +41,7 @@ require('./spec/trailing-slashes.spec'); require('./spec/times.spec'); require('./spec/time-flags.spec'); require('./spec/fs.watch.spec'); +require('./spec/fs.watchFile.spec'); require('./spec/fs.unwatchFile.spec'); require('./spec/errors.spec'); require('./spec/fs.shell.spec'); diff --git a/tests/spec/fs.watchFile.spec.js b/tests/spec/fs.watchFile.spec.js new file mode 100644 index 0000000..90cb2fc --- /dev/null +++ b/tests/spec/fs.watchFile.spec.js @@ -0,0 +1,50 @@ +'use strict'; + +var util = require('../lib/test-utils.js'); +var expect = require('chai').expect; + +describe('fs.watchFile', function() { + beforeEach(util.setup); + afterEach(util.cleanup); + + it('should be a function', function() { + var fs = util.fs(); + expect(typeof fs.watchFile).to.equal('function'); + }); + + it('should get a change event when writing a file', function(done) { + const fs = util.fs(); + + const watcher = fs.watchFile('/myfile.txt', function(event, filename) { + expect(event).to.equal('change'); + expect(filename).to.equal('/myfile.txt'); + watcher.close(); + done(); + }); + + fs.writeFile('/myfile.txt', 'data', function(error) { + if(error) throw error; + }); + }); + + /* + it('should get a change event when renaming a file', function(done) { + const fs = util.fs(); + + fs.writeFile('/myfile.txt', 'data', function(error) { + if(error) throw error; + + const watcher = fs.watchFile('/myfile.txt', function(event, filename) { + expect(event).to.equal('change'); + expect(filename).to.equal('/myfile.txt'); + watcher.close(); + done(); + }); + + fs.rename('/myfile.txt', '/mynewfile.txt', function(error) { + if(error) throw error; + }); + }); + }); + */ +}); \ No newline at end of file