watch()
    {
        const globPattern = ['**', ...this.config.exclude.map((item) => `!${item}`)]
        const files = globby.sync(globPattern, { dot: true })
        const ignoreInitial = files.length > this.config.limit

        if(ignoreInitial)
        {
            console.log(`${chalk.green.bold('watcher')} - ${chalk.cyan('start wating')} - ${chalk.red(`File limit exceeded (${this.config.limit})`)}`)
        }

        // Set up
        this.watcher = chokidar.watch(
            this.path,
            {
                // ignored: /[\/\\]\./,
                ignored: this.excludeRegex,
                ignoreInitial: ignoreInitial,
                ignorePermissionErrors: true
            }
        )

        if(this.config.debug >= 1)
        {
            console.log(`${chalk.green.bold('watcher')} - ${chalk.cyan('start watching')} - ${chalk.cyan(this.path)}`)
        }

        // Add event
        this.watcher.on('add', (_path) =>
        {
            // Set up
            const relativePath = _path.replace(this.path, '.')
            const mimeType = mime.getType(relativePath)
            const extension = path.extname(relativePath)
            const file = {}

            file.path = relativePath
            file.canRead = true

            // Test mime type
            if(mimeType && mimeType.match(/^(audio)|(video)|(image)/) && extension !== '.ts')
            {
                file.canRead = false
            }

            // Retrieve stats
            fs.stat(_path, (error, stats) =>
            {
                // Error
                if(error)
                {
                    console.log(`${chalk.green.bold('watcher')} - ${chalk.cyan('add')} - ${chalk.cyan(_path)} - ${chalk.red('error')}`)
                    return
                }

                // Max file size
                if(this.config.maxFileSize < stats.size)
                {
                    file.canRead = false
                }

                // Read
                fs.readFile(_path, (error, data) =>
                {
                    // Error
                    if(error)
                    {
                        console.log(`${chalk.green.bold('watcher')} - ${chalk.cyan('add')} - ${chalk.cyan(_path)} - ${chalk.red('error')}`)
                        return
                    }

                    if(file.canRead)
                    {
                        file.content = data.toString()
                    }

                    // Emit
                    this.socket.emit('create_file', file)
                })
            })

            // Debug
            if(this.config.debug >= 1)
            {
                console.log(`${chalk.green.bold('watcher')} - ${chalk.cyan('add')} - ${chalk.cyan(relativePath)}`)
            }
        })

        // Change event
        this.watcher.on('change', (_path) =>
        {
            // Set up
            const relativePath = _path.replace(this.path, '.')
            const mimeType = mime.getType(relativePath)
            const extension = path.extname(relativePath)
            const file = {}

            file.path = relativePath
            file.canRead = true

            // Test mime type
            if(mimeType && mimeType.match(/^(audio)|(video)|(image)/) && extension !== '.ts')
            {
                file.canRead = false
            }

            // Retrieve stats
            fs.stat(_path, (error, stats) =>
            {
                // Error
                if(error)
                {
                    console.log(`${chalk.green.bold('watcher')} - ${chalk.cyan('change')} - ${chalk.cyan(_path)} - ${chalk.red('error')}`)
                    return
                }

                // Test max file size
                if(this.config.maxFileSize < stats.size)
                {
                    file.canRead = false
                }

                // Read
                fs.readFile(_path, (error, data) =>
                {
                    // Error
                    if(error)
                    {
                        console.log(`${chalk.green.bold('watcher')} - ${chalk.cyan('change')} - ${chalk.cyan(_path)} - ${chalk.red('error')}`)
                        return
                    }

                    if(file.canRead)
                    {
                        file.content = data.toString()
                    }

                    // Emit
                    this.socket.emit('update_file', file)
                })
            })

            // Debug
            if(this.config.debug >= 1)
            {
                console.log(`${chalk.green.bold('watcher')} - ${chalk.cyan('change')} - ${chalk.cyan(relativePath)}`)
            }
        })

        // Unlink event
        this.watcher.on('unlink', (_path) =>
        {
            // Set up
            const relativePath = _path.replace(this.path, '.')

            // Emit
            this.socket.emit('delete_file', { path: relativePath })

            // Debug
            if(this.config.debug >= 1)
            {
                console.log(`${chalk.green.bold('watcher')} - ${chalk.cyan('unlink')} - ${chalk.cyan(relativePath)}`)
            }
        })

        // AddDir event
        this.watcher.on('addDir', (_path) =>
        {
            // Set up
            const relativePath = _path.replace(this.path, '.')

            // Emit
            this.socket.emit('create_folder', { path: relativePath })

            // Debug
            if(this.config.debug >= 1)
            {
                console.log(`${chalk.green.bold('watcher')} - ${chalk.cyan('addDir')} - ${chalk.cyan(relativePath)}`)
            }
        })

        // UnlinkDir event
        this.watcher.on('unlinkDir', (_path) =>
        {
            // Set up
            const relativePath = _path.replace(this.path, '.')

            // Emit
            this.socket.emit('delete_folder', { path: relativePath })

            // Debug
            if(this.config.debug >= 1)
            {
                console.log(`${chalk.green.bold('watcher')} - ${chalk.cyan('unlinkDir')} - ${chalk.cyan(relativePath)}`)
            }
        })
    }
Watch
Listen to modifications on files and folders