字节笔记本
2026年2月19日
zx:Google 开源的 JavaScript 脚本编写工具
本文介绍 zx,Google 开源的脚本编写工具,使用 JavaScript/TypeScript 编写 Shell 脚本,提供更便捷的脚本开发体验。
zx 简介
zx 是由 Google 开发的开源工具,用于编写更好的脚本。它允许开发者使用 JavaScript 或 TypeScript 替代 Bash 编写脚本,结合了 Node.js 的便利性和 Shell 的强大功能。该项目在 GitHub 上拥有 45.2k stars 和 1.2k forks,采用 Apache-2.0 许可证。
核心定位:A tool for writing better scripts
核心优势
"Bash is great, but when it comes to writing more complex scripts, many people prefer a more convenient programming language. JavaScript is a perfect choice, but the Node.js standard library requires additional hassle before using. No compromise, take the best of both."
- 跨平台:支持 Linux、macOS、Windows
- 参数自动转义:防止命令注入
- 合理的默认配置:开箱即用
- 现代 JavaScript:使用 async/await、Promise 等特性
基本信息
- GitHub 仓库:https://github.com/google/zx
- 官方文档:https://google.github.io/zx/
- 星标数:45.2k stars
- Fork 数:1.2k forks
- 许可证:Apache-2.0
- 最新版本:8.8.5
技术栈
| 类别 | 技术 |
|---|---|
| 主要语言 | JavaScript (63.4%), TypeScript (36.5%) |
| 支持运行时 | Node.js ≥12.17.0, Bun ≥1.0.0, Deno 1.x/2.x |
| 模块系统 | CJS 和 ESM 双支持 |
| 平台 | Linux, macOS, Windows |
| Shell 要求 | bash 或 PowerShell |
安装方法
全局安装
npm install -g zx项目安装
npm install zx使用示例
基础脚本
创建 script.mjs 文件:
#!/usr/bin/env zx
// 执行命令
await $`cat package.json | grep name`
// 获取命令输出
const branch = await $`git branch --show-current`
await $`dep deploy --branch=${branch}`
// 并行执行
await Promise.all([
$`sleep 1; echo 1`,
$`sleep 2; echo 2`,
$`sleep 3; echo 3`,
])
// 变量自动转义
const name = 'foo bar'
await $`mkdir /tmp/${name}`运行脚本
zx script.mjs或添加执行权限:
chmod +x script.mjs
./script.mjs核心功能
$ 函数
$ 是 zx 的核心函数,用于执行 shell 命令:
// 简单命令
await $`echo Hello`
// 使用变量(自动转义)
const file = 'my file.txt'
await $`cat ${file}`
// 获取输出
const result = await $`echo Hello`
console.log(result.stdout)内置工具
zx 提供了多个实用工具:
| 工具 | 说明 |
|---|---|
cd() | 切换目录 |
fetch() | HTTP 请求 |
question() | 交互式输入 |
sleep() | 延时 |
echo() | 输出 |
stdin() | 读取标准输入 |
chalk | 颜色输出 |
fs | 文件系统 |
os | 操作系统 |
path | 路径处理 |
示例:交互式脚本
#!/usr/bin/env zx
const name = await question('What is your name? ')
await $`echo Hello ${name}!`
const shouldDeploy = await question('Deploy? [y/n] ')
if (shouldDeploy === 'y') {
await $`npm run deploy`
}示例:HTTP 请求
#!/usr/bin/env zx
const response = await fetch('https://api.github.com/users/google')
const data = await response.json()
console.log(chalk.green(`Google has ${data.public_repos} public repos`))TypeScript 支持
zx 原生支持 TypeScript:
#!/usr/bin/env zx
import { $ } from 'zx'
interface Config {
name: string
version: string
}
const config: Config = {
name: 'my-app',
version: '1.0.0'
}
await $`echo Building ${config.name}@${config.version}`适用场景
- 构建脚本:自动化构建流程
- 部署脚本:自动化部署
- CI/CD:持续集成/持续部署
- 系统管理:服务器管理脚本
- 开发工具:开发环境配置
相关资源
- 代码示例:https://github.com/google/zx/tree/main/examples
- 轻量版:https://google.github.io/zx/lite
- TypeScript 支持:https://google.github.io/zx/typescript
免责声明
This is not an officially supported Google product.
总结
zx 是一个强大的脚本编写工具,结合了 JavaScript 的便利性和 Shell 的功能:
- 45.2k stars:社区广泛认可
- 跨平台:支持主流操作系统
- 现代语法:使用 async/await
- 类型安全:支持 TypeScript
- 15.5k+ 项目依赖:生产环境验证
对于需要编写复杂脚本的开发者来说,zx 是一个值得尝试的工具。