【Deno】denon deno热重载框架

官网

https://deno.land/x/[email protected]

简介

denon是 deno 中 nodemon 的替代品。denon会监视文件的改变,并自动重新启动程序,重新编译更改的源代码。

安装

⚠️ Make sure you are using deno version ^1.6.0 to install this executable. You can upgrade running deno upgrade.

deno版本需要在^1.6以上

deno.land源

deno install -qAf --unstable https://deno.land/x/denon/denon.ts

nest.land源

deno install -qAf --unstable https://x.nest.land/denon/denon.ts

环境变量

mac环境,在终端输入open -e ~/.bash_profile添加如下内容

# denon
export PATH="/Users/zhouwei/.deno/bin:$PATH"

检查命令是否可用, 能打印信息即成功

$ denon --version

使用

直接使用

$ denon run app.ts

$ denon run --allow-env app.ts

配置文件

  1. 只是简单使用可以生成json配置文件
  2. 如果较复杂及灵活的配置建议使用.ts配置文件,可以添加环境变量及代码块操作

生成配置文件

// 生成json配置文件
denon --init

// 生成.ts配置文件
denon --init typescript

配置文件内容如下:

 // scripts.json
{
  "$schema": "https://deno.land/x/[email protected]/schema.json",
  "scripts": {
    "start": {
      "cmd": "deno run day1/hello.ts",
      "desc": "run my app.ts file"
    },
    "fileServer": {
      "cmd": "deno run day1/fileServer.ts",
      "desc": "run my fileServer.ts file"
    }
  }
}
// scripts.config.ts
import { DenonConfig } from "https://deno.land/x/[email protected]/mod.ts";

const config: DenonConfig = {
  scripts: {
    start: {
      cmd: "deno run day1/hello.ts",
      desc: "run my app.ts file",
    },
    fileServer: {
      cmd: "deno run day1/fileServer.ts",
      desc: "run my fileServer.ts file",
    },
  },
};

export default config;

执行脚本

$ denon start
$ denon fileServer

环境配置

// scripts.json
{
  "scripts": {
    "start": {
      "cmd": "deno run app.ts",
      "desc": "Run the main server.",
            "env": {
        "PORT": 3000
      }
    }
  }
}
// scripts.config.ts
import { DenonConfig } from "https://deno.land/x/[email protected]/mod.ts";
import { config as env } from "https://deno.land/x/dotenv/mod.ts";

const config: DenonConfig = {
  scripts: {
    start: {
      cmd: "deno run day1/hello.ts",
      desc: "run my app.ts file",
    },
    fileServer: {
      cmd: "deno run day1/fileServer.ts",
      desc: "run my fileServer.ts file",
      env: env()
    },
  },
};

export default config;

权限配置

{
  "allow": {
    "read": "/etc,/tmp", // --allow-read=/etc,/tmp
    "env": true // --allow-env
  },
  "allow": [
    "run", // --allow-run
    "net" // --allow-net
  ],
  "scripts": {
    "start": {
      "cmd": "deno run app.ts",
      "desc": "Run the main server.",

      // available options...
      "allow": ["env", "write"],
      "unstable": true
      // running `denon start` will resolve in
      // deno run --allow-env --allow-write --unstable app.ts
      "watch": true
    }
  }
}
import { DenonConfig } from "https://deno.land/x/[email protected]/mod.ts";
import { config as env } from "https://deno.land/x/[email protected]/mod.ts";

const config: DenonConfig = {
  scripts: {
    start: {
      cmd: "deno run day1/hello.ts",
      desc: "run my app.ts file",
    },
    fileServer: {
      cmd: "deno run day1/fileServer.ts",
      desc: "run my fileServer.ts file",
      env: env(),
      allow: ["net", "read"],
    },
  },
};

export default config;

你可能感兴趣的:(Deno学习笔记,deno,denon)