Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add faster alternative for checking mjml version #127

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/mjml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def self.run_mjml(args, mjml_bin: valid_mjml_binary)
def self.valid_mjml_binary
self.valid_mjml_binary = @@valid_mjml_binary ||
check_for_custom_mjml_binary ||
check_for_node_modules_mjml_binary ||
check_for_package_mjml_binary ||
check_for_global_mjml_binary ||
check_for_mrml_binary
Expand All @@ -73,6 +74,15 @@ def self.check_for_custom_mjml_binary
'it is a valid MJML binary. Please check your configuration.'
end

def self.check_for_node_modules_mjml_binary
package_json = JSON.parse(Rails.root.join('node_modules/mjml/package.json').read)
if package_json['version'].start_with?(Mjml.mjml_binary_version_supported)
Rails.root.join('node_modules/.bin/mjml').to_s
end
rescue StandardError
false
end

def self.check_for_mjml_package(package_manager, bin_command, binary_path)
stdout, _, status = Open3.capture3("which #{package_manager}")
return unless status.success?
Expand Down
9 changes: 9 additions & 0 deletions test/mjml_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,15 @@ class NotifierMailerTest < ActiveSupport::TestCase
'but MJML-Rails could not validate that it is a valid MJML binary'))
end

it 'checks for node_modules first to avoid running the binary' do
Rails.stubs(:root).returns(Pathname.new('.'))
Mjml.stubs(:check_for_package_mjml_binary).never
Mjml.stubs(:check_for_global_mjml_binary).never
Mjml.stubs(:check_for_mrml_binary).never

expect(Mjml.valid_mjml_binary.to_s).must_match(%r{node_modules/.bin/mjml})
end

it 'can use MRML and check for a valid binary' do
Mjml.use_mrml = true
Mjml.stubs(:check_for_custom_mjml_binary).returns(false)
Expand Down
Loading