From 95ffbd5ed83ed4c89cf967511b252a1db58e129f Mon Sep 17 00:00:00 2001 From: coderaiser Date: Tue, 11 Sep 2012 08:11:52 -0400 Subject: [PATCH] added ability to watch is file changed wile server is running --- lib/server/appcache.js | 58 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 lib/server/appcache.js diff --git a/lib/server/appcache.js b/lib/server/appcache.js new file mode 100644 index 00000000..cfe461f1 --- /dev/null +++ b/lib/server/appcache.js @@ -0,0 +1,58 @@ +var fs = require('fs'); + +/* varible contain all watched file names + * {name: true} + */ +var FileNames = {}; +var i=0; + +/* function thet use for crossplatform + * access to fs.watch or fs.watchFile function + */ +var fs_watch = null; +var on_fs_watch =null; + +setWatachFunctions(); + +function setWatachFunctions(){ + if(process.platform === 'win32'){ + /* good on windows */ + fs_watch = fs.watch; + on_fs_watch = onWatch; + } + else{ + /* good on linux */ + fs_watch = fs.watchFile; + on_fs_watch = onWatchFile; + } + +} + +exports.watch = function(pFileName){ + console.log(pFileName + ' is watched'); + + if(!FileNames[pFileName]){ + try{ + fs_watch(pFileName, on_fs_watch(pFileName)); + } + catch(pError){ + console.log(pError); + } + + FileNames[pFileName] = true; + } +}; + +function onWatch (pFileName){ + return function(pEvent, pFileName){ + console.log('file ' + pFileName + 'is changed'); + }; +} + +function onWatchFile(pFileName){ + return function(pCurr, pPrev){ + console.log(pCurr); + if(pCurr.mtime !== pCurr.pPrev.mtime) + console.log('file ' + pFileName + 'is changed'); + }; +} \ No newline at end of file