Module: MilkTea::CLI::CommandDocs

Included in:
MilkTea::CLI
Defined in:
lib/milk_tea/tooling/cli/commands/docs.rb

Instance Method Summary collapse

Instance Method Details

#docs_commandObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/milk_tea/tooling/cli/commands/docs.rb', line 6

def docs_command
  port = nil
  open_flag = false

  while (arg = @argv.first)
    case arg
    when "--port", "-p"
      @argv.shift
      port = @argv.shift.to_i
      port = nil if port <= 0 || port > 65535
    when "--open", "-o"
      open_flag = true
      @argv.shift
    else
      break
    end
  end

  port = resolve_docs_port(port)

  DocsApp.set :port, port
  DocsApp.set :bind, "127.0.0.1"
  DocsApp.set :environment, :production
  DocsApp.set :server, :puma

  url = "http://127.0.0.1:#{port}/"

  @out.puts("Serving Milk Tea docs at #{url}")
  @out.puts("Press Ctrl+C to stop.")

  if open_flag
    open_browser(url)
  end

  DocsApp.run!
  0
rescue Interrupt
  0
end

#resolve_docs_port(preferred) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/milk_tea/tooling/cli/commands/docs.rb', line 46

def resolve_docs_port(preferred)
  return preferred if preferred

  server = TCPServer.new("127.0.0.1", 0)
  port = server.addr[1]
  server.close
  port
rescue StandardError
  4567
end