<commit_before>'use strict'

const path    = require('path')
const niceTry = require('nice-try')
const bs      = niceTry(() => require('browser-sync'))
const cache   = require('./cache')

/**
 * Get a list of files to watch.
 * @returns {Array} files
 */
const getFiles = function() {

	// Exclude the following files
	const excludedFiles = [
		'!**/.git',
		'!**/CVS',
		'!**/.svn',
		'!**/.hg',
		'!**/.lock-wscript',
		'!**/.wafpickle-N',
		'!**/*.swp',
		'!**/.DS_Store',
		'!**/._*',
		'!**/npm-debug.log',
		'!**/node_modules',
		'!**/bower_components'
	]

	// Include the following files
	const includedFiles = [
		'**/*'
	]

	// Return all ignored files
	return [
		...excludedFiles,
		...includedFiles
	]

}

/**
 * Flushes the cache and reloads the site.
 * Should be executed when a file gets updated.
 * @param {String} event
 * @param {String} file
 */
const eventHandler = function(event, file) {

	// Get the extension of the file
	const extension = path.extname(file)

	// Flush the cache no matter what event was send by Chokidar.
	// This ensures that we serve the latest files when the user reloads the site.
	cache.flush(extension)

	// Chokidar always sends an 'event' property - which could be 'add',
	// 'unlink' etc so we need to check for that and only respond to 'change'.
	if (event==='change') bs.reload()

}

/**
 * Serve a directory and reload the page when files change.
 * @public
 * @param {String} srcPath - Path to the source folder.
 * @param {Function} rewrite - URL rewrite middleware.
 * @param {Function} redirect - URL redirect middleware.
 * @param {Object} opts - Additional optional options.
 * @param {Function} next - The callback that handles the response. Receives the following properties: err.
 */
module.exports = function(srcPath, rewrite, redirect, opts, next) {

	if (bs==null) {
		return next(new Error('Rosid has been installed without optionalDependencies. Make sure that all optionalDependencies are installed before serving a site.'))
	}

	const server = {
		baseDir    : srcPath,
		middleware : [
			rewrite,
			redirect
		]
	}

	const files = {
		match   : getFiles(),
		fn      : eventHandler,
		options : {
			usePolling: opts.polling
		}
	}

	const defaults = {
		logPrefix : 'Rosid',
		server    : server,
		files     : [ files ],
		notify    : false,
		ghostMode : false
	}

	bs.create().init(defaults)

	next()

}<commit_msg>Fixed eventHandler not aware of browserSync instance<commit_after>'use strict'

const path        = require('path')
const niceTry     = require('nice-try')
const browserSync = niceTry(() => require('browser-sync'))
const cache       = require('./cache')

/**
 * Get a list of files to watch.
 * @returns {Array} files
 */
const getFiles = function() {

	// Exclude the following files
	const excludedFiles = [
		'!**/.git',
		'!**/CVS',
		'!**/.svn',
		'!**/.hg',
		'!**/.lock-wscript',
		'!**/.wafpickle-N',
		'!**/*.swp',
		'!**/.DS_Store',
		'!**/._*',
		'!**/npm-debug.log',
		'!**/node_modules',
		'!**/bower_components'
	]

	// Include the following files
	const includedFiles = [
		'**/*'
	]

	// Return all ignored files
	return [
		...excludedFiles,
		...includedFiles
	]

}

/**
 * Flushes the cache and reloads the site.
 * Should be executed when a file gets updated.
 * @param {Object} bs - Browsersync instance.
 * @param {String} event - Event sent by Chokidar.
 * @param {String} file - File affected by event.
 */
const eventHandler = function(bs, event, file) {

	// Get the extension of the file
	const extension = path.extname(file)

	// Flush the cache no matter what event was send by Chokidar.
	// This ensures that we serve the latest files when the user reloads the site.
	cache.flush(extension)

	// Chokidar always sends an 'event' property - which could be 'add',
	// 'unlink' etc so we need to check for that and only respond to 'change'.
	if (event==='change') bs.reload()

}

/**
 * Serve a directory and reload the page when files change.
 * @public
 * @param {String} srcPath - Path to the source folder.
 * @param {Function} rewrite - URL rewrite middleware.
 * @param {Function} redirect - URL redirect middleware.
 * @param {Object} opts - Additional optional options.
 * @param {Function} next - The callback that handles the response. Receives the following properties: err.
 */
module.exports = function(srcPath, rewrite, redirect, opts, next) {

	if (browserSync==null) {
		return next(new Error('Rosid has been installed without optionalDependencies. Make sure that all optionalDependencies are installed before serving a site.'))
	}

	const bs = browserSync.create()

	const server = {
		baseDir    : srcPath,
		middleware : [
			rewrite,
			redirect
		]
	}

	const files = {
		match   : getFiles(),
		fn      : eventHandler.bind(null, bs),
		options : {
			usePolling: opts.polling
		}
	}

	const defaults = {
		logPrefix : 'Rosid',
		server    : server,
		files     : [ files ],
		notify    : false,
		ghostMode : false
	}

	bs.init(defaults)

	next()

}
{'fraction_non_alphanumeric': 0.14348697394789578, 'fraction_numerical': 0.0, 'mean_word_length': 3.141908713692946, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5663075', 'n_tokens_mistral': 1708, 'n_tokens_neox': 1589, 'n_words': 530}