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:
- Create Resource named: ResourceName
- Create folder where you want to load your files: jsonfiles
- 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}`);
})