Class: FluidCLI::Theme::DevServer::Watcher

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/fluid_cli/theme/dev_server/watcher.rb

Overview

Watches for file changes and publish events to the theme

Instance Method Summary collapse

Constructor Details

#initialize(ctx, theme:, syncer:, poll: false) ⇒ Watcher

Returns a new instance of Watcher.



14
15
16
17
18
19
20
21
# File 'lib/fluid_cli/theme/dev_server/watcher.rb', line 14

def initialize(ctx, theme:, syncer:, poll: false)
  @ctx = ctx
  @theme = theme
  @syncer = syncer
  @listener = FileSystemListener.new(root: @theme.root, force_poll: poll)

  add_observer(self, :upload_files_when_changed)
end

Instance Method Details

#filter_remote_files(files) ⇒ Object



49
50
51
52
53
# File 'lib/fluid_cli/theme/dev_server/watcher.rb', line 49

def filter_remote_files(files)
  files
    .select { |file| @syncer.remote_file?(file) }
    .map { |file| @theme[file] }
end

#filter_theme_files(files) ⇒ Object



43
44
45
46
47
# File 'lib/fluid_cli/theme/dev_server/watcher.rb', line 43

def filter_theme_files(files)
  files
    .select { |file| @theme.theme_file?(file) }
    .map { |file| @theme[file] }
end

#startObject



23
24
25
# File 'lib/fluid_cli/theme/dev_server/watcher.rb', line 23

def start
  @listener.start
end

#stopObject



27
28
29
# File 'lib/fluid_cli/theme/dev_server/watcher.rb', line 27

def stop
  @listener.stop
end

#upload_files_when_changed(modified, added, removed) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/fluid_cli/theme/dev_server/watcher.rb', line 31

def upload_files_when_changed(modified, added, removed)
  modified_theme_files = filter_theme_files(modified + added)
  if modified_theme_files.any?
    @syncer.enqueue_updates(modified_theme_files)
  end

  removed_theme_files = filter_remote_files(removed)
  if removed_theme_files.any?
    @syncer.enqueue_deletes(removed_theme_files)
  end
end