class MermaidCLI

Public Instance Methods

app_dir() click to toggle source
# File lib/mermaid_cli.rb, line 27
def app_dir
  File.expand_path("../", __dir__)
end
compile(src_path, dest_pdf_path) click to toggle source
# File lib/mermaid_cli.rb, line 53
def compile(src_path, dest_pdf_path)
  parts = [
    "yarn", "run", "mmdc",
    "-i", src_path,
    "-C", css_path,
    "-c", config_path,
    "-p", puppeteer_config_path,
    "-o", dest_pdf_path
  ].map { |part| Shellwords.escape(part) }

  cmd = parts.join(" ")

  Dir.chdir app_dir do
    ensure_latest_mermaid { system cmd }
  end
end
config_path() click to toggle source
# File lib/mermaid_cli.rb, line 74
def config_path
  File.expand_path("../mermaid.json", __dir__)
end
css_path() click to toggle source
# File lib/mermaid_cli.rb, line 70
def css_path
  File.expand_path("../mermaid.css", __dir__)
end
ensure_latest_mermaid() { || ... } click to toggle source
# File lib/mermaid_cli.rb, line 20
def ensure_latest_mermaid
  raise "Yarn not installed" unless yarn_installed?
  install_mermaid
  use_latest_mermaid_for_cli
  yield
end
install_mermaid() click to toggle source
# File lib/mermaid_cli.rb, line 31
def install_mermaid
  puts "mermaid not installed. Installing..."
  unless mermaid_installed?
    Dir.chdir app_dir do
      system("yarn install")
    end
  end
end
mermaid_installed?() click to toggle source
# File lib/mermaid_cli.rb, line 16
def mermaid_installed?
  File.exist?(old_mermaid_path) && File.exist?(new_mermaid_path)
end
new_mermaid_path() click to toggle source
# File lib/mermaid_cli.rb, line 44
def new_mermaid_path
  File.expand_path("../node_modules/mermaid/dist/mermaid.min.js", __dir__)
end
old_mermaid_path() click to toggle source
# File lib/mermaid_cli.rb, line 40
def old_mermaid_path
  File.expand_path("../node_modules/mermaid.cli/mermaid.min.js", __dir__)
end
puppeteer_config_path() click to toggle source
# File lib/mermaid_cli.rb, line 78
def puppeteer_config_path
  File.expand_path("../puppeteer.json", __dir__)
end
use_latest_mermaid_for_cli() click to toggle source
# File lib/mermaid_cli.rb, line 48
def use_latest_mermaid_for_cli
  puts "Upgrading mermaid for cli"
  FileUtils.cp(new_mermaid_path, old_mermaid_path)
end
yarn_installed?() click to toggle source
# File lib/mermaid_cli.rb, line 6
def yarn_installed?
  yarn = `which yarn`
  puts "Yarn resolved to #{yarn}"
  if yarn.nil? || yarn == ""
    return false
  end

  true
end