ファイルの変更を監視して何らかの処理を行うと言ったソフトウェアは静的サイトの流行もあって幾つか出てきています。軽量なもの、プラグインで拡張できるものなど色々ですが、今回はFacebookにより開発されているWatchmanを紹介します。 特徴としてはファイルの拡張子も問わず、指定したファイル(パターン指定可)が変更されたらそれをトリガーに何らかの処理を行うと言ったソフトウェアになるようです。さっそく見ていきましょう。

最も簡単な使い方として、CSSを変更したらミニファイする場合は次のようになります。

$ watchman -- trigger ~/src buildme '*.css' -- minify-css

さらに複数のディレクトリ、ファイルを監視できます。

$ watchman watch ~/www

この場合、~/wwwを監視対象に加えます。さらに別なディレクトリも監視対象に加えられます。

$ watchman watch-list
{
    "version": "2.9.3",
    "roots": [
        "/Users/nakatsugawa/Downloads/watchman/src",
        "/Users/nakatsugawa/src"
    ]
}

で監視対象を一覧で取得できます。消す場合はwatch-delサブコマンドを使います。

$ watchman watch-del /path/to/dir

watchmanは設定をJSONで記述し、監視するフォルダやファイルそして実行する処理を定義できます。その中ではnot/allof/anyofなどのコマンドを使ってプログラマブルな定義も可能となっています。

オプションは次のようになります。

$ watchman --help
Usage: watchman [opts] command

 -h, --help                 Show this help

 -v, --version              Show version number

 -U, --sockname=PATH        Specify alternate sockname

 -o, --logfile=PATH         Specify path to logfile

     --log-level            set the log level (0 = off, default is 1, verbose = 2)

 -p, --persistent           Persist and wait for further responses

 -n, --no-save-state        Don't save state between invocations

     --statefile=PATH       Specify path to file to hold watch and trigger state

 -j, --json-command         Instead of parsing CLI arguments, take a single json object from stdin

     --output-encoding=ARG  CLI output encoding. json (default) or bser

     --server-encoding=ARG  CLI< ->server encoding. bser (default) or json

 -f, --foreground           Run the service in the foreground

     --no-pretty            Don't pretty print JSON

     --no-spawn             Don't try to start the service if it is not available

 -s, --settle               Number of milliseconds to wait for filesystem to settle (deprecated: use .watchmanconfig instead)

     --recrawl              Number of milliseconds between tree polling crawl

Watchman, by Wez Furlong.
Copyright 2012-2013 Facebook, Inc.

Watchmanは静的サイトの開発だけでなく、サーバ上でファイルの生成を監視して処理を実行したり、コンパイルを自動化したりと言った作業ができるようになりそうです。高機能なので学習に時間がかかりそうなのが難点かも知れません。 WatchmanはC製、MIT LicenseApache License 2.0のオープンソース・ソフトウェアです。

2014年01月30日:ライセンス修正

facebook/watchman