You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.3 KiB

6 months ago
4 months ago
6 months ago
4 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
4 months ago
6 months ago
  1. module RailsLiveReload
  2. ENV_FILE = Rails.root.join('.env').to_s
  3. ENV_SAMPLE_FILE = Rails.root.join('.env.sample').to_s
  4. INITIALIZER = Rails.root.join('config/initializers/hot_changes.rb').to_s
  5. CHECKSUMS = {} # rubocop:disable Style/MutableConstant
  6. # MonkeyPath Watcher
  7. class Watcher
  8. def initialize
  9. @files = {}
  10. @sockets = []
  11. # puts "Watching: #{root}"
  12. # RailsLiveReload.patterns.each do |pattern, rule|
  13. # puts " #{pattern} => #{rule}"
  14. # end
  15. build_tree
  16. start_socket
  17. start_listener
  18. end
  19. def reload_all
  20. before_reload(files)
  21. data = { event: RailsLiveReload::INTERNAL[:socket_events][:reload], files: }.to_json
  22. @sockets.each { |socket, _| socket.puts data } # rubocop:disable Style/HashEachMethods
  23. end
  24. private
  25. def before_reload(files)
  26. perform_when_change(files, ENV_SAMPLE_FILE) { Hot::Constants.load_definitions }
  27. perform_when_change(files, ENV_FILE) { Hot::Constants.load_values }
  28. perform_when_change(files, INITIALIZER) { load Rails.root.join('config', 'initializers', 'hot_changes.rb') }
  29. end
  30. def perform_when_change(files, key, &)
  31. return unless files.include?(key)
  32. current_checksum = files[key]
  33. return unless current_checksum != CHECKSUMS[key]
  34. yield
  35. CHECKSUMS[key] = current_checksum
  36. end
  37. end
  38. end