image-asset-manager
Version:
A comprehensive image asset management tool for frontend projects
223 lines • 7.7 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RealTimeUpdateManagerImpl = void 0;
const FileWatcher_1 = require("./FileWatcher");
const WebSocketManager_1 = require("./WebSocketManager");
class RealTimeUpdateManagerImpl {
constructor(fileScanner, imageAnalyzer) {
this.projectPath = "";
this.serverData = null;
this.isWatching = false;
this.fileScanner = fileScanner;
this.imageAnalyzer = imageAnalyzer;
this.fileWatcher = new FileWatcher_1.FileWatcherImpl(fileScanner);
this.wsManager = new WebSocketManager_1.WebSocketManagerImpl();
}
initialize(server, projectPath, serverData) {
this.projectPath = projectPath;
this.serverData = serverData;
// Initialize WebSocket manager
this.wsManager.initialize(server);
this.wsManager.updateServerData(serverData);
// Setup file change handler
this.fileWatcher.onFileChange(this.handleFileChange.bind(this));
}
startWatching() {
if (this.isWatching || !this.projectPath) {
return;
}
const watchOptions = {
ignored: [
"**/node_modules/**",
"**/.git/**",
"**/dist/**",
"**/build/**",
"**/.next/**",
"**/.nuxt/**",
"**/.cache/**",
"**/coverage/**",
],
persistent: true,
ignoreInitial: true,
followSymlinks: false,
depth: 10,
};
this.fileWatcher.watch(this.projectPath, watchOptions);
this.isWatching = true;
}
stopWatching() {
if (!this.isWatching) {
return;
}
this.fileWatcher.unwatch();
this.isWatching = false;
}
updateServerData(data) {
this.serverData = data;
this.wsManager.updateServerData(data);
}
async handleFileChange(event) {
try {
// Broadcast the file change immediately
this.wsManager.handleFileChange(event);
// For add/change events, we might want to trigger a partial rescan
if (event.type === "add" || event.type === "change") {
await this.handleFileAddOrChange(event);
}
else if (event.type === "unlink") {
await this.handleFileDelete(event);
}
}
catch (error) {
console.error("📡 Error handling file change:", error);
}
}
async handleFileAddOrChange(event) {
if (!this.serverData || !event.file) {
return;
}
try {
// Update the server data with the new/changed file
const existingFileIndex = this.serverData.images.findIndex((img) => img.path === event.file.path);
if (existingFileIndex >= 0) {
// Update existing file
this.serverData.images[existingFileIndex] = event.file;
}
else {
// Add new file
this.serverData.images.push(event.file);
}
// Update statistics
await this.updateStatistics();
// Broadcast updated data
this.wsManager.updateServerData(this.serverData);
}
catch (error) {
console.error("📡 Error handling file add/change:", error);
}
}
async handleFileDelete(event) {
if (!this.serverData) {
return;
}
try {
// Remove the file from server data
const initialLength = this.serverData.images.length;
this.serverData.images = this.serverData.images.filter((img) => img.path !== event.path);
if (this.serverData.images.length < initialLength) {
// Update statistics
await this.updateStatistics();
// Broadcast updated data
this.wsManager.updateServerData(this.serverData);
}
}
catch (error) {
console.error("📡 Error handling file delete:", error);
}
}
async updateStatistics() {
if (!this.serverData) {
return;
}
try {
// Recalculate basic statistics
const totalImages = this.serverData.images.length;
const totalSize = this.serverData.images.reduce((sum, img) => sum + img.size, 0);
// Update category breakdown
const categoryBreakdown = {};
this.serverData.images.forEach((img) => {
categoryBreakdown[img.category] =
(categoryBreakdown[img.category] || 0) + 1;
});
// Update format breakdown
const formatBreakdown = {};
this.serverData.images.forEach((img) => {
const ext = img.extension.toLowerCase();
formatBreakdown[ext] = (formatBreakdown[ext] || 0) + 1;
});
// Update size breakdown (simple categorization)
const sizeBreakdown = {
small: 0, // < 10KB
medium: 0, // 10KB - 100KB
large: 0, // > 100KB
};
this.serverData.images.forEach((img) => {
if (img.size < 10240) {
sizeBreakdown.small++;
}
else if (img.size < 102400) {
sizeBreakdown.medium++;
}
else {
sizeBreakdown.large++;
}
});
// Update server data statistics
this.serverData.stats = {
...this.serverData.stats,
totalImages,
totalSize,
categoryBreakdown,
formatBreakdown,
sizeBreakdown,
};
}
catch (error) {
console.error("📡 Error updating statistics:", error);
}
}
close() {
this.stopWatching();
this.wsManager.close();
}
// Utility methods
isWatchingFiles() {
return this.isWatching;
}
getWatchedPath() {
return this.projectPath;
}
getClientCount() {
return this.wsManager.getClientCount();
}
// Manual trigger for full rescan (useful for testing or manual refresh)
async triggerFullRescan() {
if (!this.projectPath || !this.serverData) {
return;
}
try {
// Perform full scan
const images = await this.fileScanner.scanDirectory(this.projectPath, {
extensions: [
".png",
".jpg",
".jpeg",
".gif",
".svg",
".webp",
".ico",
".bmp",
".tiff",
],
excludePatterns: [
"**/node_modules/**",
"**/.git/**",
"**/dist/**",
"**/build/**",
],
recursive: true,
maxDepth: 10,
});
// Update server data
this.serverData.images = images;
await this.updateStatistics();
// Broadcast updated data
this.wsManager.updateServerData(this.serverData);
}
catch (error) {
console.error("📡 Error during full rescan:", error);
}
}
}
exports.RealTimeUpdateManagerImpl = RealTimeUpdateManagerImpl;
//# sourceMappingURL=RealTimeUpdateManager.js.map