home / skills / forge-town / skills / barrel-export

barrel-export skill

/skills/barrel-export

This skill automatically generates, fixes, and verifies barrel export index files to enforce consistent export of modules across a project.

npx playbooks add skill forge-town/skills --skill barrel-export

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
6.6 KB
---
name: barrel-export
description: 支持自动生成/优化/修复/检查 index 文件,确保所有 index 文件(index.ts / index.js)都遵循桶导出规范。核心原则:所有 index 文件都必须遵循桶导出规范。
dependency:
  python: []
  system: []
---

# 桶导出操作工具

## 任务目标
本 Skill 用于自动生成、优化、修复和检查桶导出文件,确保所有 index 文件都遵循统一的规范:

- **核心原则**:所有 index 文件(index.ts / index.js)都必须遵循桶导出规范
- 自动生成符合规范的 index.ts 文件
- 修复不符合规范的导出
- 检查导出规范并生成报告

## 触发条件
当用户提出以下需求时触发本 Skill:

**核心原则**:所有 index 文件(index.ts / index.js)都必须遵循桶导出规范。

**场景 1:自动生成/优化/修复桶导出文件**
- "生成桶导出"
- "优化桶导出"
- "修复桶导出"
- "修复 index.ts"
- "生成 index.ts"
- "更新桶导出"

**场景 2:检查导出规范**
- "检查导出是否符合规范"
- "检查 index.ts"
- "验证桶导出"
- "检查导出文件"

## 操作步骤

### 场景 1:自动生成/优化/修复桶导出文件

#### 步骤 1:确认目标目录
默认锁定当前目录(`.`)作为操作对象。如果用户指定了目录,则使用指定目录。

#### 步骤 2:扫描当前目录文件
使用系统命令扫描当前目录下的所有文件:

```bash
# 列出所有文件
ls -1

# 或扫描特定扩展名
ls -1 *.ts *.tsx *.js *.jsx 2>/dev/null
```

识别合法的源文件类型:
- `.ts`(TypeScript)
- `.tsx`(TypeScript + JSX)
- `.js`(JavaScript)
- `.jsx`(JavaScript + JSX)

#### 步骤 3:应用过滤规则
排除以下文件类型:

- `index.ts` / `index.js`(桶导出文件本身)
- `*.test.*`(测试文件,如 `Button.test.tsx`)
- `*.spec.*`(规格文件,如 `format.spec.ts`)
- `*.stories.*`(Storybook 文件,如 `Button.stories.tsx`)
- `*.d.ts`(声明文件,如 `global.d.ts`)

#### 步骤 4:应用 _store 特殊规则
判断当前目录是否为 `_store` 目录:

- **如果是 `_store` 目录**:只导出 `store.ts`/`store.js` 和 `provider.ts`/`provider.js`
- **如果是其他目录**:导出所有合法文件

#### 步骤 5:生成导出语句
为每个合法模块生成导出语句:

**统一格式**:
```ts
export * from './模块名';
```

**要求**:
- 使用 `export * from` 语法
- 使用相对路径 `./`
- 按字母顺序排序
- 保持文件名大小写与实际文件一致

#### 步骤 6:写入或更新 index.ts

**如果 index.ts 不存在**:
创建新文件,写入生成的导出语句

**如果 index.ts 已存在**:
1. 读取现有 `index.ts` 内容
2. 对比现有导出和新生成的导出
3. 如果不一致,更新文件

#### 步骤 7:验证结果
检查以下内容:

- ✅ 所有合法模块已导出
- ✅ 无效/重复导出已删除
- ✅ `_store` 特殊规则已应用(如适用)
- ✅ 导出格式统一且按字母排序
- ✅ 文件名大小写与实际文件一致
- ✅ 无"找不到模块"错误

---

### 场景 2:检查导出规范

#### 步骤 1:确认目标目录
与用户确认要检查的目录路径。如果未指定,默认使用当前目录(`.`)。

#### 步骤 2:检查 index.ts 是否存在
使用系统命令检查目标目录下是否存在 `index.ts` 文件:

```bash
ls -la index.ts
```

- **如果不存在**:报告错误,建议先生成 `index.ts`
- **如果存在**:继续执行后续检查

#### 步骤 3:读取并解析 index.ts
使用 `read_file` 工具读取 `index.ts` 的完整内容,解析每一行导出语句。

#### 步骤 4:扫描目录文件
使用系统命令扫描当前目录下的所有文件:

```bash
ls -1 *.ts *.tsx *.js *.jsx 2>/dev/null
```

识别合法的源文件类型:
- `.ts`(TypeScript)
- `.tsx`(TypeScript + JSX)
- `.js`(JavaScript)
- `.jsx`(JavaScript + JSX)

#### 步骤 5:执行核心检查项
执行以下检查:

**检查项 1:文件存在性检查**
- 检查 `index.ts` 文件是否存在

**检查项 2:导出格式规范检查**
- 检查所有导出语句是否使用 `export * from` 格式
- 检查是否有 `export default`、`export { }` 等不规范格式

**检查项 3:排序检查**
- 检查导出语句是否按模块名字母顺序排序

**检查项 4:重复导出检查**
- 检查是否存在重复的模块导出

**检查项 5:导出有效性检查**
- 检查每个导出的模块是否在目录中实际存在
- 检查文件名大小写是否匹配

**检查项 6:排除规则检查**
- 检查是否导出了应排除的文件:
  - `*.test.*`(测试文件)
  - `*.spec.*`(规格文件)
  - `*.stories.*`(Storybook 文件)
  - `*.d.ts`(声明文件)

**检查项 7:_store 特殊规则检查**
- 如果目录名为 `_store`,检查是否只导出 `store` 和 `provider`

#### 步骤 6:生成检查报告
按照以下格式生成详细的检查报告:

```
=== 桶导出规范检查报告 ===

📁 检查目录: <目录路径>

📊 统计信息
- 导出模块总数: X
- 问题总数: X

✅ 检查通过: <数量>
❌ 检查失败: <数量>

---

详细检查结果

1. 文件存在性检查
   ✅ 通过 / ❌ 失败
   说明: <详细说明>

2. 导出格式规范检查
   ✅ 通过 / ❌ 失败
   说明: <详细说明>
   问题列表: <如有问题,列出所有问题>

...

---

💡 修复建议
<如有问题,提供修复建议>

🎯 总体评价
✅ 完全符合规范 / ⚠️ 部分符合规范 / ❌ 不符合规范
```

#### 步骤 7:提供修复建议
对于每个失败的检查项,提供具体的修复建议。

## 注意事项

### 核心原则

**所有 index 文件(index.ts / index.js)都必须遵循桶导出规范**

无论目录类型、文件数量、项目规模如何,只要存在 index 文件,就必须:

1. **使用统一格式**:`export * from './模块名';`
2. **按字母顺序排序**:导出语句按模块名字母顺序排列
3. **排除不应导出的文件**:test、spec、stories、d.ts 等文件不导出
4. **遵循 _store 特殊规则**:如果是 `_store` 目录,只导出 `store` 和 `provider`
5. **确保导出有效性**:所有导出的模块必须实际存在

### 使用原则

- 桶导出文件仅负责导出,不包含任何业务逻辑
- 支持目录路径和文件路径输入,文件路径会自动定位到所在目录
- `_store` 目录的桶导出只导出 `store` 和 `provider`,其他文件不应暴露
- 文档前后描述必须保持一致,避免歧义
- **强制遵循规范**:所有 index 文件都必须符合桶导出规范,无例外

Overview

This skill automates generation, optimization, repair, and validation of index export files to enforce a strict barrel export convention. It ensures every index.ts / index.js follows the rule set: use export * from './module'; alphabetical ordering; exclude test/spec/stories/d.ts; and apply a special _store rule. The tool can create or update index files and produce actionable reports when issues are found.

How this skill works

The skill scans a target directory (default . or user-specified) for valid source files (.ts, .tsx, .js, .jsx), applies exclusion filters, and detects whether the directory is a _store to apply special rules. It generates standardized export lines (export * from './module';), sorts them alphabetically, preserves filename case, and writes or updates the index file only when changes are needed. For checks it parses the existing index, compares against the canonical export set, runs validation checks, and emits a structured report with fix suggestions.

When to use it

  • Create a new barrel index (index.ts / index.js) for a directory
  • Repair or optimize an existing index file that has incorrect or inconsistent exports
  • Validate a repository to ensure all barrels follow the required export rules
  • Enforce the _store convention so only store and provider are exported
  • Generate a report before a CI gate or code review to catch export issues

Best practices

  • Run the skill from the directory root or pass explicit paths to avoid accidental edits
  • Always commit or stash changes before running automated fixes so edits are reviewable
  • Prefer running the validator in CI to catch nonconforming barrels early
  • Treat barrel files as pure export surfaces—do not add logic to index files
  • Keep file name case consistent on disk to avoid module resolution problems

Example use cases

  • Generate index.ts for a newly added component folder with multiple .tsx and .ts files
  • Fix a broken barrel where some exports referenced missing files or used default/named export syntax
  • Validate a codebase to produce a checklist of export issues and exact remediation steps
  • Enforce _store rule in Redux/Context folders so only store and provider are exported
  • Automatically update index.ts after a refactor that renamed or removed modules

FAQ

Will the skill modify files automatically?

Yes—when asked to generate or repair it will create or update index files. Validation-only mode produces a report and suggestions without writing changes.

How does it treat test, story, spec and .d.ts files?

Those files are excluded by default and will never be exported from a barrel; the checker reports any violations and the fixer removes such exports.