由于没有必要将package.json
文件包含为deno,因此作为开发人员,我如何像在npm scripts
中使用package.json
一样有类似的经历?
我一直在研究一种模拟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
您可以将自己的文件创建为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";
并且您可以将所有依赖项添加到单个文件中并使用它,这样它就看起来像程序包管理器。
velociraptor可能会有所帮助,特别是如果您想运行任意的shell脚本。
它同时接受yaml
和json
脚本配置文件。以下示例说明了主要功能:
# 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
免责声明:我是这个项目的作者
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
)--root
根目录(否则为bin
)~/.deno
选项覆盖现有的别名旁注
-f
/ .js
脚本都是有效的参考-源代码位置(本地/ URL)无关紧要。如果要包含外部Shell脚本,则也可以运行它们.ts
。Deno已经提供了用于常见生态系统任务的内置解决方案,例如稍后deno
,bundle
,fmt
和test
(请参阅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();