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.

33 lines
1.0 KiB

5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 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 = {}
  6. # MonkeyPath Watcher
  7. class Watcher
  8. def reload_all
  9. before_reload(files)
  10. data = { event: RailsLiveReload::INTERNAL[:socket_events][:reload], files: }.to_json
  11. @sockets.each { |socket, _| socket.puts data } # rubocop:disable Style/HashEachMethods
  12. end
  13. private
  14. def before_reload(files)
  15. perform_when_change(files, ENV_SAMPLE_FILE) { HotConstants.set_definitions }
  16. perform_when_change(files, ENV_FILE) { HotConstants.load_values }
  17. perform_when_change(files, INITIALIZER) { load Rails.root.join('config', 'initializers', 'hot_changes.rb') }
  18. end
  19. def perform_when_change(files, key, &)
  20. return unless files.include?(key)
  21. current_checksum = files[key]
  22. return unless current_checksum != CHECKSUMS[key]
  23. yield
  24. CHECKSUMS[key] = current_checksum
  25. end
  26. end
  27. end