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
33 lines
1.0 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 = {}
|
|
|
|
# MonkeyPath Watcher
|
|
class Watcher
|
|
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) { HotConstants.set_definitions }
|
|
perform_when_change(files, ENV_FILE) { HotConstants.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
|