Skip to content

Commit

Permalink
Allow to supress warnings for sanity checks (#359)
Browse files Browse the repository at this point in the history
* Allow to supress warnings for sanity checks

* Update sanity_checker.rb

* Update sanity_checker.rb

* Update stimulus_reflex.rb

Co-authored-by: leastbad <[email protected]>
  • Loading branch information
RolandStuder and leastbad authored Nov 11, 2020
1 parent 29ed629 commit da7f8b5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# frozen_string_literal: true

StimulusReflex.configure do |config|
# Enable/disable whether startup should be aborted when the sanity checks fail
# config.exit_on_failed_sanity_checks = true
# Enable/disable exiting / warning when the sanity checks fail options:
# `:exit` or `:warn` or `:ignore`
# config.on_failed_sanity_checks = :exit

# Override the parent class that the StimulusReflex ActionCable channel inherits from
# config.parent_channel = "ApplicationCable::Channel"
Expand Down
4 changes: 2 additions & 2 deletions lib/stimulus_reflex/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ def configuration
end

class Configuration
attr_accessor :exit_on_failed_sanity_checks, :parent_channel
attr_accessor :on_failed_sanity_checks, :parent_channel

def initialize
@exit_on_failed_sanity_checks = true
@on_failed_sanity_checks = :exit
@parent_channel = "ApplicationCable::Channel"
end
end
Expand Down
45 changes: 39 additions & 6 deletions lib/stimulus_reflex/sanity_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ class StimulusReflex::SanityChecker
JSON_VERSION_FORMAT = /(\d+\.\d+\.\d+.*)"/

def self.check!
return if StimulusReflex.config.on_failed_sanity_checks == :ignore

instance = new
instance.check_caching_enabled
instance.check_javascript_package_version
Expand Down Expand Up @@ -80,19 +82,50 @@ def package_json_path
Rails.root.join("node_modules", "stimulus_reflex", "package.json")
end

def initializer_path
@_initializer_path ||= Rails.root.join("config", "initializers", "stimulus_reflex.rb")
end

def warn_and_exit(text)
puts "WARNING:"
puts text
exit_with_info if StimulusReflex.config.exit_on_failed_sanity_checks
exit_with_info if StimulusReflex.config.on_failed_sanity_checks == :exit
end

def exit_with_info
puts
puts <<~INFO
If you know what you are doing and you want to start the application anyway,
you can add the following directive to an initializer:
StimulusReflex.config.exit_on_failed_sanity_checks = false
INFO

# bundle exec rails generate stimulus_reflex:config
if File.exist?(initializer_path)
puts <<~INFO
If you know what you are doing and you want to start the application anyway,
you can add the following directive to the StimulusReflex initializer,
which is located at #{initializer_path}
StimulusReflex.configure do |config|
config.on_failed_sanity_checks = :warn
end
INFO
else
puts <<~INFO
If you know what you are doing and you want to start the application anyway,
you can create a StimulusReflex initializer with the command:
bundle exec rails generate stimulus_reflex:config
Then open your initializer at
<RAILS_ROOT>/config/initializers/stimulus_reflex.rb
and then add the following directive:
StimulusReflex.configure do |config|
config.on_failed_sanity_checks = :warn
end
INFO
end
exit
end
end

0 comments on commit da7f8b5

Please sign in to comment.