|
| 1 | +import * as vscode from 'vscode'; |
| 2 | +import chokidar from 'chokidar'; |
| 3 | +import { directoryPath } from '../constants'; |
| 4 | +import path from 'path'; |
| 5 | +import stackRegistry from '../stack/registry'; |
| 6 | + |
| 7 | +export default function trackStackFileRenames( |
| 8 | + context: vscode.ExtensionContext |
| 9 | +) { |
| 10 | + const eventBuffer = []; |
| 11 | + const bufferTime = 1000; // Buffer time in milliseconds |
| 12 | + |
| 13 | + // Initialize Chokidar watcher. This tracks file renames and deletions. |
| 14 | + const watcher = chokidar.watch(directoryPath, { |
| 15 | + ignored: /^\./, |
| 16 | + persistent: true, |
| 17 | + }); |
| 18 | + |
| 19 | + // Function to process buffered events |
| 20 | + function processBufferedEvents() { |
| 21 | + const addedFiles = new Map(); |
| 22 | + const unlinkedFiles = new Map(); |
| 23 | + |
| 24 | + // Categorize events |
| 25 | + eventBuffer.forEach(({ event, filePath, timestamp }) => { |
| 26 | + const fileName = path.basename(filePath); |
| 27 | + if (event === 'add') { |
| 28 | + addedFiles.set(fileName, { filePath, timestamp }); |
| 29 | + } else if (event === 'unlink') { |
| 30 | + unlinkedFiles.set(fileName, { filePath, timestamp }); |
| 31 | + } |
| 32 | + }); |
| 33 | + |
| 34 | + // Process rename and delete events |
| 35 | + unlinkedFiles.forEach((unlinkedFile, unlinkedFileName) => { |
| 36 | + let isRename = false; |
| 37 | + addedFiles.forEach((addedFile, addedFileName) => { |
| 38 | + // Check if an 'add' event closely follows an 'unlink' event |
| 39 | + if ( |
| 40 | + Math.abs(addedFile.timestamp - unlinkedFile.timestamp) < bufferTime |
| 41 | + ) { |
| 42 | + const oldNameWithoutExt = unlinkedFileName.replace('.ts', ''); |
| 43 | + const newNameWithoutExt = addedFileName.replace('.ts', ''); |
| 44 | + |
| 45 | + // Handle file rename |
| 46 | + if (stackRegistry.nameExists(oldNameWithoutExt)) { |
| 47 | + stackRegistry.update(oldNameWithoutExt, newNameWithoutExt); |
| 48 | + console.log( |
| 49 | + `File renamed from ${oldNameWithoutExt} to ${newNameWithoutExt}` |
| 50 | + ); |
| 51 | + } |
| 52 | + addedFiles.delete(addedFileName); |
| 53 | + isRename = true; |
| 54 | + } |
| 55 | + }); |
| 56 | + |
| 57 | + // Handle file deletion |
| 58 | + if (!isRename) { |
| 59 | + const nameWithoutExt = unlinkedFileName.replace('.ts', ''); |
| 60 | + if (stackRegistry.nameExists(nameWithoutExt)) { |
| 61 | + stackRegistry.remove(nameWithoutExt); // Assuming you have a remove method in stackRegistry |
| 62 | + console.log(`File deleted: ${nameWithoutExt}`); |
| 63 | + } |
| 64 | + } |
| 65 | + }); |
| 66 | + |
| 67 | + eventBuffer.length = 0; // Clear the buffer after processing |
| 68 | + } |
| 69 | + |
| 70 | + // Event handler for watcher |
| 71 | + watcher.on('all', (event, filePath) => { |
| 72 | + console.log(`File ${filePath} has been ${event}ed`); |
| 73 | + eventBuffer.push({ event, filePath, timestamp: Date.now() }); |
| 74 | + |
| 75 | + // Set a timeout to process events in the buffer |
| 76 | + setTimeout(processBufferedEvents, bufferTime); |
| 77 | + }); |
| 78 | + |
| 79 | + context.subscriptions.push({ |
| 80 | + dispose: () => { |
| 81 | + watcher.close(); |
| 82 | + }, |
| 83 | + }); |
| 84 | +} |
0 commit comments