Coding Guidelines
【免费下载链接】repomix📦 Repomix is a powerful tool that packs your entire repository into a single, AI-friendly file. Perfect for when you need to feed your codebase to Large Language Models (LLMs) or other AI tools like Claude, ChatGPT, DeepSeek, Perplexity, Gemini, Gemma, Llama, Grok, and more.项目地址: https://gitcode.com/GitHub_Trending/rep/repomix
- Follow the Airbnb JavaScript Style Guide
- Suggest splitting files into smaller, focused units when appropriate
- Add comments for non-obvious logic. Keep all text in English
- All new features should have corresponding unit tests
Generate Comprehensive Output
- Include all content without abbreviation, unless specified otherwise
- Optimize for handling large codebases while maintaining output quality
然后在配置中加入: ```json5 { "output": { "instructionFilePath": "repomix-instruction.md", // other options... } }指令内容会追加在输出文件末尾的专属区段。长上下文资料置于提示词顶部、查询与指令置于底部,能显著提升模型响应质量。
8.6 注释移除
output.removeComments设为true时会尝试移除支持类型文件的注释,减小输出并聚焦核心代码。支持语言:HTML、CSS、JavaScript、TypeScript、Vue、Svelte、Python、PHP、Ruby、C、C#、Java、Go、Rust、Swift、Kotlin、Dart、Shell、YAML。
注意:注释移除采用保守策略以避免误删代码,复杂情况下部分注释可能被保留。
8.7 文件处理器(input.processors)
input.processors在打包之前用外部命令转换文件内容,匹配文件的原始内容被命令的 stdout 替换——适用于降 Token 或格式转换(JSON→TOON、SVG 压缩、notebook→脚本):
{ "input": { "processors": [ { "pattern": "**/*.json", "command": "npx @toon-format/cli {file}" } ] } }{file}占位符(必填)会被替换为保存文件内容的临时文件路径,命令 stdout 成为新内容。模式按序求值、首个匹配生效(每文件一个处理器)。每项还接受timeout(毫秒,默认60000)与onError("fail"中止打包为默认;"skip"警告并保留原始内容)。Schema 约束见 configSchema.ts。
常用示例:
| 模式 | command | 作用 |
|---|---|---|
**/*.json | jq -c . {file} | 去除空白压缩 JSON |
**/*.json | npx @toon-format/cli {file} | 转换为紧凑的 TOON 格式 |
**/*.svg | npx svgo -i {file} -o - | 压缩 SVG |
**/*.ipynb | jupyter nbconvert --to script --stdout {file} | 将 Jupyter notebook 转为纯 Python 脚本 |
警告:文件处理器会执行配置文件中的任意命令,因此默认拒绝执行:
- 仅本地 CLI 运行启用——与 npm 脚本或 Makefile 同级的信任边界。若你在他人提供的仓库中运行
repomix而未先审查其repomix.config.json,处理器命令将在你的机器上执行,务必先审查不受信任仓库的配置。- 对库 API(
pack()/runCli())、MCP 服务器与托管网站禁用(cliRun.ts 仅在真实 CLI 入口注入enableFileProcessors: true)。- 远程仓库(
--remote)只有在显式传入--remote-trust-config时才信任克隆的配置及处理器;否则远程配置根本不会被加载。- 凭证请通过环境变量(如
$TOKEN)引用而非内联,日志打印的是未展开的变量名;超时时 Repomix 会杀掉命令的 shell,但命令自拉起的常驻后台进程可能残留。
九、安全检测
Repomix 内置基于 Secretlint 的安全检查,检测文件中潜在的敏感信息,帮助你在分享打包仓库前识别风险。检测结果在打包完成后显示于 CLI 输出:
🔍 Security Check: ────────────────── 2 suspicious file(s) detected: 1. src/utils/test.txt 2. tests/utils/secretLintUtils.test.ts Please review these files for potentially sensitive information.默认启用。可关闭:
{ "security": { "enableSecurityCheck": false } }或使用 CLI 选项:
repomix --no-security-check关闭安全检查可能泄露敏感信息,仅在测试文件或含示例凭据的文档等必要场景使用。
实现上,securityCheck.ts 会把文件内容(以及启用时的 git diff、git log 内容)分批(每批 50 项)交给最多 2 个 worker 线程的线程池执行 secretlint 检测,返回可疑文件结果后,packager.ts 会从最终输出中剔除这些文件。检测规则本身位于 workers/securityCheckWorker.ts。
远程仓库配置信任
repomix.config.*是代码而非纯数据:.ts/.js配置加载时会被执行,input.processors会运行外部命令,路径选项可读取仓库外文件。加载陌生仓库的配置约等于运行它的 Makefile。
因此克隆仓库的配置默认绝不加载(你的全局配置与 CLI 选项仍然生效)。用--remote-trust-config(或REPOMIX_REMOTE_TRUST_CONFIG=true)选择信任时,交互式终端会先展示即将运行的配置再询问:
- Yes, once—— 仅信任本次运行。
- Yes, and don't ask again for this repository—— 决策基于内容固定(content-pinned):记录所批准配置的哈希,若该仓库之后变更配置会再次询问(direnv allow 模式)。
- No(默认选中)—— 中止且不加载配置。
展示的配置会转义控制字符、ANSI 与双向字符,输出有上限、每行加前缀,指向克隆目录外的符号链接配置会被拒绝——你审查的就是将运行的。该提示在--force、非交互 shell(如 CI)以及已选择始终信任时跳过。
重要:内容固定仅覆盖入口配置文件。
.ts/.js配置可以import其他文件,input.processors可以调用外部脚本,这些都不会被哈希。请将"不再询问"理解为对该仓库的信任,而不只是对读到的那个文件的信任。
远程信任决策的持久化实现位于 remoteConfigTrustStore.ts,提示 UI 见 remoteConfigTrustPrompt.ts。
十、MCP 服务器集成
Repomix 支持 Model Context Protocol(MCP),让 AI 助手直接与你的代码库交互。以 MCP 服务器运行时,Repomix 提供工具让 AI 无需手动准备文件即可打包本地或远程仓库:
repomix --mcpMCP 服务器入口见 mcpServer.ts,工具注册与运行见 mcpToolRuntime.ts。
10.1 沙箱模式
默认 MCP 服务器可读取宿主用户可读的任何路径——对可信本地助手方便,但对不可信客户端或 Agent 过于宽泛。--sandbox将文件工具限制在单一工作区内:
# 限制在当前工作目录 repomix --mcp --sandbox # 限制在指定目录 repomix --mcp --sandbox path/to/project沙箱开启时,所有路径都相对于工作区根(绝对路径、~、..、Windows 盘符/UNC 路径,以及通过符号链接解析到根之外的路径均被拒绝),且只注册只读、根受限的工具;远程打包、Skill 生成、附加外部输出均被禁用。原始文件工具(file_system_read_file、file_system_read_directory)仅在沙箱模式下可用。这是工具面的应用层限制,而非操作系统级沙箱。路径约束实现见 pathScope.ts。
10.2 配置 MCP 服务器
VS Code(两种方式,任选其一):
- 使用安装徽章一键安装(
npx -y repomix --mcp)。 - 命令行:
code --add-mcp '{"name":"repomix","command":"npx","args":["-y","repomix","--mcp"]}' code-insiders --add-mcp '{"name":"repomix","command":"npx","args":["-y","repomix","--mcp"]}'Cline(VS Code 扩展):编辑cline_mcp_settings.json:
{ "mcpServers": { "repomix": { "command": "npx", "args": ["-y", "repomix", "--mcp"] } } }Cursor:在Cursor Settings>MCP>+ Add new global MCP server中添加类似配置。
Claude Desktop:编辑claude_desktop_config.json,配置与 Cline 类似。
Claude Code:
claude mcp add repomix -- npx -y repomix --mcp使用 Docker 而非 npx:
{ "mcpServers": { "repomix-docker": { "command": "docker", "args": ["run", "-i", "--rm", "ghcr.io/yamadashy/repomix", "--mcp"] } } }配置完成后,AI 助手可直接调用 Repomix 能力分析代码库,无需手动准备文件。
10.3 可用 MCP 工具
pack_codebase:将本地代码目录打包为 AI 分析用 XML。
directory:要打包目录的绝对路径compress:(可选,默认 false)启用 Tree-sitter 压缩,可减少约 70% Token 且保留语义;通常无需,因为 grep 工具支持增量获取includePatterns/ignorePatterns:(可选)fast-glob 模式,逗号分隔outputPatterns:(可选)按文件包含级别,数组{ pattern, compress?, directoryStructureOnly? },首个匹配生效topFilesLength:(可选,默认 10)摘要中显示的最大文件数
attach_packed_output:附加已存在的打包输出文件供 AI 分析。
path:含 repomix-output.xml 的目录或直接指向打包 XML 文件topFilesLength:(可选,默认 10)- 免去重新处理,直接访问既有打包仓库
pack_remote_repository:获取、克隆并打包 GitHub 仓库。
remote:GitHub URL 或 user/repo 格式- 其余参数与 pack_codebase 相同
read_repomix_output:读取 Repomix 输出文件,支持按行范围分段读取大文件。
outputId:输出文件 IDstartLine/endLine:(可选)起始/结束行号(1 起,闭区间)
grep_repomix_output:用 grep 式功能(JavaScript RegExp 语法)在输出文件中搜索。
pattern:搜索模式contextLines:(可选,默认 0)前后上下文行数beforeLines/afterLines:(可选)分别控制前后上下文,优先级高于 contextLinesignoreCase:(可选,默认 false)忽略大小写
file_system_read_file(仅沙箱模式):读取沙箱工作区内文件,
path相对工作区根(如src/index.ts)。匹配已知密钥格式的内容会被拒绝(额外启发式防护)。file_system_read_directory(仅沙箱模式):列出沙箱工作区内目录,带
[FILE]/[DIR]指示符。
十一、Claude Code 插件与 Agent Skills
11.1 Claude Code 插件
安装:
/plugin marketplace add yamadashy/repomix # 安装 MCP 服务器插件(推荐基础) /plugin install repomix-mcp@repomix # 安装命令插件(扩展功能) /plugin install repomix-commands@repomix # 安装仓库探索插件(AI 分析) /plugin install repomix-explorer@repomix或使用交互式安装器:/plugin。
插件一览:
- repomix-mcp(MCP 服务器插件):打包本地与远程仓库、读取和搜索打包输出、自动 Tree-sitter 压缩(约 70% Token 削减)。
- repomix-commands(斜杠命令插件):
/repomix-commands:pack-local—— 以各种选项打包本地代码库/repomix-commands:pack-remote—— 打包并分析远程 GitHub 仓库- 示例:
/repomix-commands:pack-local后接Pack this project as markdown with compression;/repomix-commands:pack-remote yamadashy/repomix后接Pack only TypeScript files...
- repomix-explorer(AI 分析 Agent 插件):自然语言探索与分析,自动先运行
npx repomix@latest打包,再用 Grep/Read 工具增量搜索输出。示例:/repomix-explorer:explore-local ./src后接Find all authentication-related code。
本仓库内已附带一份可直接使用的 Repomix Explorer Skill 定义,见 skills/repomix-explorer/SKILL.md。
11.2 Agent Skills 生成
Repomix 可以生成 Claude Agent Skills 格式输出,创建可复用的代码库参考目录:
# 从本地目录生成 repomix --skill-generate # 自定义 Skill 名称 repomix --skill-generate my-project-reference # 自定义项目名 repomix --skill-generate --skill-project-name "My Project" # 从远程仓库生成 repomix --remote https://github.com/user/repo --skill-generate运行时会询问保存位置:个人 Skills(~/.claude/skills/,全项目可用)或项目 Skills(.claude/skills/,随 git 共享)。
非交互用法(CI 与自动化):
repomix --skill-generate --skill-output ./my-skills repomix --skill-generate --skill-output ./my-skills --force repomix --remote user/repo --skill-generate my-skill --skill-output ./output --force生成结构:
.claude/skills/<skill-name>/ ├── SKILL.md # 主 Skills 元数据与文档 └── references/ ├── summary.md # 用途、格式与统计 ├── project-structure.md # 带行数的目录树 ├── files.md # 全部文件内容(grep 友好) └── tech-stacks.md # 语言、框架、依赖自动生成名称:未提供名称时自动生成——repomix src/ --skill-generate生成repomix-reference-src;--remote user/repo生成repomix-reference-repo;CustomName规范化为 kebab-case。Skills 生成实现见 skill/ 目录(如 packSkill.ts、skillTechStack.ts)。
与其他特性组合:
repomix --skill-generate --include "src/**/*.ts" --ignore "**/*.test.ts" repomix --skill-generate --compress repomix --remote yamadashy/repomix --skill-generate十二、GitHub Actions 集成
在 CI 工作流中自动化打包:
- name: Pack repository with Repomix uses: yamadashy/repomix/.github/actions/repomix@main with: output: repomix-output.xml style: xml不同格式与目录、压缩组合:
- name: Pack repository with Repomix uses: yamadashy/repomix/.github/actions/repomix@main with: output: repomix-output.md style: markdown- name: Pack repository with Repomix uses: yamadashy/repomix/.github/actions/repomix@main with: directories: src tests include: "**/*.ts,**/*.md" ignore: "**/*.test.ts" output: repomix-output.txt compress: true上传产物:
- name: Pack repository with Repomix uses: yamadashy/repomix/.github/actions/repomix@main with: directories: src output: repomix-output.txt compress: true - name: Upload Repomix output uses: actions/upload-artifact@v7 with: name: repomix-output path: repomix-output.txt完整工作流示例:
name: Pack repository with Repomix on: workflow_dispatch: push: branches: [ main ] pull_request: branches: [ main ] jobs: pack-repo: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v7 - name: Pack repository with Repomix uses: yamadashy/repomix/.github/actions/repomix@main with: output: repomix-output.xml - name: Upload Repomix output uses: actions/upload-artifact@v7 with: name: repomix-output.xml path: repomix-output.xml retention-days: 30Action 输入:
| 名称 | 说明 | 默认 |
|---|---|---|
directories | 空格分隔的目录列表(如src tests docs) | . |
include | 逗号分隔的 include glob(如**/*.ts,**/*.md) | "" |
ignore | 逗号分隔的 ignore glob(如**/*.test.ts,**/node_modules/**) | "" |
output | 打包文件相对路径(扩展名决定格式:.txt、.md、.xml) | repomix-output.xml |
compress | 启用智能压缩 | true |
style | 输出风格(xml、markdown、json、plain) | xml |
additional-args | 额外裸参数(如--no-file-summary --no-security-check) | "" |
repomix-version | npm 包版本(支持 semver 范围、标签或具体版本) | latest |
Action 输出:output_file—— 生成文件的路径,可在后续步骤中用于产物上传、LLM 处理等。
十三、以库的形式使用 Repomix
除了 CLI,Repomix 也可作为 Node.js 库嵌入应用。所有公共导出见 src/index.ts。
13.1 安装与基础用法
npm install repomiximport { runCli, type CliOptions } from 'repomix'; // 用自定义选项处理当前目录 async function packProject() { const options = { output: 'output.xml', style: 'xml', compress: true, quiet: true } as CliOptions; const result = await runCli(['.'], process.cwd(), options); return result.packResult; }13.2 处理远程仓库
import { runCli, type CliOptions } from 'repomix'; // 克隆并处理 GitHub 仓库 async function processRemoteRepo(repoUrl) { const options = { remote: repoUrl, output: 'output.xml', compress: true } as CliOptions; return await runCli(['.'], process.cwd(), options); }13.3 使用核心组件
需要更细粒度控制时使用底层 API:
import { searchFiles, collectFiles, processFiles, TokenCounter } from 'repomix'; async function analyzeFiles(directory) { // 查找并收集文件 const { filePaths } = await searchFiles(directory, { /* config */ }); const rawFiles = await collectFiles(filePaths, directory); const processedFiles = await processFiles(rawFiles, { /* config */ }); // 统计 Token const tokenCounter = new TokenCounter('o200k_base'); // 返回分析结果 return processedFiles.map(file => ({ path: file.path, tokens: tokenCounter.countTokens(file.content) })); }打包全流程编排见 packager.ts:搜索文件 → 排序 → 收集内容(与 git diff/log 子进程并行)→ 安全检查与文件处理并发 → 过滤可疑文件 → 生成并写出输出(与指标计算并发),其中还包含 Token 缓存预加载、git 变更频率预取、worker 线程池预热等性能优化。
13.4 打包注意事项
使用 Rolldown 或 esbuild 等工具打包 repomix 时,部分依赖需保持外部、WASM 文件需复制:
- 必须外部的依赖:
tinypool(通过文件路径生成 worker 线程)。 - 需复制的 WASM 文件:
web-tree-sitter.wasm→ 与打包 JS 同目录(代码压缩功能需要);Tree-sitter 语言文件 →REPOMIX_WASM_DIR环境变量指定目录。
十四、常见问题与提示词模板
生成打包文件后,可搭配以下提示词使用:
代码审查与重构:
This file contains my entire codebase. Please review the overall structure and suggest any improvements or refactoring opportunities, focusing on maintainability and scalability.文档生成:
Based on the codebase in this file, please generate a detailed README.md that includes an overview of the project, its main features, setup instructions, and usage examples.测试用例生成:
Analyze the code in this file and suggest a comprehensive set of unit tests for the main functions and classes. Include edge cases and potential error scenarios.代码质量评估:
Review the codebase for adherence to coding best practices and industry standards. Identify areas where the code could be improved in terms of readability, maintainability, and efficiency. Suggest specific changes to align the code with best practices.库概览:
This file contains the entire codebase of library. Please provide a comprehensive overview of the library, including its main purpose, key features, and overall architecture.【免费下载链接】repomix📦 Repomix is a powerful tool that packs your entire repository into a single, AI-friendly file. Perfect for when you need to feed your codebase to Large Language Models (LLMs) or other AI tools like Claude, ChatGPT, DeepSeek, Perplexity, Gemini, Gemma, Llama, Grok, and more.项目地址: https://gitcode.com/GitHub_Trending/rep/repomix
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考