zx-script
Write, refactor, or improve zx-based CLI scripts. Use when creating shell automation with JavaScript/TypeScript, converting bash scripts to zx, or adding error handling and logging to existing zx scripts.
适合你,如果常用 shell 脚本但想用 JS/TS 替代
/ 通过 npx 安装 校验哈希
npx oh-my-skill add quanxiaoxiao/quan-skills/zx-script/ 通过 bash 安装
curl -fsSL https://oh-my-skill.com/install.sh | bash -s -- quanxiaoxiao/quan-skills/zx-script/ 已经装过?验证本机副本,不用重装
npx oh-my-skill verify quanxiaoxiao/quan-skills/zx-script安装目标可用 --agent / --scope 或 --to 明确指定;省略时只会在唯一已存在的 agent 目录上自动选择,零命中或多命中会停止并提示。content_hash 缺失或不一致均拒装。
0GitHub stars
~956最小装载
~956含声明引用
~1K文本包总量
索引托管
怎么用
技能原文 SKILL.md
ZX Script
Create portable, maintainable CLI scripts using zx with minimal dependencies.
Use This Skill
Use this skill when:
- Writing new CLI automation scripts
- Converting existing bash/shell scripts to zx
- Refactoring zx scripts for better error handling
- Adding structured logging to zx scripts
- Creating reusable script utilities
- Packaging scripts as npm binaries
Do NOT use this skill when:
- The task is a one-off shell command (use raw bash instead)
- The script needs complex TUI interactions (use a dedicated CLI framework)
- The script requires heavy data processing (use a proper application)
- The environment cannot run Node.js
Pre-Read Order
Read these files before writing or editing:
../../rules/scripts-global.rule.md- Base constraints for all scripts../../checklists/scripts-global.checklist.md- Verification checklist for all scriptspackage.json- Check if zx is already a dependency- Existing scripts in
scripts/orbin/directories README.md- For usage context and conventions.nvmrcorenginesfield - For Node version constraints
This skill inherits all constraints from scripts-global and adds zx-specific guidance on top.
Workflow
- Check the current setup:
- Verify zx is installed (
npm list zxor check package.json) - Identify the target Node version
- Look for existing script patterns in the repository
- Create or refactor the script:
- Add shebang:
#!/usr/bin/env zx - Import only what's needed from zx:
import { $, cd, fetch, sleep } from 'zx' - Set explicit options:
$.verbose = falseor$.quiet = trueas needed
- Prefer native tools over dependencies:
- Use
fetch()from zx instead of axios/node-fetch - Use
fsmodule instead of fs-extra - Use native
Arraymethods instead of lodash - Use built-in
pathandosmodules
- Structure the script:
- Parse arguments early using
process.argvor minimist if needed - Validate inputs before execution
- Group related operations into async functions
- Use
cd()carefully and reset paths when needed
- Add error handling:
- Wrap shell commands in try-catch blocks
- Check exit codes explicitly for critical operations
- Provide meaningful error messages
- Exit with non-zero codes on failure
- Add logging:
- Use
console.log()for user-facing output - Use
console.error()for errors - Use
$.verbose = trueonly for debugging - Keep output clean and actionable
Implementation Rules
- Single purpose: One script = one task
- No side effects: Don't modify system state without explicit flags
- Dry-run support: Add
--dry-runflag when modifying files/state - Idempotent: Running the script multiple times should be safe
- Portable: Avoid OS-specific commands; use zx helpers
- Minimal deps: Only add npm packages when native tools are insufficient
- zx scripts must use functions only.
- Do not generate classes in zx scripts.
- Use async functions and composition.
- Organize logic through small functions.
- Prefer closures when state is required.
- Avoid static utility classes.
Example pattern:
const fetchData = async () => {
...
}
const main = async () => {
const data = await fetchData()
}
await main()
Verification
Always test scripts before considering them complete:
- Syntax check:
npx zx --check script.mjs - Dry run: Execute with
--dry-runif implemented - Happy path: Run with valid inputs
- Error cases: Test with invalid inputs and verify error messages
- Exit codes: Verify
echo $?returns 0 on success, non-zero on failure - Dependencies: Ensure script works with locked dependencies (
npm ci)
Output Contract
Return results in this order:
- Summary - What the script does and when to use it
- Usage - Command examples with common flags
- Changes - What was created or modified
- Verification - How to test the script
- Dependencies - Any new packages added
Keep responses focused on the script implementation. Include example commands that can be copied and run.
按 MIT 许可原样转载,未经改动 · 在 GitHub 查看 →
评论
登录即可评论;带「已验证安装」的,是发布者名下有本店的安装或持有记录。
…