rexuws
Version:
An express-like framework built on top of uWebsocket.js aims at simple codebase and high performance
57 lines (56 loc) • 1.91 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/* eslint-disable consistent-return */
const chokidar_1 = __importDefault(require("chokidar"));
class FileWatcher {
constructor(aliases, cb) {
this._ready = false;
this._aliases = aliases;
this._callbacks = cb;
this._watcher = chokidar_1.default.watch(aliases, {
awaitWriteFinish: true,
});
this.init();
}
getRelativePath(path) {
for (let i = 0; i < this._aliases.length; i++) {
if (path.indexOf(this._aliases[i]) !== -1) {
return {
filePath: path.replace(`${this._aliases[i]}/`, ''),
baseUrl: this._aliases[i],
};
}
}
return undefined;
}
init() {
this._watcher
.on('ready', () => {
this._ready = true;
})
.on('add', (path, stats) => {
if (this._ready) {
const { baseUrl, filePath } = this.getRelativePath(path);
return this._callbacks.add(baseUrl, filePath, stats);
}
})
.on('unlink', (path) => {
if (this._ready) {
const { baseUrl, filePath } = this.getRelativePath(path);
return this._callbacks.unlink(baseUrl, filePath);
}
});
if (this._callbacks.change) {
this._watcher.on('change', (path, stats) => {
if (this._ready) {
const { baseUrl, filePath } = this.getRelativePath(path);
return this._callbacks.change(baseUrl, filePath, stats);
}
});
}
}
}
exports.default = FileWatcher;