【发布时间】:2020-05-12 22:54:06
【问题描述】:
既然没有必要将package.json 文件与 deno 一起使用,作为开发人员,我如何才能拥有与 package.json 中的 npm scripts 类似的体验?
【问题讨论】:
标签: scripting npm-scripts deno
既然没有必要将package.json 文件与 deno 一起使用,作为开发人员,我如何才能拥有与 package.json 中的 npm scripts 类似的体验?
【问题讨论】:
标签: scripting npm-scripts deno
velociraptor 可能会有所帮助,尤其是在您想运行任意 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
即
vr start
免责声明:我是作者
【讨论】:
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 已经为常见的生态系统任务提供了内置解决方案,例如bundle、fmt、test 和 lint 稍后(见 deno help)。您可以直接调用这些命令 - 无需定义自定义脚本:
deno test
deno fmt
deno cache -r main.ts # similar to `npm run build` / `tsc`
# ...
【讨论】:
我一直在研究一个模仿 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【讨论】:
--reload 怎么样?我应该在哪里指定它?
--reload 刚刚闪过我的脑海,这就是我问它的原因。如果遇到任何障碍,我会通知您。
您可以将自己的文件创建为 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";
您可以将所有依赖项添加到单个文件中并使用它,使其看起来像包管理器。
【讨论】: