UNPKG

image-asset-manager

Version:

A comprehensive image asset management tool for frontend projects

190 lines 6.41 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.FileWatcherImpl = void 0; const chokidar_1 = __importDefault(require("chokidar")); const events_1 = require("events"); const path_1 = __importDefault(require("path")); class FileWatcherImpl extends events_1.EventEmitter { constructor(fileScanner) { super(); this.watcher = null; this.watchedPath = ""; this.isWatching = false; this.fileScanner = fileScanner; } watch(watchPath, options) { if (this.isWatching) { this.unwatch(); } this.watchedPath = watchPath; // Configure chokidar options const chokidarOptions = { ignored: [ ...options.ignored, "**/node_modules/**", "**/.git/**", "**/dist/**", "**/build/**", "**/.next/**", "**/.nuxt/**", ], persistent: options.persistent, ignoreInitial: options.ignoreInitial, followSymlinks: options.followSymlinks, depth: options.depth, // Only watch image files glob: ["**/*.{png,jpg,jpeg,gif,svg,webp,ico,bmp,tiff}"], // Ignore case sensitivity caseSensitive: false, }; this.watcher = chokidar_1.default.watch(watchPath, chokidarOptions); this.setupEventHandlers(); this.isWatching = true; } unwatch() { if (this.watcher) { this.watcher.close(); this.watcher = null; } this.isWatching = false; this.watchedPath = ""; } onFileChange(callback) { this.on("fileChange", callback); } setupEventHandlers() { if (!this.watcher) return; // Handle file additions this.watcher.on("add", async (filePath) => { try { const imageFile = await this.processFileChange(filePath, "add"); this.emitFileChange({ type: "add", path: filePath, file: imageFile, }); } catch (error) { console.warn(`Failed to process added file ${filePath}:`, error); } }); // Handle file changes this.watcher.on("change", async (filePath) => { try { const imageFile = await this.processFileChange(filePath, "change"); this.emitFileChange({ type: "change", path: filePath, file: imageFile, }); } catch (error) { console.warn(`Failed to process changed file ${filePath}:`, error); } }); // Handle file deletions this.watcher.on("unlink", (filePath) => { this.emitFileChange({ type: "unlink", path: filePath, }); }); // Handle errors this.watcher.on("error", (error) => { console.error("File watcher error:", error); this.emit("error", error); }); // Handle ready event this.watcher.on("ready", () => { console.log("File watcher is ready and watching for changes"); this.emit("ready"); }); } async processFileChange(filePath, changeType) { try { // Check if file is an image if (!this.isImageFile(filePath)) { return undefined; } // Use FileScanner to process the single file const files = await this.fileScanner.scanDirectory(path_1.default.dirname(filePath), { extensions: [ ".png", ".jpg", ".jpeg", ".gif", ".svg", ".webp", ".ico", ".bmp", ".tiff", ], excludePatterns: [], recursive: false, maxDepth: 1, }); // Find the specific file that changed const fileName = path_1.default.basename(filePath); const imageFile = files.find((file) => file.name === fileName); return imageFile; } catch (error) { console.warn(`Failed to process file ${filePath}:`, error); return undefined; } } isImageFile(filePath) { const imageExtensions = [ ".png", ".jpg", ".jpeg", ".gif", ".svg", ".webp", ".ico", ".bmp", ".tiff", ]; const ext = path_1.default.extname(filePath).toLowerCase(); return imageExtensions.includes(ext); } emitFileChange(event) { this.emit("fileChange", event); } // Utility methods isWatchingPath() { return this.isWatching; } getWatchedPath() { return this.watchedPath; } // Implement incremental update mechanism async handleIncrementalUpdate(event) { // This method can be used by the main application to handle incremental updates // without triggering a full rescan switch (event.type) { case "add": if (event.file) { console.log(`New image file added: ${event.file.name}`); // The main application can add this file to its internal state } break; case "change": if (event.file) { console.log(`Image file modified: ${event.file.name}`); // The main application can update this file in its internal state } break; case "unlink": console.log(`Image file removed: ${path_1.default.basename(event.path)}`); // The main application can remove this file from its internal state break; } } } exports.FileWatcherImpl = FileWatcherImpl; //# sourceMappingURL=FileWatcher.js.map