Skip to content

gvt clean — 清理前端依赖

扫描当前项目中的前端模块,删除 node_modules 目录,可选清理包管理器缓存。支持 npm / pnpm / yarn 三种管理器,使用官方命令清理缓存。

用法

bash
gvt clean                              # 自动扫描并清理所有 node_modules
gvt clean --cache                      # 同时清理包管理器缓存
gvt clean --dry-run                    # 仅预览,不实际删除
gvt clean --force                      # 跳过确认提示
gvt clean --dir frontend/portal        # 只清理指定目录
gvt clean --dir portal --dir admin     # 清理多个目录

参数

参数简写说明
--cache-c同时清理包管理器缓存(npm cache / pnpm store / yarn cache)
--dry-run-n仅预览要清理的内容,不实际删除
--force-f跳过确认提示
--dir-d指定目标目录(可重复使用,相对于项目根)

扫描策略

gvt clean 采用 config-first 策略查找前端模块:

  1. 优先读 .gvt/hook/ 配置 — 若项目已有钩子配置,直接读取注册的前端模块列表,零 IO 遍历
  2. 回退文件系统扫描 — 无配置时扫描项目根的直接子目录(depth=1),查找包含 package.json 的目录

包管理器通过 lockfile 自动检测:pnpm-lock.yaml → pnpm、yarn.lock → yarn、package-lock.json → npm。

缓存清理

--cache 参数启用后,对每个模块调用对应包管理器的官方清理命令:

包管理器缓存命令
npmnpm cache clean --force
pnpmpnpm store prune
yarnyarn cache clean

性能优化

针对 Windows 平台进行了专门优化:

  • node_modules 删除:Windows 使用 cmd /c rmdir /s /q(原生 NTFS 批量删除),比 os.RemoveAll 快 10–100 倍,且原生支持 260 字符以上长路径
  • 扫描:depth=1 限制,跳过 .git/vendor/dist/ 等无关目录
  • 配置优先:有 .gvt/hook/ 时完全跳过文件系统扫描

适用场景

  • 项目依赖冲突,需要完全重装
  • 切换包管理器(如 npm → pnpm)后清理残留
  • CI 环境中构建前确保干净的 node_modules
  • 磁盘空间不足时批量清理多个前端模块

基于 MIT 协议开源