Deno:如何替换npm脚本(package.json)

问题描述 投票:4回答:4

由于没有必要将package.json文件包含为deno,因此作为开发人员,我如何像在npm scripts中使用package.json一样有类似的经历?

scripting npm-scripts deno
4个回答
0
投票

我一直在研究一种模拟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

0
投票

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

export { assert } from "https://deno.land/[email protected]/testing/asserts.ts";
export { green, bold } from "https://deno.land/[email protected]/fmt/colors.ts";

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


0
投票

velociraptor可能会有所帮助,特别是如果您想运行任意的shell脚本。

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

# 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

ie

vr start

免责声明:我是这个项目的作者


0
投票

deno install

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

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

deno install

结果是一个deno install --root . -n serve --allow-read --allow-net https://deno.land/[email protected]/http/file_server.ts 脚本,类似于npm serve

"scripts": { "serve": ... }

如果项目的./bin/serve # run `serve` script (~ npm run serve) 文件夹是bin环境,则命令最多可缩短为added to PATH

serve的作用

  • 创建deno install文件夹(如果不存在)>>
  • 添加具有以下内容的bin / serve文件(此处为Windows):
    serve.cmd
  • [% generated by deno install % @deno.exe "run" "--allow-read" "--allow-net" "https://deno.land/[email protected]/http/file_server.ts" %* 是以后使用的命令名称(可以是-n
  • left out指定--root根目录(否则为bin
  • [~/.deno选项覆盖现有的别名
  • 旁注

:任何-f / .js脚本都是有效的参考-源代码位置(本地/ URL)无关紧要。如果要包含外部Shell脚本,则也可以运行它们.ts

内置inside a subprocess命令

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

deno help

示例:自定义deno test deno fmt deno cache -r main.ts # similar to `npm run build` / `tsc` # ...

npm run build
// ./scripts/build.ts

// create subprocess
const p = Deno.run({
  cmd: ["deno", "cache", "-r", "--unstable", "main.ts"],
});

await p.status();
© www.soinside.com 2019 - 2024. All rights reserved.