ddl-manager
Version:
store postgres procedures and triggers in files
160 lines • 5.3 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileWatcher = void 0;
const events_1 = require("events");
const node_watch_1 = __importDefault(require("node-watch"));
const path_1 = __importDefault(require("path"));
const FSEvent_1 = require("./FSEvent");
const FileReader_1 = require("./FileReader");
const utils_1 = require("./utils");
class FileWatcher extends events_1.EventEmitter {
constructor(rootFolders) {
super();
this.reader = new FileReader_1.FileReader(rootFolders);
this.state = this.reader.state;
this.rootFolders = rootFolders;
}
static async watch(rootFolders) {
const watcher = new FileWatcher(rootFolders);
await watcher.watch();
return watcher;
}
async watch() {
this.reader.read();
const handler = (eventType, rootFolder, subPath) => {
try {
this.onChangeWatcher(eventType, rootFolder, subPath);
}
catch (err) {
this.emitError({
subPath,
err
});
}
};
const promise = Promise.all(this.rootFolders.map(rootFolder => new Promise((resolve) => {
this.fsWatcher = node_watch_1.default(rootFolder, {
recursive: true,
delay: 5
}, (eventType, fullPath) => {
const subPath = path_1.default.relative(rootFolder, fullPath);
handler(eventType, rootFolder, subPath);
});
this.fsWatcher.on("ready", () => {
resolve();
});
})));
return promise;
}
stopWatch() {
if (this.fsWatcher) {
this.fsWatcher.close();
delete this.fsWatcher;
}
}
onChangeWatcher(eventType, rootFolderPath, subPath) {
// subPath path to file from rootFolderPath
subPath = utils_1.formatPath(subPath);
// full path to file or dir with rootFolderPath
const fullPath = rootFolderPath + "/" + subPath;
if (eventType === "remove") {
this.onRemoveDirOrFile(rootFolderPath, subPath);
}
else {
// ignore NOT sql files
if (!utils_1.isSqlFile(fullPath)) {
return;
}
// if file was parsed early
const parsedFile = this.state.files.find(file => file.path === subPath &&
file.folder === rootFolderPath);
if (parsedFile) {
this.onChangeFile(rootFolderPath, subPath, parsedFile);
}
else {
this.onCreateFile(rootFolderPath, subPath);
}
}
}
onRemoveDirOrFile(rootFolderPath, subPath) {
let hasChange = false;
let fsEvent = new FSEvent_1.FSEvent();
for (let i = 0, n = this.state.files.length; i < n; i++) {
const file = this.state.files[i];
if (file.folder !== rootFolderPath) {
continue;
}
const isRemoved = (
// removed this file
file.path === subPath ||
// removed dir, who contain this file
file.path.startsWith(subPath + "/"));
if (!isRemoved) {
continue;
}
// remove file, from state
this.state.removeFile(file);
i--;
n--;
// generate event
hasChange = true;
fsEvent = fsEvent.remove(file);
}
if (hasChange) {
this.emit("change", fsEvent);
}
}
onChangeFile(rootFolderPath, subPath, oldFile) {
let newFile = null;
try {
newFile = this.reader.parseFile(rootFolderPath, rootFolderPath + "/" + subPath);
}
catch (err) {
this.emitError({
subPath,
err
});
}
const hasChange = (JSON.stringify(newFile)
!==
JSON.stringify(oldFile));
if (!hasChange) {
return;
}
this.state.removeFile(oldFile);
let fsEvent = new FSEvent_1.FSEvent({
removed: [oldFile]
});
try {
if (newFile) {
this.state.addFile(newFile);
fsEvent = fsEvent.create(newFile);
}
}
catch (err) {
this.emitError({
subPath,
err
});
}
this.emit("change", fsEvent);
}
onCreateFile(rootFolderPath, subPath) {
const file = this.reader.parseFile(rootFolderPath, rootFolderPath + "/" + subPath);
if (!file) {
return;
}
this.state.addFile(file);
const fsEvent = new FSEvent_1.FSEvent({ created: [file] });
this.emit("change", fsEvent);
}
emitError(params) {
const outError = utils_1.prepareError(params.err, params.subPath);
this.emit("error", outError);
}
}
exports.FileWatcher = FileWatcher;
//# sourceMappingURL=FileWatcher.js.map