lib/lookbook/file_watcher.rb in lookbook-1.5.5 vs lib/lookbook/file_watcher.rb in lookbook-2.0.0.beta.0

- old
+ new

@@ -1,47 +1,31 @@ module Lookbook class FileWatcher - attr_reader :listeners, :force_polling + class << self + def new(...) + if evented? + Lookbook.logger.debug "Using `EventedFileUpdateChecker` for file watching" + else + Lookbook.logger.debug "The 'listen' gem was not found. Using `FileUpdateChecker` for file watching" + end - def initialize(force_polling: false) - @force_polling = force_polling - @listeners = [] - end - - def watch(paths, extensions = ".*", opts = nil, &block) - paths = PathUtils.normalize_paths(paths) - - if paths.any? - opts = opts.to_h - opts[:only] = /\.(#{Array(extensions).join("|")})$/ - - listeners << init_listener(paths, opts, &block) + file_watcher.new(...) end - end - def start - if listeners.any? - Lookbook.logger.debug "Starting listeners" - listeners.each { |l| l.start } + def evented? + !(file_watcher <= ActiveSupport::FileUpdateChecker) end - end - def stop - if listeners.any? - Lookbook.logger.debug "Stopping listeners" - listeners.each { |l| l.stop } - end - end + protected - protected + def file_watcher + @_file_watcher ||= begin + require_relative "./support/evented_file_update_checker" - def init_listener(paths, opts, &block) - Listen.to( - *paths, - **opts, - force_polling: force_polling - ) do |modified, added, removed| - block.call({modified: modified, added: added, removed: removed}) + EventedFileUpdateChecker + rescue LoadError + ActiveSupport::FileUpdateChecker + end end end end end