Class: MermaidCLI

Inherits:
Object
  • Object
show all
Defined in:
lib/mermaid_cli.rb

Instance Method Summary collapse

Instance Method Details

#app_dirObject



27
28
29
# File 'lib/mermaid_cli.rb', line 27

def app_dir
  File.expand_path("../", __dir__)
end

#compile(src_path, dest_pdf_path) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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_pathObject



74
75
76
# File 'lib/mermaid_cli.rb', line 74

def config_path
  File.expand_path("../mermaid.json", __dir__)
end

#css_pathObject



70
71
72
# File 'lib/mermaid_cli.rb', line 70

def css_path
  File.expand_path("../mermaid.css", __dir__)
end

#ensure_latest_mermaidObject



20
21
22
23
24
25
# 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_mermaidObject



31
32
33
34
35
36
37
38
# 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?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/mermaid_cli.rb', line 16

def mermaid_installed?
  File.exist?(old_mermaid_path) && File.exist?(new_mermaid_path)
end

#new_mermaid_pathObject



44
45
46
# 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_pathObject



40
41
42
# 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_pathObject



78
79
80
# File 'lib/mermaid_cli.rb', line 78

def puppeteer_config_path
  File.expand_path("../puppeteer.json", __dir__)
end

#use_latest_mermaid_for_cliObject



48
49
50
51
# 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?Boolean

Returns:

  • (Boolean)


6
7
8
9
10
11
12
13
14
# 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