Class: SvelteOnRails::Lib::Utils
- Inherits:
-
Object
- Object
- SvelteOnRails::Lib::Utils
- Defined in:
- lib/svelte_on_rails/lib/utils.rb
Class Method Summary collapse
- .asset_path(file_path) ⇒ Object
- .backtrace_until_sor(exception) ⇒ Object
- .color_supported? ⇒ Boolean
- .component_paths_uncached(component, current_controller_path) ⇒ Object
- .error_log(metrics_key, title, messages) ⇒ Object
- .file_exist_case_sensitive?(containing_dir, filename) ⇒ Boolean
- .gem_app_dir ⇒ Object
-
.log_with_severity(level, text, label, color_code) ⇒ Object
Internal: emits a tagged, colored block via Rails.logger.
- .manifest_file_mtime ⇒ Object
-
.puts_error(text) ⇒ Object
Defining methods to log errors and warnings using Rails logger.
-
.puts_warning(text) ⇒ Object
Defining method to log warnings using Rails logger.
- .ssr_server_log(msg) ⇒ Object
- .ssr_server_logfile ⇒ Object
- .vite_source_code_dir ⇒ Object
Class Method Details
.asset_path(file_path) ⇒ Object
9 10 11 12 13 |
# File 'lib/svelte_on_rails/lib/utils.rb', line 9 def self.asset_path(file_path) config = SvelteOnRails::Configuration.instance manifest = config.ssr_manifest manifest[file_path]['file'] end |
.backtrace_until_sor(exception) ⇒ Object
159 160 161 162 163 164 165 166 167 168 |
# File 'lib/svelte_on_rails/lib/utils.rb', line 159 def self.backtrace_until_sor(exception) trace = Array(exception.backtrace) last_match_index = trace.rindex do |line| line.include?("svelte-on-rails") || line.include?("svelte_on_rails") || line.include?(Rails.root.to_s) end return trace if last_match_index.nil? trace[0..last_match_index] end |
.color_supported? ⇒ Boolean
51 52 53 54 55 56 57 |
# File 'lib/svelte_on_rails/lib/utils.rb', line 51 def self.color_supported? return false if Rails.env.production? # Rails dev logger writes to STDOUT; check that it's a TTY. $stdout.tty? rescue false end |
.component_paths_uncached(component, current_controller_path) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/svelte_on_rails/lib/utils.rb', line 59 def self.component_paths_uncached(component, current_controller_path) file_basename = File.basename(component, ".svelte") conf = SvelteOnRails::Configuration.instance cnf = conf.configs aliases = cnf[:aliases] && cnf[:aliases].stringify_keys || {} ali = component[/^([^\/]*)\//, 1] # everything before the first "/" dir = component.match(/^.*(?=\/[^\/]*$)/).to_s path = if aliases[ali] File.join(aliases[ali], component.sub(/^#{ali}\//, '')) + '.svelte' elsif cnf[:default_components_root].to_s.match(/^app\/views/) @attempts = [ File.join('app', 'views', dir, "_#{file_basename}" + '.svelte'), File.join('app', 'views', dir, file_basename + '.svelte'), File.join('app', 'views', current_controller_path, dir, "_#{file_basename}" + '.svelte'), File.join('app', 'views', current_controller_path, dir, file_basename + '.svelte'), ] @attempts.find { |f| File.exist? Rails.root.join(f) } else cmp_root = File.join(cnf[:vite_source_dir], cnf[:components_subdir]) File.join(cmp_root, dir, file_basename + '.svelte') end vite_path = if path.to_s.match(/^#{cnf[:vite_source_dir]}/) "/#{path.sub(/^#{cnf[:vite_source_dir]}[\/]/, '')}" elsif path # we need the relative path from vite-source-dir # for matching with the frontend javascript source_dir = Pathname.new(cnf[:vite_source_dir]) Pathname.new(path).relative_path_from(source_dir).to_s end { path: path, component: [(dir.present? ? dir : nil), file_basename].compact.join('/'), file_basename: file_basename, name: file_basename.camelcase, vite_path: vite_path, failed_attempts: (@attempts unless path) } end |
.error_log(metrics_key, title, messages) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/svelte_on_rails/lib/utils.rb', line 112 def self.error_log(metrics_key, title, ) # failure title mapping failure_titles = { ssr_server_unreachable: 'SVELTE SSR SERVER UNREACHABLE!', build_failed: 'SVELTE BUILD FAILED!', render_failed: 'SVELTE RENDER FAILED!', build_warnings: 'warnings' } failure_title = failure_titles[metrics_key] raise "[SOR] Utils.error_log: invalid metrics_key" unless failure_title conf = SvelteOnRails::Configuration.instance # failures ranking conf.request_metrics[metrics_key] ||= 0 conf.request_metrics[metrics_key] += 1 ranked_key = failure_titles.keys.find { |k| conf.request_metrics[k] } conf.request_metrics[:failure_title] = failure_titles[ranked_key] if ranked_key # error log sep_char = '=' sep_line = sep_char * 100 inner_sep = '-' * 100 puts sep_line puts failure_title puts title if title puts sep_line puts .join("\n#{inner_sep}\n") puts sep_line puts "END #{title.to_s.downcase}" puts sep_line $stdout.flush end |
.file_exist_case_sensitive?(containing_dir, filename) ⇒ Boolean
15 16 17 18 19 20 21 22 23 |
# File 'lib/svelte_on_rails/lib/utils.rb', line 15 def self.file_exist_case_sensitive?(containing_dir, filename) # Combine the directory path and filename full_path = File.join(containing_dir, filename) # Check if the file exists and the path matches case-sensitively File.exist?(full_path) && Dir[File.join(containing_dir, "**/*")].any? do |f| f == full_path end end |
.gem_app_dir ⇒ Object
5 6 7 |
# File 'lib/svelte_on_rails/lib/utils.rb', line 5 def self.gem_app_dir File.('../../svelte_on_rails', __dir__) + '/' end |
.log_with_severity(level, text, label, color_code) ⇒ Object
Internal: emits a tagged, colored block via Rails.logger. ANSI colors are always included; modern terminals + the Rails dev logger render them. In production the logger strips them or they pass through harmlessly to aggregators.
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/svelte_on_rails/lib/utils.rb', line 39 def self.log_with_severity(level, text, label, color_code) clear = "\e[0m" bold = "\e[1m" header = "#{color_code}#{bold}[SOR] #{label}#{clear}" Rails.logger.public_send(level, header) text.to_s.split("\n").each do |line| Rails.logger.public_send(level, "#{color_code} => #{line}#{clear}") end end |
.manifest_file_mtime ⇒ Object
103 104 105 106 107 108 109 110 |
# File 'lib/svelte_on_rails/lib/utils.rb', line 103 def self.manifest_file_mtime cnf = SvelteOnRails::Configuration.instance if File.exist?(cnf.manifest_json_path) File.mtime(cnf.manifest_json_path).to_f.round(3) else 0.0 end end |
.puts_error(text) ⇒ Object
Defining methods to log errors and warnings using Rails logger
26 27 28 |
# File 'lib/svelte_on_rails/lib/utils.rb', line 26 def self.puts_error(text) log_with_severity(:error, text, "ERROR", "\e[31m") # red end |
.puts_warning(text) ⇒ Object
Defining method to log warnings using Rails logger
31 32 33 |
# File 'lib/svelte_on_rails/lib/utils.rb', line 31 def self.puts_warning(text) log_with_severity(:warn, text, "WARNING", "\e[33m") end |
.ssr_server_log(msg) ⇒ Object
174 175 176 177 178 179 180 181 |
# File 'lib/svelte_on_rails/lib/utils.rb', line 174 def self.ssr_server_log(msg) File.open(ssr_server_logfile, 'a') do |f| f.sync = true f.puts("[#{Time.now.utc.iso8601(3)} ppid:#{Process.ppid} pid:#{Process.pid}] #{msg}") end puts "[SOR] #{msg}" $stdout.flush end |
.ssr_server_logfile ⇒ Object
170 171 172 |
# File 'lib/svelte_on_rails/lib/utils.rb', line 170 def self.ssr_server_logfile Rails.root.join("log", "svelte-ssr-server.log") end |
.vite_source_code_dir ⇒ Object
151 152 153 154 155 156 157 |
# File 'lib/svelte_on_rails/lib/utils.rb', line 151 def self.vite_source_code_dir vite_config = Rails.root.join('config', 'vite.json') if vite_config.exist? vc = JSON.parse(File.read(vite_config)) vc['all']['sourceCodeDir'] end end |