AgentSkillsCN

bun

Bun运行时的模式与最佳实践。当您运行Bun命令(bun run、bun test、bunx),处理package.json或bun.lock文件,编写在Bun环境下运行的TypeScript脚本,开发Claude Code技能/钩子/脚本(这些工具均以Bun作为运行时环境),或在采用Bun作为包管理器的项目中开展工作时,此工具都将为您提供全方位的支持。内容涵盖bunx执行、锁文件管理、模块解析、使用Bun.$进行Shell脚本编程、子进程启动、文件I/O,以及通过bun test进行测试。

SKILL.md
--- frontmatter
name: bun
description: >-
  Bun runtime patterns and best practices. Use when running bun commands (bun run, bun test, bunx),
  working with package.json or bun.lock, writing TypeScript scripts that run under Bun,
  developing Claude Code skills/hooks/scripts (which use Bun as their runtime),
  or working in projects that use Bun as their package manager.
  Covers bunx execution, lockfile handling, module resolution, shell scripting with Bun.$,
  subprocess spawning, file I/O, and testing with bun test.
user-invocable: false

Bun

bunx

Use --bun before the executable name to force the Bun runtime over Node shebangs. Use -p/--package when the binary name differs from the package name (e.g., bunx -p @angular/cli ng).

See references/bunx.md

Lockfile

bun.lock is a text-based lockfile. Resolve merge conflicts by deleting it and running bun install to regenerate from scratch — never attempt to merge lockfile contents.

See references/lockfile.md

Resolution

Runtime flags must precede the script path: bun --cwd ./packages/app run dev. Never use .js extensions in TypeScript imports — Bun resolves them natively.

See references/resolution.md

Shell

Bun.$ is a tagged template for shell execution. Use response methods (.text(), .json(), .lines()) to extract output, .nothrow() to handle failures without exceptions, and pipe/redirect syntax for composing commands.

See references/shell.md

Subprocess

Bun.spawn spawns subprocesses with streaming I/O. Use Bun.spawnSync for synchronous execution. Configure stdin/stdout/stderr, environment variables, and working directory. Check exitCode for error handling.

See references/spawn.md

File I/O

Bun.file() creates file handles with .text(), .json(), .arrayBuffer(), and .stream() methods. Bun.write() writes content to files or stdout/stderr. Both support streaming for large files.

See references/file-io.md

Testing

Bun runs tests in a single process. Use describe/it/test for structure, expect matchers for assertions, lifecycle hooks (beforeEach, afterEach) for setup/teardown, mock() for function mocking, and .toMatchSnapshot() for snapshot testing. Set AGENT=1 to suppress passing test output.

See references/testing.md