Class: Hanami::CLI::Commands::App::Assets::Watch

Inherits:
Command
  • Object
show all
Defined in:
lib/hanami/cli/commands/app/assets/watch.rb

Instance Method Summary collapse

Methods inherited from Command

#call, #initialize

Constructor Details

This class inherits a constructor from Hanami::CLI::Commands::App::Assets::Command

Instance Method Details

#execute_assets_command(assets, output_dir) ⇒ Object



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
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/hanami/cli/commands/app/assets/watch.rb', line 11

def execute_assets_command(assets, output_dir)
  out.puts "Starting Sprockets asset watch mode..."
  out.puts "Press Ctrl+C to stop"

  require "listen"

  asset_paths = find_asset_paths(assets)

  if asset_paths.empty?
    out.puts "No asset directories found. Looking for:"
    %w[app/assets lib/assets vendor/assets].each do |path|
      out.puts "  #{path}"
    end
    return
  end

  out.puts "Watching directories:"
  asset_paths.each { |path| out.puts "  #{path}" }

  listener = Listen.to(*asset_paths, only: /\.(css|js|scss|sass|coffee|erb)$/) do |modified, added, removed|
    out.puts "\nChanges detected:"
    (modified + added + removed).each { |file| out.puts "  #{file}" }

    begin
      out.puts "Recompiling assets..."
      assets.precompile(output_dir)
      out.puts "Assets recompiled successfully"
    rescue => e
      out.puts "Error recompiling assets: #{e.message}"
    end
  end

  listener.start

  trap("INT") { listener.stop; exit }

  sleep
rescue LoadError
  out.puts "Error: 'listen' gem is required for watch mode."
  out.puts "Add 'gem \"listen\"' to your Gemfile and run 'bundle install'"
  raise
rescue => e
  out.puts "Error in watch mode: #{e.message}"
  raise
end