pixelplanet/src/core/fsWatcher.js
HF 8d41c6533d don't require server restart on client changes,
by watching the asset files and reloading if changed
2023-12-13 11:11:25 +01:00

56 lines
1.2 KiB
JavaScript

/*
* Watch for filesystem changes
*/
import fs from 'fs';
import path from 'path';
import logger from './logger';
import { ASSET_DIR } from './config';
class FsWatcher {
#path;
#timeout = null;
#listeners = [];
filetypes;
delay;
constructor(watchPath, { delay = 5000, filetypes = [] }) {
if (!watchPath) {
throw new Error('Must define a path to watch');
}
this.#path = watchPath;
this.delay = delay;
this.filetypes = filetypes;
this.initialize();
}
initialize() {
const watchPath = this.#path;
fs.watch(watchPath, (eventType, filename) => {
if (filename && this.filetypes.length) {
const ext = filename.split('.').pop();
if (!this.filetypes.includes(ext)) {
return;
}
}
if (this.#timeout) {
clearTimeout(this.#timeout);
}
this.#timeout = setTimeout(() => {
logger.info('ASSET CHANGE, detected change in asset files');
this.#listeners.forEach((cb) => cb(eventType, filename));
}, this.delay);
});
}
onChange(cb) {
this.#listeners.push(cb);
}
}
const assetWatcher = new FsWatcher(
path.join(__dirname, 'public', ASSET_DIR),
{ filetypes: ['js', 'css'] },
);
export default assetWatcher;