3

File watching with chokidar

 3 years ago
source link: https://sebastiandedeyne.com/file-watching-with-chokidar/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
File watching with chokidar — Sebastian De Deyne

File watchers are powerful tools. We take them for granted because they’re often baked in, like Webpack rebuilding when you save a file.

Sometimes I’m in an environment without auto-reloading to my disposal. Yesterday, I was working on a CLI tool. The workflow was modify code, save changes, switch to terminal, run the tool, and look at the output. It’s not that bad, but I’d prefer a shorter feedback loop.

This is where a file watcher gives you superpowers. I went with chokidar because it has the friendliest API.

First, install chokidar-cli:

npm i chokidar-cli -g

Then you can watch the files you’re working on, and run a command when they change.

chokidar "src/**/*.php" -c "php ./build.php"

When a PHP file changes in src, the build command will run. If I keep iTerm (and Ray) open alongside my editor, I’ll have immediate feedback for my changes. This is also a great way to automatically run tests for immediate feedback.

chokidar \
  "src/**/*.php" "tests/**/*.php" \
  -c "phpunit --filter test_i_am_working_on"

I like having a file watcher handy, it’s one of those Swiss Army knife developer tools that came come in useful in many scenarios.

Keep in touch

I occasionally send out a newsletter with personal stories, things I’ve been working on in the past month, and interesting things I come across.

Pinky swear that I won't use your data for any other purposes. Your email address will be stored on Mailchimp and nowhere else.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK