2025-11-30 03:04:49 +00:00
|
|
|
import * as fs from 'fs';
|
|
|
|
|
import * as path from 'path';
|
|
|
|
|
import * as smartrx from '@push.rocks/smartrx';
|
|
|
|
|
import type { IWatcher, IWatcherOptions, IWatchEvent, TWatchEventType } from './interfaces.js';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Node.js/Bun file watcher using native fs.watch API
|
|
|
|
|
*/
|
|
|
|
|
export class NodeWatcher implements IWatcher {
|
|
|
|
|
private watchers: Map<string, fs.FSWatcher> = new Map();
|
|
|
|
|
private watchedFiles: Set<string> = new Set();
|
|
|
|
|
private _isWatching = false;
|
2025-12-08 16:06:18 +00:00
|
|
|
|
|
|
|
|
// Debounce: pending emits per file path
|
|
|
|
|
private pendingEmits: Map<string, NodeJS.Timeout> = new Map();
|
2025-11-30 03:04:49 +00:00
|
|
|
|
|
|
|
|
public readonly events$ = new smartrx.rxjs.Subject<IWatchEvent>();
|
|
|
|
|
|
2025-12-08 16:06:18 +00:00
|
|
|
constructor(private options: IWatcherOptions) {}
|
2025-11-30 03:04:49 +00:00
|
|
|
|
2025-12-08 15:09:16 +00:00
|
|
|
/**
|
|
|
|
|
* Check if a file is a temporary file created by editors
|
|
|
|
|
*/
|
|
|
|
|
private isTemporaryFile(filePath: string): boolean {
|
|
|
|
|
const basename = path.basename(filePath);
|
|
|
|
|
// Editor temp files: *.tmp.*, *.swp, *.swx, *~, .#*
|
|
|
|
|
if (basename.includes('.tmp.')) return true;
|
|
|
|
|
if (basename.endsWith('.swp') || basename.endsWith('.swx')) return true;
|
|
|
|
|
if (basename.endsWith('~')) return true;
|
|
|
|
|
if (basename.startsWith('.#')) return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-30 03:04:49 +00:00
|
|
|
get isWatching(): boolean {
|
|
|
|
|
return this._isWatching;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async start(): Promise<void> {
|
|
|
|
|
if (this._isWatching) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// Start watching each base path
|
|
|
|
|
for (const basePath of this.options.basePaths) {
|
|
|
|
|
await this.watchPath(basePath, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this._isWatching = true;
|
|
|
|
|
|
|
|
|
|
// Perform initial scan to emit 'add' events for existing files
|
|
|
|
|
for (const basePath of this.options.basePaths) {
|
|
|
|
|
await this.scanDirectory(basePath, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Emit ready event
|
|
|
|
|
this.events$.next({ type: 'ready', path: '' });
|
|
|
|
|
} catch (error: any) {
|
|
|
|
|
this.events$.next({ type: 'error', path: '', error });
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async stop(): Promise<void> {
|
|
|
|
|
this._isWatching = false;
|
2025-12-08 16:06:18 +00:00
|
|
|
|
|
|
|
|
// Cancel all pending debounced emits
|
|
|
|
|
for (const timeout of this.pendingEmits.values()) {
|
|
|
|
|
clearTimeout(timeout);
|
|
|
|
|
}
|
|
|
|
|
this.pendingEmits.clear();
|
2025-11-30 03:04:49 +00:00
|
|
|
|
|
|
|
|
// Close all watchers
|
2025-12-08 16:06:18 +00:00
|
|
|
for (const [, watcher] of this.watchers) {
|
2025-11-30 03:04:49 +00:00
|
|
|
watcher.close();
|
|
|
|
|
}
|
|
|
|
|
this.watchers.clear();
|
|
|
|
|
this.watchedFiles.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Start watching a path (file or directory)
|
|
|
|
|
*/
|
|
|
|
|
private async watchPath(watchPath: string, depth: number): Promise<void> {
|
|
|
|
|
if (depth > this.options.depth) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const stats = await this.statSafe(watchPath);
|
|
|
|
|
if (!stats) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (stats.isDirectory()) {
|
|
|
|
|
// Watch the directory with recursive option (Node.js 20+ supports this on all platforms)
|
|
|
|
|
const watcher = fs.watch(
|
|
|
|
|
watchPath,
|
|
|
|
|
{ recursive: true, persistent: true },
|
|
|
|
|
(eventType, filename) => {
|
|
|
|
|
if (filename) {
|
|
|
|
|
this.handleFsEvent(watchPath, filename, eventType);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
watcher.on('error', (error) => {
|
|
|
|
|
this.events$.next({ type: 'error', path: watchPath, error });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.watchers.set(watchPath, watcher);
|
|
|
|
|
}
|
|
|
|
|
} catch (error: any) {
|
|
|
|
|
this.events$.next({ type: 'error', path: watchPath, error });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-12-08 16:06:18 +00:00
|
|
|
* Handle raw fs.watch events - debounce and normalize them
|
2025-11-30 03:04:49 +00:00
|
|
|
*/
|
2025-12-08 16:06:18 +00:00
|
|
|
private handleFsEvent(
|
2025-11-30 03:04:49 +00:00
|
|
|
basePath: string,
|
|
|
|
|
filename: string,
|
|
|
|
|
eventType: 'rename' | 'change' | string
|
2025-12-08 16:06:18 +00:00
|
|
|
): void {
|
2025-11-30 03:04:49 +00:00
|
|
|
const fullPath = path.join(basePath, filename);
|
|
|
|
|
|
2025-12-08 15:09:16 +00:00
|
|
|
// Skip temporary files created by editors (atomic saves)
|
|
|
|
|
if (this.isTemporaryFile(fullPath)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-08 16:06:18 +00:00
|
|
|
// Debounce: cancel any pending emit for this file
|
|
|
|
|
const existing = this.pendingEmits.get(fullPath);
|
|
|
|
|
if (existing) {
|
|
|
|
|
clearTimeout(existing);
|
2025-11-30 03:04:49 +00:00
|
|
|
}
|
|
|
|
|
|
2025-12-08 16:06:18 +00:00
|
|
|
// Schedule debounced emit
|
|
|
|
|
const timeout = setTimeout(() => {
|
|
|
|
|
this.pendingEmits.delete(fullPath);
|
|
|
|
|
this.emitFileEvent(fullPath, eventType);
|
|
|
|
|
}, this.options.debounceMs);
|
|
|
|
|
|
|
|
|
|
this.pendingEmits.set(fullPath, timeout);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Emit the actual file event after debounce
|
|
|
|
|
*/
|
|
|
|
|
private async emitFileEvent(
|
|
|
|
|
fullPath: string,
|
|
|
|
|
eventType: 'rename' | 'change' | string
|
|
|
|
|
): Promise<void> {
|
2025-11-30 03:04:49 +00:00
|
|
|
try {
|
|
|
|
|
const stats = await this.statSafe(fullPath);
|
|
|
|
|
|
|
|
|
|
if (eventType === 'rename') {
|
|
|
|
|
// 'rename' can mean add or unlink - check if file exists
|
|
|
|
|
if (stats) {
|
|
|
|
|
// File exists - it's either a new file or was renamed to this location
|
|
|
|
|
if (stats.isDirectory()) {
|
|
|
|
|
if (!this.watchedFiles.has(fullPath)) {
|
|
|
|
|
this.watchedFiles.add(fullPath);
|
|
|
|
|
this.events$.next({ type: 'addDir', path: fullPath, stats });
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2025-12-08 15:09:16 +00:00
|
|
|
const wasWatched = this.watchedFiles.has(fullPath);
|
|
|
|
|
this.watchedFiles.add(fullPath);
|
|
|
|
|
this.events$.next({
|
|
|
|
|
type: wasWatched ? 'change' : 'add',
|
|
|
|
|
path: fullPath,
|
|
|
|
|
stats
|
|
|
|
|
});
|
2025-11-30 03:04:49 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// File doesn't exist - it was deleted
|
|
|
|
|
if (this.watchedFiles.has(fullPath)) {
|
|
|
|
|
const wasDir = this.isKnownDirectory(fullPath);
|
|
|
|
|
this.watchedFiles.delete(fullPath);
|
|
|
|
|
this.events$.next({
|
|
|
|
|
type: wasDir ? 'unlinkDir' : 'unlink',
|
|
|
|
|
path: fullPath
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if (eventType === 'change') {
|
2025-12-08 16:06:18 +00:00
|
|
|
// File was modified
|
2025-11-30 03:04:49 +00:00
|
|
|
if (stats && !stats.isDirectory()) {
|
2025-12-08 16:06:18 +00:00
|
|
|
const wasWatched = this.watchedFiles.has(fullPath);
|
|
|
|
|
if (!wasWatched) {
|
|
|
|
|
// This is actually an 'add' - file wasn't being watched before
|
|
|
|
|
this.watchedFiles.add(fullPath);
|
|
|
|
|
this.events$.next({ type: 'add', path: fullPath, stats });
|
|
|
|
|
} else {
|
|
|
|
|
this.events$.next({ type: 'change', path: fullPath, stats });
|
2025-11-30 03:04:49 +00:00
|
|
|
}
|
2025-12-08 16:06:18 +00:00
|
|
|
} else if (!stats && this.watchedFiles.has(fullPath)) {
|
|
|
|
|
// File was deleted
|
|
|
|
|
this.watchedFiles.delete(fullPath);
|
|
|
|
|
this.events$.next({ type: 'unlink', path: fullPath });
|
2025-11-30 03:04:49 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (error: any) {
|
|
|
|
|
this.events$.next({ type: 'error', path: fullPath, error });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Scan directory and emit 'add' events for existing files
|
|
|
|
|
*/
|
|
|
|
|
private async scanDirectory(dirPath: string, depth: number): Promise<void> {
|
|
|
|
|
if (depth > this.options.depth) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const entries = await fs.promises.readdir(dirPath, { withFileTypes: true });
|
|
|
|
|
|
|
|
|
|
for (const entry of entries) {
|
|
|
|
|
const fullPath = path.join(dirPath, entry.name);
|
2025-12-08 16:06:18 +00:00
|
|
|
|
|
|
|
|
// Skip temp files during initial scan too
|
|
|
|
|
if (this.isTemporaryFile(fullPath)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-30 03:04:49 +00:00
|
|
|
const stats = await this.statSafe(fullPath);
|
|
|
|
|
|
|
|
|
|
if (!stats) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (entry.isDirectory()) {
|
|
|
|
|
this.watchedFiles.add(fullPath);
|
|
|
|
|
this.events$.next({ type: 'addDir', path: fullPath, stats });
|
|
|
|
|
await this.scanDirectory(fullPath, depth + 1);
|
|
|
|
|
} else if (entry.isFile()) {
|
|
|
|
|
this.watchedFiles.add(fullPath);
|
|
|
|
|
this.events$.next({ type: 'add', path: fullPath, stats });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (error: any) {
|
|
|
|
|
if (error.code !== 'ENOENT' && error.code !== 'EACCES') {
|
|
|
|
|
this.events$.next({ type: 'error', path: dirPath, error });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Safely stat a path, returning null if it doesn't exist
|
|
|
|
|
*/
|
|
|
|
|
private async statSafe(filePath: string): Promise<fs.Stats | null> {
|
|
|
|
|
try {
|
|
|
|
|
if (this.options.followSymlinks) {
|
|
|
|
|
return await fs.promises.stat(filePath);
|
|
|
|
|
} else {
|
|
|
|
|
return await fs.promises.lstat(filePath);
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if a path was known to be a directory (for proper unlink event type)
|
|
|
|
|
*/
|
|
|
|
|
private isKnownDirectory(filePath: string): boolean {
|
|
|
|
|
// Check if any watched files are children of this path
|
|
|
|
|
for (const watched of this.watchedFiles) {
|
|
|
|
|
if (watched.startsWith(filePath + path.sep)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|