LoginSignup
17
17

More than 5 years have passed since last update.

Gruntfile更新したらgruntコマンドをリスタート

Last updated at Posted at 2013-09-20

もっと賢いやり方がありそうですが

やりたいこと

  • Gruntfileを修正する度にgruntコマンド止めてgruntとやってるのが面倒になってきた
  • node-dev app.jsみたいなのをgruntコマンドでもやりたい

やりかた

  • node-devnodemongruntコマンドファイル(中身はjs)のパスを渡せばOK
  • which gruntの戻り値を渡せばOK

  • nodemon使う
  • ここで使ってるGruntfile.coffeeの抜粋
  grunt.loadNpmTasks "grunt-koko"
  grunt.loadNpmTasks "grunt-contrib-jade"
  grunt.loadNpmTasks "grunt-contrib-coffee"
  grunt.loadNpmTasks "grunt-contrib-watch"
  grunt.loadNpmTasks "grunt-contrib-uglify"
  grunt.loadNpmTasks "grunt-contrib-concat"
  grunt.loadNpmTasks "grunt-contrib-clean"
  grunt.loadNpmTasks "grunt-contrib-connect"
  grunt.loadNpmTasks "grunt-contrib-cssmin"
  # resigster
  grunt.registerTask "default", [ "connect","watch"]

nodemon

  • Gruntfileのあるディレクトリで以下のnodemonコマンド実行
nodemon "$(which grunt)"
  • "$(which grunt)"の部分は展開されて、次のように実行したのと同じになる
nodemon $HOME/nvm/v0.10.2/lib/node_modules/grunt-cli/bin/grunt`
  • 自分のはnvm使ってて、ver.が少し古いv0.10.2使ってるからこんな風なパスになる

一応動きこんな感じにterminalに出力される

$ nodemon /Users/akymrk/nvm/v0.10.2/lib/node_modules/grunt-cli/bin/grunt
20 Sep 18:47:27 - [nodemon] v0.7.10
20 Sep 18:47:27 - [nodemon] to restart at any time, enter `rs`
20 Sep 18:47:27 - [nodemon] watching: /Users/akymrk/sample
20 Sep 18:47:27 - [nodemon] starting `node /Users/akymrk/nvm/v0.10.2/lib/node_modules/grunt-cli/bin/grunt`
Running "connect:livereload" (connect) task
Started connect web server on localhost:9001.

Running "watch" task
Waiting...20 Sep 18:48:11 - [nodemon] restarting due to changes...
20 Sep 18:48:11 - [nodemon] /Users/akymrk/sample/Gruntfile.coffee


20 Sep 18:48:11 - [nodemon] starting `node /Users/akymrk/nvm/v0.10.2/lib/node_modules/grunt-cli/bin/grunt`
Running "connect:livereload" (connect) task
Started connect web server on localhost:9001.

Running "watch" task
Waiting...20 Sep 18:49:07 - [nodemon] restarting due to changes...
20 Sep 18:49:07 - [nodemon] /Users/akymrk/sample/Gruntfile.coffee


20 Sep 18:49:07 - [nodemon] starting `node /Users/akymrk/nvm/v0.10.2/lib/node_modules/grunt-cli/bin/grunt`
Running "connect:livereload" (connect) task
Started connect web server on localhost:9002.

Running "watch" task
Waiting...20 Sep 18:49:13 - [nodemon] restarting due to changes...
20 Sep 18:49:13 - [nodemon] /Users/akymrk/sample/Gruntfile.coffee
  • nodemonGruntfile.coffeeの修正を検知すると、gruntコマンドを停止・再開してくれる

nodemonが更新を監視してるファイル

  • カレントディレクトリの.jsファイルと.coffeeファイル
    • Gruntfile.coffee以外にも.js.coffeeがカレントディレクトリにあれば、そのファイルの更新も再起動のトリガーになる
    • これがデフォルト実行時の監視対象
    • 同じディレクトリに普通置かれてるpackage.jsonの更新は検知しない

package.jsonが更新されたときも'nodemon'使ってリスタートしたい

  • 使ってるプラグインのアップデートして npm--save-devしてpackage.jsonが更新されたときとか
  • Gruntfile.jsでのみ有効
  • Gruntfile.coffeeだとNG
  • nodemon-eオプションで監視するカレントディレクトリのファイル拡張子を渡す nodemon -e js,json "$(which grunt)"

nodemon使うとgruntのlivereloadが正常に動かない

  • .coffeeファイル修正したときは、動かず、.css.jade編集したときは動いた
  • .nodemonignoreファイルに、nodemonによる監視を無視するファイル、ディレクトリを書いたらOKだった
# .nodemonignoreの例
/css/*
/jade/*
/coffee/*
/js/*
.git/*

grunt devとかオプション渡したいとき

nodemon "$(which grunt)" devという風に足せば動いた

node-devでもやり方同じ

node-dev  "$(which grunt)" default
17
17
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
17
17