‹ 首页

zx-script

@quanxiaoxiao · 收录于 今天 · 上游提交 3 个月前

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作者撰写 · MIT · f6f857f

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:

  1. ../../rules/scripts-global.rule.md - Base constraints for all scripts
  2. ../../checklists/scripts-global.checklist.md - Verification checklist for all scripts
  3. package.json - Check if zx is already a dependency
  4. Existing scripts in scripts/ or bin/ directories
  5. README.md - For usage context and conventions
  6. .nvmrc or engines field - For Node version constraints

This skill inherits all constraints from scripts-global and adds zx-specific guidance on top.

Workflow
  1. Check the current setup:
  2. Verify zx is installed (npm list zx or check package.json)
  3. Identify the target Node version
  4. Look for existing script patterns in the repository
  1. Create or refactor the script:
  2. Add shebang: #!/usr/bin/env zx
  3. Import only what's needed from zx: import { $, cd, fetch, sleep } from 'zx'
  4. Set explicit options: $.verbose = false or $.quiet = true as needed
  1. Prefer native tools over dependencies:
  2. Use fetch() from zx instead of axios/node-fetch
  3. Use fs module instead of fs-extra
  4. Use native Array methods instead of lodash
  5. Use built-in path and os modules
  1. Structure the script:
  2. Parse arguments early using process.argv or minimist if needed
  3. Validate inputs before execution
  4. Group related operations into async functions
  5. Use cd() carefully and reset paths when needed
  1. Add error handling:
  2. Wrap shell commands in try-catch blocks
  3. Check exit codes explicitly for critical operations
  4. Provide meaningful error messages
  5. Exit with non-zero codes on failure
  1. Add logging:
  2. Use console.log() for user-facing output
  3. Use console.error() for errors
  4. Use $.verbose = true only for debugging
  5. 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-run flag 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:

  1. Syntax check: npx zx --check script.mjs
  2. Dry run: Execute with --dry-run if implemented
  3. Happy path: Run with valid inputs
  4. Error cases: Test with invalid inputs and verify error messages
  5. Exit codes: Verify echo $? returns 0 on success, non-zero on failure
  6. Dependencies: Ensure script works with locked dependencies (npm ci)
Output Contract

Return results in this order:

  1. Summary - What the script does and when to use it
  2. Usage - Command examples with common flags
  3. Changes - What was created or modified
  4. Verification - How to test the script
  5. Dependencies - Any new packages added

Keep responses focused on the script implementation. Include example commands that can be copied and run.

按 MIT 许可原样转载,未经改动 · 在 GitHub 查看 →

评论

登录即可评论;带「已验证安装」的,是发布者名下有本店的安装或持有记录。