← 返回 Skills 市场
wangzhiming1999

Find Orphans

作者 wangzhiming · GitHub ↗ · v1.0.0 · MIT-0
cross-platform ✓ 安全检测通过
98
总下载
0
收藏
0
当前安装
1
版本数
在 OpenClaw 中安装
/install find-orphans
功能描述
Finds orphaned files, unused components, and dead code in projects. Use when 清理代码, 查找孤儿文件, 删除无用代码, cleanup, find unused, or removing legacy code.
使用说明 (SKILL.md)

孤儿文件与无效代码清理(Find Orphans)

系统化分析项目中的孤儿文件、未使用的组件和无效代码,帮助清理历史遗留。

触发场景

  • 用户说「清理无用代码」「查找孤儿文件」「删除未使用的文件」「项目瘦身」
  • 重构前的准备工作、代码审计
  • 项目历史遗留清理

执行流程

1. 项目结构分析

读取 package.json,识别:

  • 框架类型(React/Vue/Angular/Next.js)
  • 构建工具(Vite/Webpack/Rollup)
  • 入口文件(main 字段、scripts.dev/build 指向的文件)

扫描目录结构,识别:

  • 源码目录:src/app/lib/
  • 组件目录:components/views/pages/
  • 工具目录:utils/helpers/hooks/
  • 样式目录:styles/css/

2. 孤儿文件检测

步骤一:收集所有源码文件

用 Glob 扫描所有 .ts.tsx.vue.js.jsx.css.scss 文件,排除:

  • node_modules/dist/build/.next/
  • *.config.**.d.ts
  • *.test.**.spec.*(除非用户要求包含)

步骤二:提取所有 import 引用

用 Grep 搜索所有 import/require 语句,将相对路径解析为绝对路径,构建「被引用文件集合」。

步骤三:比对找出孤儿

所有源码文件 - 入口文件 - 被引用文件集合 = 孤儿文件

特殊检测

  • 路由孤儿:读取路由配置文件(router/index.tsroutes.ts),找出 pages/ 目录下未在路由中注册的组件
  • 样式孤儿:检查 .css/.scss/.less 是否被任何文件 import 或在 HTML 中 \x3Clink> 引用
  • 资源孤儿:检查 assets/ 中的图片/字体是否在代码或样式中被引用

3. 未使用组件检测

导入但未使用

对每个文件,提取 import 的标识符,检查该标识符是否在文件其余部分出现(排除 import 行本身):

  • React:检查是否出现在 JSX 标签中(\x3CButton
  • Vue:检查是否在 components: {} 注册且在 \x3Ctemplate> 中使用

Barrel exports 未使用

找到所有 index.ts barrel 文件,提取其 export 的标识符,全局搜索这些标识符是否被其他文件 import。未被引用的即为无效导出。

4. 无效工具函数检测

扫描 utils/helpers/hooks/lib/ 目录:

导出但未被 import

  • 找到所有 export function / export const 定义
  • 全局搜索是否有文件 import 了这个名字
  • 未被 import 的标记为无效

文件内定义但未调用的私有函数

  • 找出所有非 export 的 function xxx 定义
  • 检查函数名是否在文件其他位置被调用

重复工具函数

  • 在 utils 目录中搜索功能相似的函数(相同参数类型、相似函数名)
  • 标记为「可能重复,建议合并」

5. 安全验证

在标记为「可删除」前,对每个候选文件验证:

  • 不在 package.json 的 filesexports 字段中
  • 不是动态 import 的目标(搜索 import( 中的字符串拼接)
  • 不在 vite.config / webpack.config 的 entry
  • 不被 .gitignore 的反向规则保护(!filename

6. 输出清理报告

按以下格式输出,并询问用户是否生成删除脚本:

输出模板

## 孤儿文件清理报告

### 📊 统计摘要
- 扫描文件总数:N
- 孤儿文件:N 个
- 未使用组件:N 个
- 无效工具函数:N 个
- 预计可删除代码:~N 行

---

### 🔴 建议立即删除

#### 完全未引用的文件
| 文件 | 行数 | 最后修改 | 备注 |
|------|------|----------|------|
| `src/components/OldModal.tsx` | 312 | 2023-08 | 可能被 NewModal.tsx 替代 |
| `src/utils/legacy.ts` | 89 | 2022-11 | 导出 3 个函数,全部未使用 |

#### 路由未注册的页面组件
- `src/pages/TestPage.tsx` — 未在 router/index.ts 中注册

#### 孤儿样式文件
- `src/styles/old-theme.scss` — 无任何文件 import

---

### 🟡 需要人工确认

#### 可能被动态引用
- `src/plugins/dynamicLoader.ts` — 项目中存在动态 import 模式,无法静态分析确认

#### 导入但未在 JSX 中使用的组件
- `src/pages/Dashboard.tsx` 中导入了 `\x3CChart>` 但未使用(可能是注释掉的功能)

---

### 🟢 已排除(自动跳过)
- 配置文件:vite.config.ts、tsconfig.json 等
- 类型声明:*.d.ts
- 公共入口:main.tsx、App.tsx

---

### 🔧 下一步

是否生成删除脚本?运行前建议先:
1. `git checkout -b cleanup/remove-orphans`
2. 审查脚本中的每个文件
3. 执行脚本后运行构建验证:`npm run build`

如果用户确认,生成 cleanup-orphans.sh

#!/bin/bash
# 孤儿文件清理脚本 - 生成于 YYYY-MM-DD
# 回滚:git checkout backup-before-cleanup
set -e

git rm src/components/OldModal.tsx
git rm src/utils/legacy.ts
git rm src/pages/TestPage.tsx
git rm src/styles/old-theme.scss

echo "清理完成,请运行 npm run build 验证"

误报场景(需人工判断)

场景 原因 处理方式
动态路由文件 pages/[id].tsx 通过约定路由加载 检查框架路由约定
require.context Webpack 批量加载目录 搜索 require.context 用法
环境变量控制的功能 feature flag 动态引用 检查 process.env 条件分支
测试 mock 数据 仅在测试中使用 --include-tests 重扫
Service Worker / PWA 通过 public/ 直接引用 检查 public/ 目录配置

注意事项

  • 不要删除:正在开发的功能分支文件、feature flag 控制的实验性功能
  • 分批清理:先删高置信度文件,构建验证通过后再处理中置信度
  • 保留 git 历史:用 git rm 而非直接删除

与其他 Skill 配合

  • /simplify:清理后简化剩余代码
  • /refactor-safely:重构前先用本 skill 清理孤儿
  • /health:清理后检查整体代码质量
  • /review:清理 PR 提交前的安全审查
安全使用建议
This skill appears to do what it claims: scan your codebase and identify likely orphaned files and unused code, and it can generate a bash script to remove them. Before installing or running it: 1) Ensure you trust the agent's environment and that it has access only to the intended repository (the skill will read the whole project). 2) Review clawhub.json: it requests file_read, file_write, and bash — which lets it write scripts and propose git rm commands. 3) Never run generated deletion scripts blindly: create a new branch (as the skill itself suggests), inspect the script, and run tests/build (npm run build) before merging. 4) Make sure git and your build tools are available where the agent runs (SKILL.md assumes git/npm but they aren't listed as required binaries). 5) Avoid running this on repositories that contain sensitive secrets you don't want scanned or exposed. If you want extra caution, run the skill on a cloned copy of the repo or in CI with isolated permissions.
功能分析
Type: OpenClaw Skill Name: find-orphans Version: 1.0.0 The 'find-orphans' skill is a legitimate utility designed to identify and clean up unused code, components, and files in a project. It uses standard static analysis techniques (globbing and grep) to build dependency graphs and identify orphaned assets, providing a detailed report and a safe 'git rm' based cleanup script for the user to review. No indicators of data exfiltration, malicious execution, or prompt injection were found in SKILL.md or the configuration files.
能力评估
Purpose & Capability
Name/description (find orphaned files, unused components, dead code) align with the instructions (scan package.json, extract imports, compare referenced files, detect unused exports/functions, analyze routes/styles/assets). The declared permissions in clawhub.json (file_read, file_write, bash) match the intended actions (reading the repo, outputting reports, optionally creating a bash removal script).
Instruction Scope
SKILL.md limits actions to static analysis of project files and producing reports/scripts. However it explicitly proposes generating and removing files via a bash script using git rm; that is destructive by nature. The doc includes sensible safeguards (recommend creating a branch, reviewing the script, running build verification) but the generated commands must be reviewed by a human before execution. It also expects access to git/npm for verification steps, but these binaries are not declared in the metadata.
Install Mechanism
Instruction-only skill with no install spec or external downloads; no packages or remote code are fetched. This is low-risk from an install perspective.
Credentials
No environment variables, credentials, or external endpoints are requested. The skill works only with repository files and local project config, which is proportional to its purpose.
Persistence & Privilege
always is false and autonomous invocation is the platform default. The skill requests file_read/file_write/bash permissions (clawhub.json) which are reasonable for a tool that reads the repo and can emit cleanup scripts; there is no request to modify other skills or system-wide settings.
如何使用
  1. 确保已安装 OpenClaw(本地或 Docker 部署)
  2. 在对话框中输入安装命令:/install find-orphans
  3. 安装完成后,直接呼叫该 Skill 的名称或使用 /find-orphans 触发
  4. 根据 Skill 的参数说明提供必要输入,即可获得结构化输出
版本历史
v1.0.0
Initial release: Find orphaned files, unused components, and dead code in projects
元数据
Slug find-orphans
版本 1.0.0
许可证 MIT-0
累计安装 0
当前安装数 0
历史版本数 1
常见问题

Find Orphans 是什么?

Finds orphaned files, unused components, and dead code in projects. Use when 清理代码, 查找孤儿文件, 删除无用代码, cleanup, find unused, or removing legacy code. 它是一个面向 Claude Code / OpenClaw 的 AI Agent Skill 插件,目前累计下载 98 次。

如何安装 Find Orphans?

在 OpenClaw 或 Claude Code 对话框中运行命令「/install find-orphans」即可一键安装,无需额外配置。

Find Orphans 是免费的吗?

是的,Find Orphans 完全免费,采用 MIT-0 许可证,可自由下载、安装和使用。

Find Orphans 支持哪些平台?

Find Orphans 跨平台运行,可在任意部署了 OpenClaw / Claude Code 的环境中使用(cross-platform)。

谁开发了 Find Orphans?

由 wangzhiming(@wangzhiming1999)开发并维护,当前版本 v1.0.0。

💬 留言讨论