Class: StaticSiteBuilder::DevServer

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

Overview

Development server with file watching and live reload

Watches for file changes using the listen gem, rebuilds the site automatically, and provides live reload via WebSocket server.

Instance Method Summary collapse

Constructor Details

#initialize(root: Dir.pwd, port: nil, ws_port: nil) ⇒ DevServer

Returns a new instance of DevServer.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/static_site_builder/dev_server.rb', line 19

def initialize(root: Dir.pwd, port: nil, ws_port: nil)
  @root = Pathname.new(root)
  @port = port || ENV['PORT']&.to_i || DEFAULT_PORT
  @ws_port = ws_port || ENV['WS_PORT']&.to_i || DEFAULT_WS_PORT
  @dist_dir = @root.join('dist')
  @reload_file = @root.join('.reload')
  @listener = nil
  @ws_server = nil
  @http_server = nil
  @running = false
end

Instance Method Details

#start ⇒ Object

Start the development server

Builds the site initially, starts file watcher, WebSocket server, and HTTP server



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/static_site_builder/dev_server.rb', line 34

def start
  puts 'Building site...'
  build_site

  puts "\nStarting development server..."
  puts "  HTTP server: http://localhost:#{@port}"
  puts "  WebSocket server: ws://localhost:#{@ws_port}"
  puts "  Watching for changes... (Ctrl+C to stop)\n"

  start_websocket_server
  start_file_watcher
  start_http_server
end

#stop ⇒ Object

Stop the development server



49
50
51
52
53
54
55
# File 'lib/static_site_builder/dev_server.rb', line 49

def stop
  @running = false
  @listener&.stop
  @ws_server&.stop
  @http_server&.shutdown if @http_server
  puts "\nShutting down..."
end