Class: GemActivityTracker::Watcher

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

Class Method Summary collapse

Class Method Details

.start(path) ⇒ Object



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

def self.start(path)
  puts "👀 Watching for changes in: #{path}"

  listener = Listen.to(path, ignore: [%r{activity_tracker/}, %r{tmp/}, %r{log/}, /\.log$/]) do |modified, added, removed|
    changes = { modified: modified, added: added, removed: removed }
    log_file = File.join(path, "activity_tracker", "log.txt")
    FileUtils.mkdir_p(File.dirname(log_file))

    any_change = false
    changes.each do |type, files|
      files.each do |file|
        timestamp = Time.now.strftime("%Y-%m-%d %H:%M:%S")
        relative_path = file.sub("#{path}/", '')
        log_line = "[#{timestamp}] #{type.to_s.capitalize}: #{relative_path}"
        puts log_line
        File.open(log_file, "a") { |f| f.puts log_line }
        any_change = true
      end
    end

    GemActivityTracker::Tracker.track(path) if any_change
  end

  listener.start
  puts "✅ Watcher started. Press Ctrl+C to stop."
  sleep
end