Search code examples
rubycoffeescriptguard

How to make Guard watch files in a subdirectory


I'm trying to have Guard compile my CoffeeScript files to JS files, and then have Juicer merge and minify them. I use a tmp directory to store the intermediate JS files. From what I understand this should work, but it doesn't:

guard :coffeescript, :input => "src/coffee", :output => "tmp"

guard :shell do
    watch %r{^tmp/.+\.js$} do
        system 'juicer', 'merge', 'tmp/app.js', '-sfo', 'js/app.js'
    end
end

CoffeeScript files are correctly compiled into the tmp directory every time they are touched, but then the shell guard does not fire.

Launching guard with --debug and changing one of the JS files in tmp by hand, I don't get any debug line in the terminal. It seems like those files are not being monitored.

$ guard --debug
18:53:51 - DEBUG - Command execution: which notify-send
18:53:51 - DEBUG - Command execution: emacsclient --eval '1' 2> /dev/null || echo 'N/A'
18:53:51 - INFO - Guard is using TerminalTitle to send notifications.
18:53:51 - DEBUG - Command execution: hash stty
18:53:51 - DEBUG - Guard starts all plugins
18:53:51 - DEBUG - Hook :start_begin executed for Guard::CoffeeScript
18:53:51 - DEBUG - Hook :start_end executed for Guard::CoffeeScript
18:53:51 - DEBUG - Hook :start_begin executed for Guard::Shell
18:53:51 - DEBUG - Hook :start_end executed for Guard::Shell
18:53:51 - INFO - Guard is now watching at '/home/tobia/my_project'
18:53:51 - DEBUG - Start interactor
[1] guard(main)> 

^^^ if I modify the JS files in /home/tobia/my_project/tmp at this point, nothing happens.

I'm using Ruby 1.9.1 from Debian stable, Guard 1.8.2 and Guard-shell 0.5.1 installed with sudo gem install


Solution

  • After some investigation I realized that tmp is in the default ignore list, so this is why Guard doesn't pick up changes from the generated JavaScript files. To work around this, you can either...

    1. Remove tmp from the ignored directories list

    ignore! /.git/
    
    guard :coffeescript, :input => "src/coffee", :output => "tmp"
    
    guard :shell do
      watch %r{^tmp/.+\.js$} do
        system 'juicer', 'merge', 'tmp/app.js', '-sfo', 'js/app.js'
      end
    end
    

    2. Compile the JavaScript to a different directory

    guard :coffeescript, :input => "src/coffee", :output => "src/js"
    
    guard :shell do
      watch %r{^src/.+\.js$} do
        system 'juicer', 'merge', 'tmp/app.js', '-sfo', 'js/app.js'
      end
    end