class SvelteOnRails::Renderer

def render(props = {})

def render(props = {})
  require 'base64'
  require 'json'
  utils = SvelteOnRails::Lib::Utils
  cnf = SvelteOnRails::Configuration.instance
  cmd = [
    cnf.node_bin_path,
    File.join(utils.gem_app_dir, 'renderer', 'render.js'),
    @component_files[:compiled_file] + '.js',
    cnf.rails_root
  ].join(' ')
  Dir.chdir(cnf.rails_root) do
    stdout, stderr, status = Open3.capture3(cmd, stdin_data: props.to_json, chdir: cnf.rails_root)
    puts "render.js executed within #{cnf.rails_root}"
  end
  begin
    res = JSON.parse(stdout)
    css_file = @component_files[:compiled_file] + '.css'
    if File.exist?(css_file)
      res['css'] = File.read(css_file)
    end
    unless status.to_s.match(/^pid [0-9]+ exit 0$/)
      cmp = "#{@component_files[:svelte_filename]} was returned «#{status.to_s}»\n\n"
      msg = "#{cmp}output from render.js (stderr) =>\n+++\n" + stderr + "+++\n\nRender Svelte Server-side =>\n#{cmd}\n\n"
      utils.puts_warning(msg)
    end
    return res
  rescue JSON::ParserError => e
    raise "[svelte-on-rails] render ERROR Svelte Server-side: Expected JSON, got: «#{stdout}»\n\ncomponent: #{@component_files[:svelte_filename]}\n\nstdout:\n+++\n#{stderr}+++\n\ncmd: «#{cmd}»"
  end
end