kist
Version:
Package Pipeline Processor
141 lines (140 loc) • 5.69 kB
JavaScript
"use strict";
// ============================================================================
// Import
// ============================================================================
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LiveWatcher = void 0;
const chokidar_1 = __importDefault(require("chokidar"));
const AbstractProcess_1 = require("../core/abstract/AbstractProcess");
const ConfigStore_1 = require("../core/config/ConfigStore");
// ============================================================================
// Class
// ============================================================================
/**
* LiveWatcher is a utility class for monitoring file and directory changes.
* It leverages the `chokidar` library to efficiently detect file changes and
* trigger appropriate callbacks.
*/
class LiveWatcher extends AbstractProcess_1.AbstractProcess {
// Constructor
// ========================================================================
/**
* Creates an instance of LiveWatcher.
* @param pathsToWatch - An array of paths to monitor for changes.
* @param ignoredPaths - A regular expression to specify paths or patterns
* to exclude from watching.
* @param onChange - A callback function that is executed when a file
* change is detected.
*/
constructor(
// private pathsToWatch: string[],
// private ignoredPaths: RegExp,
onChange) {
var _a, _b;
super();
// Parameters
// ========================================================================
/**
* The chokidar file watcher instance.
*/
this.watcher = null;
// Retrieve live reload configuration from ConfigStore
const liveReloadOptions = ConfigStore_1.ConfigStore.getInstance().get("options.live") || {};
this.pathsToWatch = (_a = liveReloadOptions.watchPaths) !== null && _a !== void 0 ? _a : [
"src/**/*",
"config/**/*",
"pack.yaml",
];
this.ignoredPaths = (_b = liveReloadOptions.ignoredPaths) !== null && _b !== void 0 ? _b : ["node_modules"];
this.onChange = onChange;
this.startWatching();
}
// Methods
// ========================================================================
/**
* Initializes and configures the chokidar watcher to monitor files and
* directories.
*/
setupWatchers() {
if (!this.watcher)
return;
this.watcher
.on("ready", () => {
this.logInfo("File watching is active. Waiting for changes...");
})
.on("change", (filePath) => {
this.logInfo(`File changed: ${filePath}`);
try {
this.onChange(filePath);
}
catch (error) {
this.logError(`Error handling file change for ${filePath}:`, error);
}
})
.on("error", (error) => {
this.logError("Watcher encountered an error:", error);
});
}
/**
* Starts the file watcher if it is not already running. If the watcher
* was stopped previously, it re-initializes the watcher.
*/
startWatching() {
if (this.watcher) {
this.logInfo("Watcher is already running.");
return;
}
this.logInfo("Starting file watcher...");
this.watcher = chokidar_1.default.watch(this.pathsToWatch, {
ignored: this.ignoredPaths,
persistent: true,
// Prevents initial "add" events on startup
ignoreInitial: true,
awaitWriteFinish: {
// Polling interval to check for file stability
pollInterval: 100,
// Waits for file to finish writing
stabilityThreshold: 100,
},
});
this.setupWatchers();
}
/**
* Stops the file watcher and releases its resources. This is useful when
* you need to clean up or reinitialize the watcher.
*/
stopWatching() {
return __awaiter(this, void 0, void 0, function* () {
if (this.watcher) {
yield this.watcher.close();
this.logInfo("File watching has been stopped.");
this.watcher = null;
}
});
}
/**
* Restarts the file watcher by first stopping the existing watcher (if
* any) and then starting a new one. This can be useful in scenarios where
* watcher configurations or paths have changed.
*/
restartWatcher() {
return __awaiter(this, void 0, void 0, function* () {
this.logInfo("Restarting file watcher...");
yield this.stopWatching();
this.startWatching();
});
}
}
exports.LiveWatcher = LiveWatcher;