Use Chokidar to view files with specific sizes.

I need to add support for this feature in my application. My current implementation is very simple:

            this.watcher.on("add", (pathName: string) => {
                this.sendNotifyAction(new NotifyAction(PathEvent.Add, pathName));
            }).on("change", (pathName: string) => {
                this.sendNotifyAction(new NotifyAction(PathEvent.Change, pathName));
            }).on("unlink", (pathName: string) => {
                this.sendNotifyAction(new NotifyAction(PathEvent.Delete, pathName));
            }).on("ready", () => {
                this.sendReadinessNotification();
            });

Now I want to have something like:

private acceptedFileExtensions: string[] = ['.txt', '.docx', '.xlx', ...]

And use this array of extensions inside Chokidar. Therefore, if the file in the observed directory has an extension from the list, send a notification; if not, do nothing.

I saw a similar question at https://stackoverflow.com/a/166186/16/16 , but this is not what I really need.

Filtering inside the callback functions does not look very good, but I see no other options. Please inform.

Thank.

+1
source share
1 answer

@robertklep, chokidar . :

private buildWildcardList(path:string): string[] {
        let result: string[] = [];
        _.each(this.acceptedFileExtensions, (extension: string) => {
           result.push(path + '/**/*' + extension);
        });

       return result;
    }

let wildcardList: string[] = this.buildWildcardList(path);
this.watcher = chokidar.watch(wildcardList, watchOptions);
0

Source: https://habr.com/ru/post/1660087/


All Articles