Class: MermaidCLI

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

Instance Method Summary collapse

Instance Method Details

#app_dirObject



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

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

#compile(src_path, dest_pdf_path) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/mermaid_cli.rb', line 52

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



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

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

#css_pathObject



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

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

#ensure_latest_mermaidObject



20
21
22
23
24
# File 'lib/mermaid_cli.rb', line 20

def ensure_latest_mermaid
  raise "Yarn not installed" unless yarn_installed?
  install_mermaid if !mermaid_installed?
  yield
end

#install_mermaidObject



30
31
32
33
34
35
36
37
# File 'lib/mermaid_cli.rb', line 30

def install_mermaid
  puts "mermaid not installed. Installing..."
  Dir.chdir app_dir do
    if system("yarn install")
      use_latest_mermaid_for_cli
    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



43
44
45
# File 'lib/mermaid_cli.rb', line 43

def new_mermaid_path
  File.expand_path("../node_modules/mermaid/dist/mermaid.min.js", __dir__)
end

#old_mermaid_pathObject



39
40
41
# File 'lib/mermaid_cli.rb', line 39

def old_mermaid_path
  File.expand_path("../node_modules/mermaid.cli/mermaid.min.js", __dir__)
end

#puppeteer_config_pathObject



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

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

#use_latest_mermaid_for_cliObject



47
48
49
50
# File 'lib/mermaid_cli.rb', line 47

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