Class: Esp::Watcher

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

Overview

File-watch-driven rebuild. Watches a mod’s source directory (and its scripts/, i18n/, etc. subtrees) and re-invokes Esp::Mw::Builder on any matching change. Blocks until SIGINT.

Constant Summary collapse

WATCHED_EXTS =
%w[.json .rb .py .js .mjs .ts .mwscript .yaml .yml].freeze

Instance Method Summary collapse

Constructor Details

#initialize(mod, locale: nil, root: Esp::ROOT, output: $stdout) ⇒ Watcher

Returns a new instance of Watcher.



10
11
12
13
14
15
# File 'lib/esp/watcher.rb', line 10

def initialize(mod, locale: nil, root: Esp::ROOT, output: $stdout)
  @mod = mod
  @locale = locale
  @root = root
  @output = output
end

Instance Method Details

#startObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/esp/watcher.rb', line 17

def start
  source_dir = File.dirname(Esp::Mw::Loader.resolve(@mod, root: @root))

  rebuild
  log "watching #{relative(source_dir)} (Ctrl-C to stop)"

  listener = Listen.to(source_dir, only: extensions_regex, latency: 0.2) { rebuild }
  listener.start
  trap('INT') do
    listener.stop
    log 'stopped.'
    exit 0
  end
  sleep
end