Class: Dugway::Cli::Server

Inherits:
Thor::Group
  • Object
show all
Defined in:
lib/dugway/cli/server.rb

Instance Method Summary collapse

Instance Method Details

#startObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/dugway/cli/server.rb', line 32

def start
  if options[:suppress_warnings]
    $VERBOSE = nil
  end

  listener = Listen.to('.', only: /\.(css|html|jpg|js|json|png|sass|svg)$/) do |modified|
    puts "Theme files changed, restarting server..."
    args = [RbConfig.ruby, '-S', 'dugway', 'server', '--host', options[:host], '--port', options[:port].to_s, '--server', options[:server]]
    args << '--suppress_warnings' if options[:suppress_warnings]
    exec(*args)
  end

  Thread.new { listener.start }

  begin
    Rack::Server.start({
      :config => File.join(Dir.pwd, 'config.ru'),
      :environment => 'none',
      :Host => options[:host],
      :Port => options[:port],
      :server => options[:server]
    })
  rescue Errno::EADDRINUSE
    handle_port_in_use_error(options[:port])
    exit 1
  rescue Errno::EACCES
    handle_permission_error(options[:port])
    exit 1
  rescue Errno::EADDRNOTAVAIL
    handle_address_error(options[:host], options[:port])
    exit 1
  rescue StandardError => e
    if e.message.include?("port is in use") || e.message.include?("no acceptor")
      handle_port_in_use_error(options[:port])
    elsif e.message.include?("requires root privileges")
      handle_permission_error(options[:port])
    else
      puts "\nError starting server: #{e.message}"
      puts "Try running with different options or check your theme configuration."
    end
    exit 1
  end
end