feat(Smartchok): Migrate to chokidar 4.x with picomatch glob support, update documentation and tests
This commit is contained in:
@ -1,8 +1,8 @@
|
||||
/**
|
||||
* autocreated commitinfo by @pushrocks/commitinfo
|
||||
* autocreated commitinfo by @push.rocks/commitinfo
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartchok',
|
||||
version: '1.0.34',
|
||||
description: 'A smart wrapper for chokidar to facilitate file watching with enhanced features.'
|
||||
version: '1.1.0',
|
||||
description: 'A smart wrapper for chokidar 4.x with glob pattern support and enhanced features.'
|
||||
}
|
||||
|
@ -18,7 +18,9 @@ export type TFsEvent =
|
||||
export class Smartchok {
|
||||
public watchStringmap = new Stringmap();
|
||||
public status: TSmartchokStatus = 'idle';
|
||||
private watcher: plugins.watcher;
|
||||
private watcher: plugins.chokidar.FSWatcher;
|
||||
private globPatterns: string[] = [];
|
||||
private globMatchers: Map<string, (path: string) => boolean> = new Map();
|
||||
private watchingDeferred = plugins.smartpromise.defer<void>(); // used to run things when watcher is initialized
|
||||
private eventObservablemap = new plugins.smartrx.Observablemap(); // register one observable per event
|
||||
|
||||
@ -94,17 +96,72 @@ export class Smartchok {
|
||||
public start(): Promise<void> {
|
||||
const done = plugins.smartpromise.defer<void>();
|
||||
this.status = 'starting';
|
||||
this.watcher = new plugins.watcher(
|
||||
this.watchStringmap.getStringArray().map((string) => {
|
||||
const result = this.getGlobBase(string);
|
||||
console.log(`Watching ${result} for changes`);
|
||||
return result;
|
||||
}),
|
||||
{
|
||||
depth: 4,
|
||||
recursive: true,
|
||||
|
||||
// Store original glob patterns and create matchers
|
||||
this.globPatterns = this.watchStringmap.getStringArray();
|
||||
const basePaths = new Set<string>();
|
||||
|
||||
this.globPatterns.forEach((pattern) => {
|
||||
const basePath = this.getGlobBase(pattern);
|
||||
basePaths.add(basePath);
|
||||
|
||||
// Create a picomatch matcher for each glob pattern
|
||||
const matcher = plugins.picomatch(pattern, {
|
||||
dot: true,
|
||||
basename: false
|
||||
});
|
||||
this.globMatchers.set(pattern, matcher);
|
||||
});
|
||||
|
||||
// Convert Set to Array for chokidar
|
||||
const watchPaths = Array.from(basePaths);
|
||||
console.log('Base paths to watch:', watchPaths);
|
||||
|
||||
this.watcher = plugins.chokidar.watch(watchPaths, {
|
||||
persistent: true,
|
||||
ignoreInitial: false,
|
||||
followSymlinks: false,
|
||||
depth: 4,
|
||||
awaitWriteFinish: {
|
||||
stabilityThreshold: 300,
|
||||
pollInterval: 100
|
||||
},
|
||||
ignored: (path: string, stats?: plugins.fs.Stats) => {
|
||||
// Don't filter during initialization - let chokidar watch everything
|
||||
// We'll filter events as they come in
|
||||
return false;
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
// Set up event handlers with glob filtering
|
||||
const fileEvents: Array<'add' | 'change' | 'unlink' | 'addDir' | 'unlinkDir'> =
|
||||
['add', 'addDir', 'change', 'unlink', 'unlinkDir'];
|
||||
|
||||
// Handle file events
|
||||
fileEvents.forEach(eventName => {
|
||||
this.watcher.on(eventName, (path: string, stats?: plugins.fs.Stats) => {
|
||||
// Only emit event if the path matches our glob patterns
|
||||
if (this.shouldWatchPath(path)) {
|
||||
this.eventObservablemap.getSubjectForEmitterEvent(this.watcher, eventName)
|
||||
.next([path, stats]);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Handle error events
|
||||
this.watcher.on('error', (error: Error) => {
|
||||
this.eventObservablemap.getSubjectForEmitterEvent(this.watcher, 'error')
|
||||
.next([error, undefined]);
|
||||
});
|
||||
|
||||
// Handle raw events
|
||||
this.watcher.on('raw', (eventType: string, path: string, details: any) => {
|
||||
if (this.shouldWatchPath(path)) {
|
||||
this.eventObservablemap.getSubjectForEmitterEvent(this.watcher, 'raw')
|
||||
.next([path, undefined]);
|
||||
}
|
||||
});
|
||||
|
||||
this.watcher.on('ready', () => {
|
||||
this.status = 'watching';
|
||||
this.watchingDeferred.resolve();
|
||||
@ -128,4 +185,36 @@ export class Smartchok {
|
||||
await closeWatcher();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a path should be watched based on glob patterns
|
||||
*/
|
||||
private shouldWatchPath(filePath: string): boolean {
|
||||
// Normalize the path - remove leading ./ if present
|
||||
let normalizedPath = filePath.replace(/\\/g, '/');
|
||||
if (normalizedPath.startsWith('./')) {
|
||||
normalizedPath = normalizedPath.substring(2);
|
||||
}
|
||||
|
||||
// Check if the path matches any of our glob patterns
|
||||
for (const [pattern, matcher] of this.globMatchers) {
|
||||
// Also normalize the pattern for comparison
|
||||
let normalizedPattern = pattern;
|
||||
if (normalizedPattern.startsWith('./')) {
|
||||
normalizedPattern = normalizedPattern.substring(2);
|
||||
}
|
||||
|
||||
// Try matching with both the original pattern and normalized
|
||||
if (matcher(normalizedPath) || matcher(filePath)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Also try matching without the leading path
|
||||
const withoutLeading = 'test/' + normalizedPath.split('test/').slice(1).join('test/');
|
||||
if (matcher(withoutLeading)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -19,9 +19,10 @@ export {
|
||||
}
|
||||
|
||||
// thirdparty scope
|
||||
// @ts-nocheck
|
||||
import watcher from '@tempfix/watcher';
|
||||
import * as chokidar from 'chokidar';
|
||||
import picomatch from 'picomatch';
|
||||
|
||||
export {
|
||||
watcher,
|
||||
chokidar,
|
||||
picomatch,
|
||||
}
|
||||
|
Reference in New Issue
Block a user