diff --git a/.env.sample b/.env.sample index 0204e6f..1df2736 100644 --- a/.env.sample +++ b/.env.sample @@ -1,3 +1,5 @@ +# STATIC: restart server when changes! +#------------------------------------- RAILS_PORT=7500 DATABASE_HOST=localhost @@ -5,3 +7,9 @@ DATABASE_NAME=easy-going-rails DATABASE_USER=$DATABASE_NAME DATABASE_PASSWORD=$DATABASE_NAME +# HOT CONSTANTS +#-------------- +STIMULUS_DEBUG=false +LOG_ACTIVE_RECORD=false +LOG_ACTION_VIEW=false + diff --git a/app/reload/live_constants.rb b/app/reload/live_constants.rb deleted file mode 100644 index 28a1fcf..0000000 --- a/app/reload/live_constants.rb +++ /dev/null @@ -1,15 +0,0 @@ -# Development Constants -module LiveConstants - # constants - STIMULUS_DEBUG = false - LOG_ACTIVE_RECORD = false - LOG_ACTION_VIEW = false - - def self.reload! - ActionView::Base.logger.level = LOG_ACTION_VIEW ? :debug : :fatal - ActiveRecord::Base.logger.level = LOG_ACTIVE_RECORD ? :debug : :fatal - SemanticLogger[to_s].warn 'reloaded!' - end - - reload! -end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index fae87f7..7dc7f8b 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -11,7 +11,7 @@ <%= tag :meta, name: :viewport, content: 'width=device-width,initial-scale=1' %> <% if Rails.env.development? %> <%= tag :meta, name: 'turbo-prefetch', content: false %> - <%= tag :meta, name: 'stimulus-debug', content: LiveConstants::STIMULUS_DEBUG %> + <%= tag :meta, name: 'stimulus-debug', content: LiveConstants.stimulus_debug %> <% end%> diff --git a/config/environments/development.rb b/config/environments/development.rb index b7c97c5..d3128ee 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -95,4 +95,9 @@ Rails.application.configure do # rubocop:disable Metrics/BlockLength routes.default_url_options[:host] = '127.0.0.1' routes.default_url_options[:port] = ARGV[0] == '--port' ? ARGV[1] : 3000 # ie: Procfile.dev first two arguments + + config.after_initialize do + require Rails.root.join('lib', 'hot_constants', 'live_constants') + LiveConstants.reload! + end end diff --git a/config/initializers/rails_live_reload.rb b/config/initializers/rails_live_reload.rb index 2de780d..9ef43db 100644 --- a/config/initializers/rails_live_reload.rb +++ b/config/initializers/rails_live_reload.rb @@ -1,9 +1,7 @@ # frozen_string_literal: true -if defined?(RailsLiveReload) +if defined?(RailsLiveReload) && Rails.env.development? RailsLiveReload.configure do |config| - # config.url = "/rails/live/reload" - # Default watched folders & files config.watch %r{app/views/.+\.(erb|haml|slim)$} config.watch %r{(app|vendor)/(assets|javascript)/\w+/(.+\.(css|js|html|png|jpg|ts|jsx)).*}, reload: :always @@ -11,6 +9,7 @@ if defined?(RailsLiveReload) # USEFUL for tailwind changes!!!! config.watch %r{app/assets/builds/tailwind.css}, reload: :always - config.enabled = Rails.env.development? + # HOT Constants + config.watch(%r{/\.env}, reload: :always) end end diff --git a/lib/hot_constants/live_constants.rb b/lib/hot_constants/live_constants.rb new file mode 100644 index 0000000..bbb514f --- /dev/null +++ b/lib/hot_constants/live_constants.rb @@ -0,0 +1,24 @@ +require 'dotenv' + +# Development Constants +module LiveConstants + HOT = {} # rubocop:disable Style/MutableConstant + LOGGER = SemanticLogger[to_s] + + class << self + def stimulus_debug = load_boolean('STIMULUS_DEBUG', false) + def log_active_record = load_boolean('LOG_ACTIVE_RECORD', true) + def log_action_view = load_boolean('LOG_ACTION_VIEW', true) + def load_boolean(key, default) = HOT.fetch(key, default).to_s.downcase == 'true' + + def reload! + HOT.replace Dotenv.parse + # LOGGER.warn 'reloaded!' + + ActiveRecord::Base.logger.level = log_active_record ? :debug : :fatal + ActionView::Base.logger.level = log_action_view ? :debug : :fatal + end + end + + reload! +end diff --git a/lib/monkey_patches/rails_live_reload/watcher.rb b/lib/monkey_patches/rails_live_reload/watcher.rb new file mode 100644 index 0000000..ae20af1 --- /dev/null +++ b/lib/monkey_patches/rails_live_reload/watcher.rb @@ -0,0 +1,18 @@ +module RailsLiveReload + # MonkeyPath Watcher + class Watcher + ENV_FILE = Rails.root.join('.env').to_s + def reload_all + data = { + event: RailsLiveReload::INTERNAL[:socket_events][:reload], + files: + }.to_json + + LiveConstants.reload! if files.find { |change| change[0] == ENV_FILE } + + @sockets.each do |socket, _| + socket.puts data + end + end + end +end