0

I would like to get updated content of a certain file, I was only able to get the updated file name. The updated content will always be on a new line, appreciate all answers from now!

NOTE: I found a solution similar to what I need but it was in java. How to watch file for new content and retrieve that content

const md5 = require('md5');
require('log-timestamp');

const file = './file.txt';

console.log(`Watching for file changes on ${file}`);

let md5Previous = null;
let fsWait = false;
fs.watch(file, (event, filename) => {
  if (filename) {
    if (fsWait) return;
    fsWait = setTimeout(() => {
      fsWait = false;
    }, 100);
    const md5Current = md5(fs.readFileSync(buttonPressesLogFile));
    if (md5Current === md5Previous) {
      return;
    }
    md5Previous = md5Current;
    console.log(`${filename} file Changed`);
  }
});```
Alhassan Raad
  • 179
  • 11

1 Answers1

0

For anyone searching for an answer, I used chokidar, read-last-lines, and EventEmitter to watch the file, and get the latest contents.

Alhassan Raad
  • 179
  • 11