Appearance
CLI 用法
¥CLI Usage
Zx 提供了用于运行脚本的 CLI。它随软件包一起安装,可用作 zx 可执行文件。
¥Zx provides a CLI for running scripts. It is installed with the package and can be used as zx executable.
sh
zx script.mjs无扩展名
¥No extensions
如果脚本没有文件扩展名(如 .git/hooks/pre-commit),zx 假定它是 ESM 模块。
¥If script does not have a file extension (like .git/hooks/pre-commit), zx assumes that it is an ESM module.
bash
zx docs/markdown.md远程脚本
¥Remote scripts
如果 zx 可执行文件的参数以 https:// 开头,则将下载并执行该文件。
¥If the argument to the zx executable starts with https://, the file will be downloaded and executed.
bash
zx https://medv.io/game-of-life.js来自标准输入的脚本
¥Scripts from stdin
zx 支持从 stdin 执行脚本。
¥The zx supports executing scripts from stdin.
js
zx << 'EOF'
await $`pwd`
EOF--eval
将以下参数作为脚本进行评估。
¥Evaluate the following argument as a script.
bash
cat package.json | zx --eval 'const v = JSON.parse(await stdin()).version; echo(v)'--install
js
// script.mjs:
import sh from 'tinysh'
sh.say('Hello, world!')将 --install 标志添加到 zx 命令以自动安装缺少的依赖。
¥Add --install flag to the zx command to install missing dependencies automatically.
bash
zx --install script.mjs你还可以在导入后添加注释 @ 来指定所需版本。
¥You can also specify needed version by adding comment with @ after the import.
js
import sh from 'tinysh' // @^1--quiet
抑制任何输出。
¥Suppress any outputs.
--verbose
启用详细模式。
¥Enable verbose mode.
--shell
指定自定义 shell 二进制文件。
¥Specify a custom shell binary.
bash
zx --shell=/bin/bash script.mjs--prefix & --postfix
将命令附加到每个命令的开头或结尾。
¥Attach a command to the beginning or the end of every command.
bash
zx --prefix='echo foo;' --postfix='; echo bar' script.mjs--cwd
设置当前工作目录。
¥Set the current working directory.
bash
zx --cwd=/foo/bar script.mjs--version
打印 zx 的当前版本。
¥Print the current version of zx.
--help
打印帮助。
¥Print help.
__filename 和 __dirname
¥__filename & __dirname
在 ESM 模块中,Node.js 不提供 __filename 和 __dirname 全局变量。由于此类全局变量在脚本中非常方便,因此 zx 提供了这些全局变量以在 .mjs 文件中使用(当使用 zx 可执行文件时)。
¥In ESM modules, Node.js does not provide __filename and __dirname globals. As such globals are really handy in scripts, zx provides these for use in .mjs files (when using the zx executable).
require()
在 ESM 模块中,未定义 require() 功能。zx 提供 require() 功能,因此它可以与 .mjs 文件中的导入一起使用(当使用 zx 可执行文件时)。
¥In ESM modules, the require() function is not defined. The zx provides require() function, so it can be used with imports in .mjs files (when using zx executable).
js
const {version} = require('./package.json')