During script runs – that change files – I frequently need to check which files have been modified by the scripts, especially in CRON automated tasks. This allows me to take appropriate actions depending on the state change.

The following post shows how we can monitor file state changes in . For this purpose we will use the file-state-monitor npm package.

You can install using npm as usual.

npm i file-state-monitor --save

An example code for using the file monitor is shown below.

const FileMonitor = require('file-state-monitor').FileMonitor;
const LastModifiedState = require('file-state-monitor').LastModifiedState
 
let monitor = new FileMonitor(LastModifiedState);
 
// The state changes of the files will be stored in the below file
let stateFile = 'states.json';
let loaded = monitor.load(stateFile);
 
//  to monitor for file changes, this can be a single file or directory
monitor.monitorPath('./Hello ');
 
let changedFiles = monitor.getChangedFiles();

Once the above code is run the ‘changedFiles’ variable will hold a list of files that have been changed. This is a map object so we will need to read it as below.

for (var [key, value] of changedFiles.entries()) {
  console.log(key + ' = ' + value);
}

The file or directory to monitor is set using the ‘monitorPath’ method.

monitor.monitorPath('./Hello World');

You can also write the states of all the files directly to a file.

// Write the current file states back to disk
monitor.write(stateFile);

This will create a new json file ‘states.json’ with the following json content. This is for my example ‘Hello World’ directory, so your results will vary. This gives the file names and the last modified date. Note that this will write the state of all the files, even those that have not changed.

{  
   "files":[  
      {  
         "path":"Hello World\Hello World.js",
         "lastModified":1422340070000
      },
      {  
         "path":"Hello World\package.json",
         "lastModified":1422340070000
      },
      {  
         "path":"Hello World\README.md",
         "lastModified":1422340070000
      }
   ]
}

Whenever a file is modified the last modified timestamp will be updated in the json file.

You can also use other file state change implementations. Various file state implementations come bundled with the library, 4 currently:

LastModifiedState: Uses the last modified timestamp to detect if a file changed.

SizeState: Uses the file size to detect if a file changed.

ContentHashState: Computes a SHA-1 hash of the file’s content and uses that hash to detect if a file changed.

SmartState: Uses all of the above checks in series, marking a file changed as soon as the first check returns true. Starts with the inexpensive checks for modification time and file size, and only then does the expensive content hash check.

For small file directory it will be optimal to use the ‘ContentHashState’ or ‘SmartState’ implementation, while for large number of files ‘SizeState’ will be more computationally efficient. In practice it will depend on your usage requirement.

You can change the state method like below.

onst FileMonitor = require('file-state-monitor').FileMonitor;
const contentHashState = require('file-state-monitor').ContentHashState
 
let monitor = new FileMonitor(contentHashState);
 
// The state changes of the files will be stored in the below file
let stateFile = 'states.json';
let loaded = monitor.load(stateFile);
 
// Path to monitor for file changes
monitor.monitorPath('./Hello World');
 
let changedFiles = monitor.getChangedFiles();
 
monitor.write(stateFile);

This will return the hash values of all the files monitored.

{  
   "files":[  
      {  
         "path":"Hello World\dns.js",
         "sha1":"318501466ae0a6aa89ffbb4654cb59e3d6836d98"
      },
      {  
         "path":"Hello World\Hello World.js",
         "sha1":"e6e6d0332795762c0a6e639cf19eca3e6e687792"
      },
      {  
         "path":"Hello World\package.json",
         "sha1":"67de757bccfe11eb5187b40e459fd4b8c8b5bc2b"
      },
      {  
         "path":"Hello World\README.md",
         "sha1":"f8fdd837a804f41fa0892d3a85ac8da45e65631d"
      }
   ]
}



Source link
thanks you RSS link
( https://www.codediesel.com/nodejs/-file-changes-using-nodejs/)

LEAVE A REPLY

Please enter your comment!
Please enter your name here