Commit 62bbbccf by Amir Aharon

reload when file is changed

parent 0361c765
......@@ -4,12 +4,14 @@ const url = require('url');
const fs = require('fs');
const request = require('request');
const moment = require('moment');
const md5 = require('md5');
var myArgs = require('optimist').argv,
help = 'node app.js -p port -f file-path/directory [-d delay-between-samples --postUrl http url to post instead of websocket]';
var config = {};
var jsonFilesMap = [];
var filesMD5 = [];
//const twoDaysAgoMs = 1000 * 3600 * 47;
/**
......@@ -198,3 +200,29 @@ fs.stat(config.file, (err, stats) => {
}
});
let fsWait = false;
fs.watch(config.file, (event, filename) => {
if (filename) {
// avoid temp editing files
if (filename.startsWith(".")) return;
// avoid multiple events
if (fsWait) return;
fsWait = setTimeout(() => {
fsWait = false;
}, 100);
try {
const fileUrl = config.file + '/' + filename;
const md5Current = md5(fs.readFileSync(fileUrl));
if (filesMD5[filename] && filesMD5[filename] === md5Current){
return;
}
// else file was really changed
console.log(`${filename} file Changed`);
let filePrefix = getFilePrefix(filename);
handleReadFile(fileUrl,filePrefix);
} catch (error) {
console.log(error);
}
}
});
\ No newline at end of file
......@@ -17,11 +17,12 @@
"express": "^4.16.2",
"fs": "0.0.1-security",
"http": "0.0.0",
"md5": "^2.2.1",
"moment": "^2.22.2",
"optimist": "^0.6.1",
"request": "^2.83.0"
},
"bin": {
"ws-event-emulator": "./app.js"
"polling-simulator": "./app.js"
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment