【问题标题】:Deno: How to substitute npm scripts (package.json)Deno:如何替换 npm 脚本(package.json)
【发布时间】:2020-05-12 22:54:06
【问题描述】:

既然没有必要将package.json 文件与 deno 一起使用,作为开发人员,我如何才能拥有与 package.json 中的 npm scripts 类似的体验?

【问题讨论】:

    标签: scripting npm-scripts deno


    【解决方案1】:

    velociraptor 可能会有所帮助,尤其是在您想运行任意 shell 脚本时。

    它接受yamljsonts 脚本配置文件。以下示例说明了主要功能:

    # scripts.yaml
    scripts:
    
      start: deno run server.ts # Scripts can be simple command strings
    
      opts:                     # Or objects
        cmd: deno run server.ts
        desc: Starts the server
        tsconfig: tsconfig.json # Deno cli options
        imap: importmap.json
        allow:
          - read
          - net
        env:                    # Env vars
          PORT: 8080
    
      compact: server.ts        # `deno run` is automatically prepended
                                # when the script starts with a .ts file
    
      multiple:                 # Lists of commands are executed in series
        - echo one
        - echo two
    
      concurrent:               # Use the pll property to declare
        pll:                    # concurrent scripts
          - echo one
          - echo two
    
    env:                        # Top level options are sent to all the scripts
      PORT: 3000
    allow:
      - write
    

    不带参数运行vr 以查看可用脚本列表。执行脚本运行:

    $ vr <script name> [additional args]...
    # or
    $ vr run <script name> [additional args]...
    # Additional args are passed to the script
    

    vr start
    

    免责声明:我是作者

    【讨论】:

      【解决方案2】:

      deno install

      您可以使用deno install 创建可执行的别名脚本。

      它将为指定的主模块和 CLI 参数提供一个瘦壳/cmd 包装器。示例:

      deno install --root . -n serve --allow-read --allow-net https://deno.land/std@0.54.0/http/file_server.ts
      

      结果是一个serve脚本,类似于npm "scripts": { "serve": ... }

      ./bin/serve # run `serve` script (~ npm run serve)
      

      如果项目的bin 文件夹是added to PATH 环境,则命令缩短为serve

      deno install 做了什么

      • 创建bin 文件夹(如果不存在)
      • 添加serve/serve.cmd 文件,内容如下(此处为Windows):
        % generated by deno install %
        

      @deno.exe "运行" "--allow-read" "--allow-net" "https://deno.land/std@0.54.0/http/file_server.ts" %* ```

      • -n是后面使用的命令名(可以是left out
      • --root 指定bin 根位置(否则~/.deno
      • -f 选项覆盖现有别名

      旁注:任何.js/.ts 脚本都是有效的参考——源代码位置(本地/URL)无关紧要。如果要包含外部 shell 脚本,您也可以运行它们inside a subprocess

      示例:自定义npm run build

      // ./scripts/build.ts
      
      // create subprocess
      const p = Deno.run({
        cmd: ["deno", "cache", "-r", "--unstable", "main.ts"],
      });
      
      await p.status();
      
      deno install --root . --allow-run scripts\build.ts
      
      ./bin/build # execute build script
      

      内置deno 命令

      Deno 已经为常见的生态系统任务提供了内置解决方案,例如bundlefmttestlint 稍后(见 deno help)。您可以直接调用这些命令 - 无需定义自定义脚本:

      deno test
      deno fmt
      deno cache -r main.ts # similar to `npm run build` / `tsc`
      # ...
      

      【讨论】:

        【解决方案3】:

        我一直在研究一个模仿 package.json 脚本部分的解决方案,同时添加一些 Deno 特定功能。

        你需要先安装denox你可以在这里找到说明https://github.com/BentoumiTech/denox

        然后创建一个.deno-workspace 文件,在其中指定脚本列表:

        scripts:
          # "denox run start" will execute main.ts with example.com networking permissions
          start:
            file: main.ts
            permissions:
              allow-net: example.com
          # "denox run develop" will execute main.ts with localhost networking permissions
          develop:
            file: main.ts
            permissions:
              allow-net: localhost
        

        然后你可以运行

        1. $ denox run start
        2. $ denox run develop

        【讨论】:

        • 太棒了! --reload 怎么样?我应该在哪里指定它?
        • @Cea 作为临时解决方案,您可以将其添加到权限中,我目前正在努力添加对更多选项的支持
        • @Cea 如果您需要使用该工具的指导,我很乐意为您提供指导
        • 谢谢!目前正在从事另一个项目,所以我还不能尝试 denox。 --reload 刚刚闪过我的脑海,这就是我问它的原因。如果遇到任何障碍,我会通知您。
        • 这将如何与 deno 是一个项目的 pnpm 工作区(monorepo)结合?
        【解决方案4】:

        您可以将自己的文件创建为 denoDept.js

        export { assert } from "https://deno.land/std@v0.39.0/testing/asserts.ts";
        export { green, bold } from "https://deno.land/std@v0.39.0/fmt/colors.ts";
        

        您可以将所有依赖项添加到单个文件中并使用它,使其看起来像包管理器。

        【讨论】:

          猜你喜欢
          • 2018-09-04
          • 2021-09-29
          • 2014-06-26
          • 2021-10-01
          • 2020-01-22
          • 2023-02-04
          • 2016-03-05
          • 2021-11-12
          • 2021-10-03
          相关资源
          最近更新 更多