Class: Breakfast::CompilationListener

Inherits:
Object
  • Object
show all
Defined in:
lib/breakfast/compilation_listener.rb

Constant Summary collapse

ASSET_EXTENSIONS =
["css", "js"].freeze
SOURCE_CODE_EXTENSIONS =
["rb", "html", "haml", "slim"].freeze

Class Method Summary collapse

Class Method Details

.broadcast(channel, data) ⇒ Object



45
46
47
# File 'lib/breakfast/compilation_listener.rb', line 45

def self.broadcast(channel, data)
  ActionCable.server.broadcast(channel, data)
end

.start(asset_output_folder:, source_code_folders:) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/breakfast/compilation_listener.rb', line 6

def self.start(asset_output_folder:, source_code_folders:)
  asset_listener =
    ::Listen.to(asset_output_folder) do |modified, added, removed|
      files = modified + added + removed

      ASSET_EXTENSIONS.each do |extension|
        if files.any? { |file| file.match(/\.#{extension}/) }
          broadcast(Breakfast::RELOAD_CHANNEL, { extension: extension })
        end
      end
    end

  rails_listener =
    ::Listen.to(*source_code_folders) do |modified, added, removed|
      files = modified + added + removed

      SOURCE_CODE_EXTENSIONS.each do |extension|
        matched = files.select { |file| file.match(/\.#{extension}/) }
        if matched.present?
          broadcast(Breakfast::RELOAD_CHANNEL, { extension: extension })

          file_names = matched
            .map { |file| file.split("/").last }
            .join(", ")
            .truncate(60)

          broadcast(Breakfast::STATUS_CHANNEL, {
            status: "success",
            message: "saved: #{file_names}",
            extension: extension
          })
        end
      end
    end

  asset_listener.start
  rails_listener.start
end