Skip to content

filewatcher/filewatcher-cli

Repository files navigation

Filewatcher CLI

Cirrus CI - Base Branch Build Status Codecov branch Code Climate Depfu Inline docs License Gem

CLI for Filewatcher.

Installation

Add this line to your application's Gemfile:

gem 'filewatcher-cli'

And then execute:

bundle install

Or install it yourself as:

gem install filewatcher-cli

Usage

Run the echo command when the file myfile is changed:

$ filewatcher "myfile" "echo 'myfile has changed'"

Run any JavaScript in the current directory when it is updated in Windows PowerShell:

> filewatcher *.js "node %FILENAME%"

In Linux/macOS:

$ filewatcher *.js 'node $FILENAME'

Place filenames in quotes to use Ruby filename globbing instead of shell filename globbing. This will make Filewatcher look for files in subdirectories too. To watch all JavaScript files in subdirectories in Windows:

> filewatcher "**/*.js" "node %FILENAME%"

In Linux/macOS:

$ filewatcher '**/*.js' 'node $FILENAME'

By default, Filewatcher executes the command only for the first changed file that found from file system check, but you can using the --every/-E option for running the command on each changed file.

$ filewatcher -E * 'echo file: $FILENAME'

Try to run the updated file as a script when it is updated by using the --exec/-e option. Works with files with file extensions that looks like a Python, Ruby, Perl, PHP, JavaScript or AWK script.

$ filewatcher -e *.rb

Print a list of all files matching *.css first and then output the filename when a file is being updated by using the --list/-l option:

$ filewatcher -l '**/*.css' 'echo file: $FILENAME'

Watch the "src" and "test" folders recursively, and run test when the file system gets updated:

$ filewatcher "src test" "ruby test/test_suite.rb"

Restart long running commands

The --restart/-r option kills the command if it's still running when a file system change happens. Can be used to restart locally running web servers on updates, or kill long running tests and restart on updates. This option often makes Filewatcher faster in general. To not wait for tests to finish:

$ filewatcher --restart "**/*.rb" "rake test"

By default, it sends TERM signal, but you can change it to what you want via --restart-signal option:

$ filewatcher --restart --restart-signal=KILL "**/*.rb" "rake test"

Due to problems with signals on Windows, the default value of restart signal is KILL on Windows (while on Unix-like is TERM), which is a single one supported at the current moment, so use other values carefully.

The --immediate/-I option starts the command on startup without waiting for file system updates. To start a web server and have it automatically restart when HTML files are updated:

$ filewatcher --restart --immediate "**/*.html" "python -m SimpleHTTPServer"

Daemonizing Filewatcher process

The --daemon/-D option starts Filewatcher in the background as system daemon, so Filewatcher will not be terminated by Ctrl+C, for example.

Available environment variables

The environment variable $FILENAME is available in the shell command argument. On UNIX like systems the command has to be enclosed in single quotes. To run node whenever a JavaScript file is updated:

$ filewatcher *.js 'node $FILENAME'

Environment variables available from the command string:

BASENAME           File basename.
FILENAME           Relative filename.
ABSOLUTE_FILENAME  Absolute filename.
RELATIVE_FILENAME  Same as FILENAME but starts with "./"
EVENT              Event type. Is either 'updated', 'deleted' or 'created'.
DIRNAME            Absolute directory name.

Enable plugins

When you use Filewatcher via CLI, you can enable plugins for it via --plugins/-p option:

# Require `spinner` plugin and then enable a spinner via new option from this plugin
$ filewatcher -p spinner --spinner '**/*'

Development

After checking out the repo, run bundle install to install dependencies.

Then, run toys rspec to run the tests.

To install this gem onto your local machine, run toys gem install.

To release a new version, run toys gem release %version%. See how it works here.

Contributing

Bug reports and pull requests are welcome on GitHub.

License

The gem is available as open source under the terms of the MIT License.