Search code examples
watchman

How to watch content from a subfolder of a parent folder using watchman?


I am developing an image uploader by watching files. The images are enclosed in a folder (subfolder) and that folder is being copied in a parent folder that is being watched.

Example:

~/parent/subfolder1/image1.png
~/parent/subfolder2/image2.png

How do I watch these images using the parent folder argument being passed to watchman? or How do I include subfolders?

This is what I have now:

export PARENT_FOLDER = ~/parent/
export UPLOADER_SCRIPT = ~/upload_image.sh
watchman -- trigger $PARENT_FOLDER upload_image -- $UPLOADER_SCRIPT

Inside the upload_image.sh is just an upload function with the image file path as argument

python upload.py image1.png

Solution

  • Use the extended syntax: http://facebook.github.io/watchman/docs/trigger#extended-syntax so that you can define richer matching expressions. For example, this will match any .png file using a glob expression, but you can use any of the expression terms to scope this exactly how you need it:

    $ watchman -j <<-EOT
    ["trigger", "/path/to/parent", {
      "name": "upload_image",
      "expression": ["match", "**/*.png"],
      "command": ["/path/to/upload_image.sh"]
    }]
    EOT
    

    You may also want to take a look at anyof as a way to combine multiple criteria.