既然不需要package.json使用 deno 文件,作为开发人员,我如何才能拥有与npm scriptsin类似的体验package.json?
4 回答
我一直在研究一种模拟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
然后你可以运行
$ denox run start$ denox run develop
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文件夹添加到 PATH环境中,则该命令将缩短为serve.
deno install做什么
- 创建
bin文件夹,如果不存在 - 添加具有以下内容的
serve/文件(此处为 Windows):serve.cmd% generated by deno install %
@deno.exe "运行" "--allow-read" "--allow-net" "https://deno.land/std@0.54.0/http/file_server.ts" %* ```
-n是稍后使用的命令名称(可以省略)--root指定bin根位置(否则~/.deno)-f选项覆盖现有别名
旁注:任何.js/.ts脚本都是有效的参考 - 源代码位置(本地/URL)无关紧要。如果要包含外部 shell 脚本,您也可以在 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 已经为常见的生态系统任务提供了内置的解决方案,例如、bundle和稍后(参见 参考资料)。您可以直接调用这些命令 - 无需定义自定义脚本:fmttestlintdeno help
deno test
deno fmt
deno cache -r main.ts # similar to `npm run build` / `tsc`
# ...
速激肽可能会有所帮助,尤其是在您想运行任意 shell 脚本时。
它接受yaml,json和ts脚本配置文件。以下示例说明了主要功能:
# 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
免责声明:我是作者
您可以创建自己的文件作为 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";
您可以将所有依赖项添加到单个文件中并使用它,使其看起来像包管理器。