Jack Wallen shows you how to install and use the directory monitor tool, fswatch.
With each passing day, it seems we are more and more likely to suffer from data theft. To that end, we do everything in our powers to prevent such an event. We pay exorbitant prices for a secure network pipe, spend long hours constantly configuring and updating our servers, and so much more. And yet, our data is…
Anya is live and ready to show you everything. Watch her strip, dance, and perform exclusive shows just for you. Interact in real-time and make your fantasies come true.
✓ Live Streaming✓ Interactive Chat✓ Private Shows✓ HD Quality
Anya is LIVE right now
FREE
Free to watch • No registration required • HD streaming
I like having tests run as I work on them, or as I work on the Code Under Test. Here’s a quick 1-liner to get your mocha or jenkins-mocha based nodejs unit tests to run after you’ve made any changes to a given set of files.
Install fswatch (if you're on a mac, or inotifywait if you're on linux). brew install fswatch
Set the 1-liner to execute the test runner over the test you modified: fswatch -0 ./tests/bin | xargs -0 -n 1 -I {} ./node_modules/jenkins-mocha/bin/jenkins.js {}
Note: I'm using jenkins-mocha.
Now, when you modify any tests in ./tests/bin, fswatch will see those files change, pipe them into xargs, which will then run jenkins-mocha with only the file name that changed. This is useful if you want tests to execute every time you save a file, for instance.
Automatically build files when they change with Make
When writing code which needs to be built before it can be used, whether that's a transpile step like ES7 JavaScript to ES5, or a compile step like with Go, you're likely going to want to do this when a file in your application tree is modified. There are a large number of project and language specific tools which were developed to tackle this problem but did you know that there are system level packages available that you can use across all your projects?
Introducing inotifywait, which is an efficient and easy to use cli tool which uses Linux's inotify interface to watch for changes to the file system. And fswatch for those on OSX. Most of the language and project specific tools are built using wrappers around these two tools.
If you're like me and don't like to add more build tools and layers of abstraction than necessary then you're probably already using Make to build and develop your application, and you'll be happy to know that using these tools with it is trivial. Make has no way to know when a file has changed until the next time you run make so because of this many have tried something like the following which will run the build target every 2s.
watch -n 2 make build
Or they will build the loop into the makefile.
.PHONY watch watch: while true; do \ make build --silent; \ sleep 1; \ done
This works, but it's performing a lot of unneccesary work being run in a loop especially since the file system is able to tell us when a file or directory has been modified using inotify. Instead of automatically looping we wait for a file system event that we're interested in and then run our build target. In the following code we're able to create a make target in our makefile which will watch for file changes under our specified directory recursively.
.PHONY watch watch: while true; do \ inotifywait -qr -e modify -e create -e delete -e move app/src; \ make build; \ done
This works by creating an infinite loop which is started when you run the watch target. Before the first loop can finish it hits inotifywait which sets up listeners on all of the files and directories in your app/src directory. These listeners are waiting for any files to be modified, created, deleted, or moved in or out of app/src/.... When a file or directory changes inotifywait lets the loop continue triggering the call to your build target. That target executes in its entirety building only the file(s) which changed (assuming you've properly set up your makefile) and then the loop starts again with inotifywait waiting for those files to change again.
Using this technique will allow you to create an easy and efficient file change watcher for your makefile without too many additional tools. If you have any questions or comments leave them in the comments below or mention me on twitter @fromanegg. Thanks for reading!