mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 18:55:26 +00:00
added ability to watch is file changed wile server is running
This commit is contained in:
parent
5661c131fb
commit
95ffbd5ed8
1 changed files with 58 additions and 0 deletions
58
lib/server/appcache.js
Normal file
58
lib/server/appcache.js
Normal 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');
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue