FileWatcher (Real Time File additions, changes, and removals) (with chokidar & path)


#1

What this is:

  • JS system to watch files, and get the contents of the file on additions, changes, and removals.
  • Dynamic event based call back

Description:
FileWatcher is made in NodeJS with chokidar (watches files) and path (makes path’n easier).
It currently uses a function to register a Resource and a directory that you choose. It’s only setup to use one dir per resource at this time (It will change, need to find a better way to do onResourceStop, sometimes it just doesn’t work).

Downloads:
FileWatcher: https://github.com/LexTheGreat/FileWatcher
DirectDownload: https://github.com/LexTheGreat/FileWatcher/archive/master.zip

Config.js:

  • this.baseResource = ‘/resources/’; This is the path to your resources from the starting location of your .bat or .sh
  • this.GameModeDir = ‘’; If you plan to use some kind of sub folder this is where you would. If you leave this blank It’s ignored. Example “[system]/” requires / at end. Plan to make this dynamic.
  • this.debug = true; Set to false if you don’t want debug messages.

Exported Functions:

  • function createWatcher(resourceName, dir, fileType, encoding) | Register the resource with It’s name, the dir you want to watch, whitelisted file type (ex: “.json”), and endoding.
  • function removeWatcher(resourceName)

Dynamic Events:

  • “resourceName:watchedDirName:FileWatcherAdd” parameters: fileName, fileContents
  • “resourceName:watchedDirName:FileWatcherChange” parameters: fileName, fileContents
  • “resourceName:watchedDirName:FileWatcherRemove” parameters: fileName

Example:

  1. Create Resource named: ResourceName
  2. Create folder where you want to load your files: jsonfiles
  3. Use code below

Loading Jsonfiles:

console.log("[ResourceName] Waiting for FileWatcher...")
var FWTimeout = setInterval(FWTimeoutFunc, 5000);
function FWTimeoutFunc() {
	if (exports.FileWatcher.createWatcher != undefined && exports.FileWatcher.createWatcher != null) {
		exports.FileWatcher.createWatcher("ResourceName", "jsonfiles", ".json", 'utf8');
		clearInterval(FWTimeout);
	} else {
		console.log("[ResourceName] Waiting for FileWatcher...")
	}
}

onNet('ResourceName:jsonfiles:FileWatcherAdd', (fileName, fileContents) => {
	var jsonObject = JSON.parse(fileContents);
	console.log(`New File ${fileName}`);
})

onNet('ResourceName:jsonfiles:FileWatcherChange', (fileName, fileContents) => {
	var jsonObject = JSON.parse(fileContents);
	console.log(`Changed File ${fileName}`);
})

onNet('ResourceName:jsonfiles:FileWatcherRemove', (fileName) => {
	console.log(`Removed File ${fileName}`);
})

#2

hmmm okay cool im testing it now


#3

I have 3 resources using this at the moment to load json files everything seems to work fine let me know if you run into any issues!


#4

Not really understand what does it do… :confused:


#5

If you don’t understand then it’s not for you. The use for this resource is clearly stated in the OP.


#6

This was once again a classic example of a brainless, rare answer from a child’s brain…you are the King for help Dude, better you play with Lego :wink: three posting and one of them JHodson1 oh sorry i mean senseless…


#7

Basically exposes chokidar (https://github.com/paulmillr/chokidar) with events.

exports.FileWatcher.createWatcher(“ResourceName”, “jsonfiles”, “.json”, ‘utf8’);
“ResourceName:test:FileWatcherAdd”

The resource “ResourceName” will look in the folder “jsonfiles” for .json files.
I use it to update data (FileWatcherChange) for my resources real time with no resource restarts.

I can just change my .json file (teleport locations, spawn positions, etc) then it just updates in-game.


#8

Lmao, thanks.


#9

Lol, calm with the salt boi