设置

语言

Claude 代码技能:为您的 AI 编码助手构建自定义工作流

L
LemonData
·2026年2月26日·69 次浏览
#Claude Code#技能#开发者工具#教程#生产力
Claude 代码技能:为您的 AI 编码助手构建自定义工作流

Claude Code 技能:为您的 AI 编码助手构建自定义工作流程

Claude Code 配备了通用的 AI 助手。技能让您可以对其进行专业化。技能是一个 markdown 文件,教会 Claude Code 如何处理特定类型的任务:部署到 Kubernetes、编写数据库迁移、审查拉取请求,或遵循您团队的编码规范。

“帮我写一个 React 组件”和“帮我写一个遵循我们设计系统、使用自定义 hooks、具备正确错误边界和无障碍属性的 React 组件”之间的区别,就是一个技能。

技能到底是什么

技能是位于 .claude/commands/(项目级)或 ~/.claude/commands/(全局)中的 markdown 文件。当您在 Claude Code 中输入 /skill-name,该文件内容会作为指令注入对话中。

.claude/
  commands/
    deploy.md          # /deploy
    review-pr.md       # /review-pr
    write-test.md      # /write-test

仅此而已。无特殊语法,无需编译,无需 SDK。只是描述如何完成某事的 markdown 文件。

编写您的第一个技能

这里有一个实用示例:一个强制执行团队提交信息规范的技能。

创建 .claude/commands/commit.md

# Commit Workflow

## Steps
1. Run `git diff --staged` to see what's being committed
2. Analyze the changes and categorize: feat, fix, refactor, docs, test, chore
3. Write a commit message following our convention:
   - Format: `type(scope): description`
   - Scope is the package or module name
   - Description is imperative mood, lowercase, no period
   - Body explains WHY, not WHAT
4. If changes touch multiple scopes, create separate commits
5. Run `git commit -m "message"` with the generated message

## Rules
- Never use `--no-verify` to skip hooks
- Never amend published commits
- If tests fail in pre-commit, fix the issue first

## Examples
- `feat(billing): add stripe webhook handler`
- `fix(auth): handle expired refresh tokens`
- `refactor(api): extract rate limiter to shared package`

现在,输入 /commit 会给 Claude Code 一个结构化的工作流程,而不是模糊的“提交我的更改”指令。

技能设计模式

清单模式

适合包含多个核查步骤的任务。

# Pre-Deploy Checklist

Before deploying, verify each item:

- [ ] `pnpm typecheck` passes
- [ ] `pnpm test` passes
- [ ] No console.log statements in production code
- [ ] Environment variables documented in .env.example
- [ ] Database migrations are reversible
- [ ] API changes are backward compatible

If any check fails, stop and report the issue. Do not proceed with deployment.

决策树模式

适合根据上下文决定处理方式的任务。

# Bug Fix Workflow

1. Reproduce the bug (find or write a failing test)
2. Identify the root cause:
   - If it's a type error → fix the type definition at the source
   - If it's a race condition → add proper locking/sequencing
   - If it's a missing validation → add schema validation at the boundary
   - If it's a logic error → fix and add regression test
3. Verify the fix doesn't break existing tests
4. Write a test that would have caught this bug

模板模式

适合生成一致输出。

# New API Endpoint

Create a new API endpoint following our conventions:

## File Structure
- Route handler: `apps/api/src/routes/{resource}/{action}.ts`
- Schema: `apps/api/src/schemas/{resource}.ts`
- Test: `apps/api/src/routes/{resource}/__tests__/{action}.test.ts`

## Required Elements
- Zod schema for request validation
- Authentication middleware
- Rate limiting
- Structured error responses using errorResponse()
- Success responses using successResponse()
- OpenAPI documentation comments

安装社区技能

Claude Code 生态系统拥有不断增长的社区技能库。使用以下命令安装:

npx add-skill username/repo-name -y

热门技能合集:

  • coreyhaines31/marketingskills(29 个市场营销/SEO 技能)
  • hedging8563/lemondata-api-skill(LemonData API 集成)

安装的技能会出现在 ~/.claude/commands/,并可跨所有项目使用。

项目级技能 vs 全局技能

位置 范围 使用场景
.claude/commands/ 仅限当前项目 项目规范、部署工作流程
~/.claude/commands/ 所有项目 个人偏好、通用工具

项目技能应提交到代码仓库,让整个团队受益。全局技能则用于个人工作流程偏好。

高级用法:带钩子的技能

技能可以引用钩子(在特定事件触发的 shell 命令)实现自动化执行:

# Pre-Commit Check

Before any commit, the following hooks run automatically:
- `pre-commit`: runs typecheck + lint
- `post-commit`: updates changelog

If a hook fails, investigate the error output and fix the issue.
Do not use --no-verify to bypass hooks.

钩子配置位于 .claude/settings.json

{
  "hooks": {
    "pre-commit": "pnpm typecheck && pnpm lint-staged"
  }
}

打造高效技能的建议

  1. 明确文件路径和命名规范。“创建组件”过于模糊,“在 src/components/ui/ 使用 PascalCase 命名创建组件”更具操作性。

  2. 包含正确输出示例。Claude Code 从示例中学习效果更好,胜过抽象规则。

  3. 定义禁止事项。“绝不使用 any 类型”比“使用合适类型”更易执行。

  4. 保持技能聚焦。每个技能对应一个工作流程。一个覆盖所有内容的 200 行技能,不如五个各自处理单一任务的 40 行技能实用。

  5. 为技能版本管理。随着规范演进,更新技能。过时技能比没有技能更糟,因为它们强制执行旧模式。

实际影响

采用技能的团队报告了持续的改进:

  • 代码审查周期缩短,因为规范在审查前已被执行
  • 新成员入职时间减少,因为新人获得与资深开发同样的指导
  • AI 生成代码质量提升,因为 AI 明确了解项目标准

投入少(编写前几个技能只需 30 分钟),回报随着每次交互不断累积。


用 AI 构建,遵循您自己的规则。lemondata.cc 提供 AI 驱动开发工具的 API 基础设施。

分享: