Class: Js2coffee::Watcher

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/js2coffee/watcher.rb

Instance Method Summary collapse

Constructor Details

#initializeWatcher

Returns a new instance of Watcher.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/js2coffee/watcher.rb', line 11

def initialize
  @notifier = INotify::Notifier.new

  Signal.trap(2) {|_sig| Process.exit }

  start_watch_files

  @notifier.watch('.', :moved_from, :moved_to, :create, :delete, :onlydir, :recursive) do |event|
    # skip temp file.
    next if event.name =~ /^\.#|^#.*\#$/

    start_watch_files
  end

  watched_files.each do |file|
    Js2coffee.watch_file(file)
  end

  # start loop.
  @notifier.run
  puts 'Watcher is started.'
end

Instance Method Details

#start_watch_filesObject

watch all exist files modify event.



35
36
37
38
39
40
41
# File 'lib/js2coffee/watcher.rb', line 35

def start_watch_files
  watched_files.each do |file|
    @notifier.watch(file, :modify) do
      Js2coffee.watch_file(file)
    end
  end
end

#watched_filesObject



43
44
45
46
47
48
49
# File 'lib/js2coffee/watcher.rb', line 43

def watched_files
  watched_files = Dir["./**/*.#{$script_extname}"]
  exclude_pattern = ['node_modules|bower_components'] << ENV['JS2COFFEE_EXCLUDE_PATTERN']

  watched_files.reject! {|e| e =~ /#{exclude_pattern.join('|')}/ }
  watched_files
end