added ability to watch is file changed wile server is running

This commit is contained in:
coderaiser 2012-09-11 08:11:52 -04:00
parent 5661c131fb
commit 95ffbd5ed8

58
lib/server/appcache.js Normal file
View file

@ -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');
};
}