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
47 lines
1.3 KiB
module RailsLiveReload
|
|
ENV_FILE = Rails.root.join('.env').to_s
|
|
ENV_SAMPLE_FILE = Rails.root.join('.env.sample').to_s
|
|
INITIALIZER = Rails.root.join('config/initializers/hot_changes.rb').to_s
|
|
CHECKSUMS = {} # rubocop:disable Style/MutableConstant
|
|
|
|
# MonkeyPath Watcher
|
|
class Watcher
|
|
def initialize
|
|
@files = {}
|
|
@sockets = []
|
|
|
|
# puts "Watching: #{root}"
|
|
# RailsLiveReload.patterns.each do |pattern, rule|
|
|
# puts " #{pattern} => #{rule}"
|
|
# end
|
|
|
|
build_tree
|
|
start_socket
|
|
start_listener
|
|
end
|
|
|
|
def reload_all
|
|
before_reload(files)
|
|
data = { event: RailsLiveReload::INTERNAL[:socket_events][:reload], files: }.to_json
|
|
@sockets.each { |socket, _| socket.puts data } # rubocop:disable Style/HashEachMethods
|
|
end
|
|
|
|
private
|
|
|
|
def before_reload(files)
|
|
perform_when_change(files, ENV_SAMPLE_FILE) { Hot::Constants.load_definitions }
|
|
perform_when_change(files, ENV_FILE) { Hot::Constants.load_values }
|
|
perform_when_change(files, INITIALIZER) { load Rails.root.join('config', 'initializers', 'hot_changes.rb') }
|
|
end
|
|
|
|
def perform_when_change(files, key, &)
|
|
return unless files.include?(key)
|
|
|
|
current_checksum = files[key]
|
|
return unless current_checksum != CHECKSUMS[key]
|
|
|
|
yield
|
|
CHECKSUMS[key] = current_checksum
|
|
end
|
|
end
|
|
end
|