masalibの日記

システム開発、運用と猫の写真ブログです

Denoにdenonをいれてみた

Denoのソースを修正するたびに、プログラムを止めて起動するという事をおこなっていた。
Node.jsではnodemonというアプリケーションがあってソースを修正すると自動的に再起動してくれるツールがあった。 Denoにも自動的に再起動するツールがあるのでいれてみた。 結論から言うとDenoを勉強する人はこれを絶対にいれて!!

インストール

簡単で下記のコマンドを実行すればOK

deno install --allow-read --allow-run --allow-write -f --unstable https://deno.land/x/denon/denon.ts

初期設定

設定ファイルの作成

denon --init

カレントディレクトリにdenon.jsonファイルができます。構成ファイルのjsonyamlの両方をサポートしている。

{
  "scripts": {
    "start": "app.js"
  }
}

自分の設定

{
  "$schema": "https://deno.land/x/denon/schema.json",
  "env": {
    "TOKEN":"xxxxdummyxxxx"
  },
  "scripts": {
    "start": {
      "cmd": "deno run server.ts",
      "allow": [
        "env",
        "read",
        "write",
        "net",
        "plugin"
      ],
      "unstable": true,
      "env": {
        "MYSQL_HOST_ENV":"127.0.0.1",
        "MYSQL_USERNAME_ENV":"wordpress",
        "MYSQL_DB_ENV":"wordpress",
        "MYSQL_PASSWORD_ENV":"wordpress",
        "MYSQL_PORT_ENV":"3306",
        "PORT":"5000"
      }
    }
  },
  "logger": {
    "debug": true
  },
  "watcher": {
    "interval": 350,
    "exts": ["js", "ts", "json"],
    "match": ["*.*"],
    "skip": ["*/.git/*" ,"README.md" ],
    "legacy": false
  }
}

無視ファイルもあるのでうれしい。

実行

enon start
[denon] v2.0.2
[denon] watching path(s): *.*
[denon] watching extensions: js,ts,json
[denon] starting `deno run --allow-env --allow-read --allow-write --allow-net --allow-plugin server.ts`
 S: starting process with pid 9384
 M: monitoring status of process with pid 9384
Server Running on port 5000

修正時のログ

 path server.ts is matched
 path server.ts is matched
 R: reload event detected, starting the reload procedure...
[denon] watching path(s): *.*
[denon] watching extensions: js,ts,json
[denon] restarting due to changes...
 K: killing 1 process[es]
 K: closing (windows) process with pid 9384
[denon] starting `deno run --allow-env --allow-read --allow-write --allow-net --allow-plugin server.ts`
 S: starting process with pid 1868
 M: monitoring status of process with pid 1868
 M: error getting status of process with pid 9384
 M: process with pid 9384 was killed
Compile file:///D:/git/deno_study/REST_API_mongodb/server.ts
Server Running on port 5000

注意点

  • denon.jsonに記載されている環境変数だけは更新しても反映されません。 反映するにはdenonを止める必要があります
  • allowやenv以外も設定できる。unstableという設定があるがそちらはtrue or flaseで設定できた

感想

なんで最初からいれなかったのかと後悔するぐらい便利!!