Class: ShopifyCLI::Theme::DevServer::Watcher

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/shopify_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:, ignore_filter: nil, poll: false) ⇒ Watcher

Returns a new instance of Watcher.



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

def initialize(ctx, theme:, syncer:, ignore_filter: nil, poll: false)
  @ctx = ctx
  @theme = theme
  @syncer = syncer
  @ignore_filter = ignore_filter
  @listener = FileSystemListener.new(root: @theme.root, force_poll: poll,
    ignore_regex: @ignore_filter&.regexes)

  add_observer(self, :upload_files_when_changed)
end

Instance Method Details

#filter_remote_files(files) ⇒ Object



52
53
54
55
56
57
# File 'lib/shopify_cli/theme/dev_server/watcher.rb', line 52

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

#filter_theme_files(files) ⇒ Object



45
46
47
48
49
50
# File 'lib/shopify_cli/theme/dev_server/watcher.rb', line 45

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

#startObject



25
26
27
# File 'lib/shopify_cli/theme/dev_server/watcher.rb', line 25

def start
  @listener.start
end

#stopObject



29
30
31
# File 'lib/shopify_cli/theme/dev_server/watcher.rb', line 29

def stop
  @listener.stop
end

#upload_files_when_changed(modified, added, removed) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/shopify_cli/theme/dev_server/watcher.rb', line 33

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