-
Notifications
You must be signed in to change notification settings - Fork 9
/
mjml.rb
80 lines (66 loc) · 1.88 KB
/
mjml.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
require 'dry-configurable'
require 'mjml/logger'
require 'mjml/feature'
require 'mjml/parser'
# MJML library for ruby
module MJML
class BinaryNotFound < StandardError; end
# Constants
MIME_TYPE = 'text/mjml'.freeze
EXTENSION = '.mjml'.freeze
VERSION_3_REGEX = /^(\d\.\d\.\d)/i
VERSION_4_REGEX = /^mjml-cli: (\d\.\d\.\d)/i
extend Dry::Configurable
# Available settings
setting :bin_path
setting :debug
setting :logger
setting :minify_output
setting :validation_level
def self.setup!
# Init config
configure do |config|
config.bin_path = find_executable
config.debug = nil
config.logger = Logger.setup!(STDOUT)
config.minify_output = false
config.validation_level = :skip
end
end
def self.find_executable
local_path = File.expand_path('node_modules/.bin/mjml', Dir.pwd)
return local_path if File.file?(local_path)
`/usr/bin/env bash -c "which mjml"`.strip
end
def self.executable_version
@executable_version ||= extract_executable_version
end
def self.extract_executable_version
ver, _status = Open3.capture2(config.bin_path, '-V')
# mjml 3.x outputs version directly:
# 3.3.5
# --> just take this as the version
# mjml 4.x outputs two rows:
# mjml-core: 4.0.0
# mjml-cli: 4.0.0
# --> we take the second number as the version, since we call the cli
case ver.count("\n")
when 1
# one line, mjml 3.x
match = ver.match(VERSION_3_REGEX)
when 2
# two lines, might be 4.x
match = ver.match(VERSION_4_REGEX)
end
match.nil? ? nil : match[1]
rescue Errno::ENOENT => _e
raise BinaryNotFound, "mjml binary not found for path '#{config.bin_path}'"
end
def self.logger
config.logger
end
end
MJML.setup!
require 'tilt/mjml' if defined?(Tilt)
require 'sprockets/mjml' if defined?(Sprockets)
require 'mjml/railtie' if defined?(Rails)