Class: Herb::Dev::Runner

Inherits:
Object
  • Object
show all
Includes:
Colors
Defined in:
lib/herb/dev/runner.rb

Constant Summary collapse

PATCHABLE_TYPES =

: Array

["text_changed", "attribute_value_changed", "attribute_added", "attribute_removed"].freeze
CLEAR_SCREEN =
"\e[2J\e[H"
HIDE_CURSOR =
"\e[?25l"
SHOW_CURSOR =
"\e[?25h"

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Colors

bold, bright_magenta, cyan, dimmed, enabled?, fg, fg_bg, green, magenta, red, white, yellow

Constructor Details

#initialize(path: ".", cli: nil) ⇒ Runner

Returns a new instance of Runner.



27
28
29
30
# File 'lib/herb/dev/runner.rb', line 27

def initialize(path: ".", cli: nil)
  @path = path
  @cli = cli
end

Class Method Details

.can_patch?(operations) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
17
18
19
20
21
# File 'lib/herb/dev/runner.rb', line 13

def self.can_patch?(operations)
  operations.all? { |operation|
    next false unless PATCHABLE_TYPES.include?(operation.type.to_s)
    next false if operation.new_node&.type&.to_s&.include?("ERB")
    next false if operation.old_node&.type&.to_s&.include?("ERB")

    true
  }
end

Instance Method Details

#restartObject



91
92
93
94
95
96
97
# File 'lib/herb/dev/runner.rb', line 91

def restart
  require_relative "server"

  Herb::Dev::ServerEntry.all.each(&:stop!)
  sleep 0.5
  run
end

#runObject



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
# File 'lib/herb/dev/runner.rb', line 32

def run
  require_cruise
  require_relative "server"

  unless File.directory?(@path)
    puts "Not a directory: '#{@path}'."
    exit(1)
  end

  config = Herb::Configuration.load(@path)
  expanded_path = File.realpath(File.expand_path(config.project_root || @path))

  check_existing_server(expanded_path)
  port = find_port

  print CLEAR_SCREEN
  print HIDE_CURSOR
  print_header(config, expanded_path)

  file_states = index_files(config, @path)

  websocket = Herb::Dev::Server.new(port: port, project_path: expanded_path)
  websocket.start

  puts "  #{fg("WebSocket:".ljust(11), 245)}#{fg("ws://localhost:#{websocket.port}", 250)}"
  puts
  puts "  #{fg("Ready!", 42)} #{fg("Watching for changes...", 241)}"
  puts

  watch_files(config, expanded_path, websocket, file_states)
rescue Interrupt
  websocket&.stop
  print SHOW_CURSOR
  puts
  puts "Stopped."
  exit(0)
ensure
  websocket&.stop
  print SHOW_CURSOR
end

#statusObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/herb/dev/runner.rb', line 99

def status
  require_relative "server"

  entries = Herb::Dev::ServerEntry.all

  if entries.empty?
    puts "No herb dev servers running."
  else
    entries.each do |entry|
      puts "#{entry.project_name} — PID: #{entry.pid}, port: #{entry.port}, started: #{entry.started_at}"
    end
  end

  exit(0)
end

#stopObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/herb/dev/runner.rb', line 73

def stop
  require_relative "server"

  entries = Herb::Dev::ServerEntry.all

  if entries.empty?
    puts "No herb dev servers running."
    exit(0)
  end

  entries.each do |entry|
    entry.stop!
    puts "Stopped herb dev server for #{entry.project_name} (PID: #{entry.pid}, port: #{entry.port})"
  end

  exit(0)
end