Home > OS >  How to detect changes in the contents of a file, using Regex and Chokidar
How to detect changes in the contents of a file, using Regex and Chokidar

Time:01-27

I would like to run a JS function when a specific section of a file changes. The contents that are being watched should be selected by Regex.

As far as I have seen, libraries like Chokidar can only detect changes to a file directly and do not provide information about the contents of the file before and after the changes.

CodePudding user response:

Have you looked into all the data available on chokidar events? It's built right on top of node's fs package, so it should have everything available in the native fs.stats package.

Chokidar example

// 'add', 'addDir' and 'change' events also receive stat() results as second
// argument when available: http://nodejs.org/api/fs.html#fs_class_fs_stats
watcher.on('change', function(path, stats) {
  if (stats) console.log('File', path, 'changed size to', stats.size);
});

Here's what is provided on the native fs.stats:

Stats {
  dev: 2114,
  ino: 48064969,
  mode: 33188,
  nlink: 1,
  uid: 85,
  gid: 100,
  rdev: 0,
  size: 527,
  blksize: 4096,
  blocks: 8,
  atimeMs: 1318289051000.1,
  mtimeMs: 1318289051000.1,
  ctimeMs: 1318289051000.1,
  birthtimeMs: 1318289051000.1,
  atime: Mon, 10 Oct 2011 23:24:11 GMT,
  mtime: Mon, 10 Oct 2011 23:24:11 GMT,
  ctime: Mon, 10 Oct 2011 23:24:11 GMT,
  birthtime: Mon, 10 Oct 2011 23:24:11 GMT }

Chokidar config options

chokidar.watch('file', {
  persistent: true,

  ignored: '*.txt',
  ignoreInitial: false,
  followSymlinks: true,
  cwd: '.',
  disableGlobbing: false,

  usePolling: false,
  interval: 100,
  binaryInterval: 300,
  alwaysStat: false, // <------- turn this on
  depth: 99,
  awaitWriteFinish: {
    stabilityThreshold: 2000,
    pollInterval: 100
  },

  ignorePermissionErrors: false,
  atomic: true // or a custom 'atomicity delay', in milliseconds (default 100)
});

CodePudding user response:

Chokidar does not provide a way to directly compare the contents of the file before and after the change. However, you can use it with fs library to detect the difference in the contents of the file.

const fs = require('fs');
const chokidar = require('chokidar');
const util = require('util');

let previousContent = '';

chokidar.watch('file.txt', {
  ignored: /(^|[\/\\])\../,
}).on('change', (path) => {
  const content = await fs.readFileSync(path, 'utf8');
  const match = content.match(/regex/);
  if (match && previousContent !== match[0]) {
      console.log(`change detected: ${match[0]}`);
      previousContent = match[0];
      // run your JS function here
  }
});
  • Related