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

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

由于不一定要有 package.json 的文件,作为开发者,我如何才能像我们在使用 npm scriptspackage.json?

scripting npm-scripts deno
1个回答
2
投票

deno install

你可以用以下方法创建可执行的别名脚本 deno install.

它将为指定的主模块和CLI参数提供一个精简的shellcmd包装。例子:

deno install --root . -n serve --allow-read --allow-net https://deno.land/[email protected]/http/file_server.ts

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

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

如果项目的 bin 文件夹是 添加到PATH 环境下,命令缩短为 serve.

ぐ或 deno install 是否

  • 创造 bin 文件夹,如果不存在
  • 补充 serveserve.cmd 文件,内容如下(这里是Windows)。
    % generated by deno install %
    @deno.exe "run" "--allow-read" "--allow-net" "https://deno.land/[email protected]/http/file_server.ts" %*
    
  • -n 是以后使用的命令名(可以是 漏掉)
  • --root 指定了 bin 根位 ~/.deno)
  • -f 选项会覆盖现有的别名

附注: 任何 .js.ts 脚本是一个有效的引用--源代码位置(localURL)并不重要。如果要包含外部shell脚本,你也可以运行它们 在子进程中.

内置 deno 命令

Deno已经为常见的生态系统任务提供了内置的解决方案,例如。bundle, fmt, testlint 以后 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();
deno install --root . --allow-run scripts\build.ts

./bin/build # execute build script

1
投票

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

您需要首先安装 denox 你可以在这里找到说明 https:/github.comBentoumiTechdenox。

然后创建一个 .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

1
投票

迅猛龙 可能会有帮助,特别是当你想运行任意的shell脚本时。

它接受 yaml, jsonts 脚本配置文件。下面的例子说明了主要功能。

# 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
投票

你可以创建你自己的文件作为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";

而且你可以在一个文件中添加所有的依赖关系,并使用它,所以它看起来像包管理器。

© www.soinside.com 2019 - 2024. All rights reserved.