LoginSignup
1
1

watchexec cliでファイルの変更に伴ってコマンドを実行する

Posted at

インストール(apt)

aptでパッケージ管理をするlinuxを想定
適宜sudoを使ってください
その他のインストール方法については以下を参照

curl -fsSL https://apt.cli.rs/pubkey.asc | tee -a /usr/share/keyrings/rust-tools.asc
curl -fsSL https://apt.cli.rs/rust-tools.list | tee /etc/apt/sources.list.d/rust-tools.list
apt update
apt install watchexec-cli

基本的な使い方

  1. js, css, htmlファイルが変更された際にmakeコマンドを実行する
watchexec --exts js,css,html make
  1. カレントディレクトリとそのサブディレクトリに変更があった際ls -laコマンドを実行する
watchexec -- ls -la

想定されるユースケース

docker-compose.yamlに変更があった際にcompose upする

watchexec -w docker-compose.yaml docker compose up

requirements.txtに変更があった際、自動でインストールする

watchexec -w requirements.txt pip install -r requirements.txt

ニッチな問題

WSL2, devcontainerで開発していると、ファイルシステムが影響してファイルの変更が検知されないのでポーリングを設定しておきましょう。

alias watchexec="watchexec --poll 100"
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1