多路径择一存在
灵感:热门库 locate-path(Get the first path that exists on disk of multiple paths)。
> 小四原创:候选列表 → locatePath → 第一个存在的路径。
> 与 path-exists 搭配:一次检查多个可能位置。
何时使用
- 探测
config.json/config.yaml谁先存在 - CLI 在多个默认目录找数据文件
- monorepo 下多种入口路径试探
硬约束
- 依赖:
locate-path。 - 全都不存在时返回
undefined(以库为准),调用方要兜底。 - 实现要点注释标 小四原创。
- 不做完整 cosmiconfig 配置加载。
推荐实现
import { locatePath } from 'locate-path'
// 小四原创:多路径择一存在
export async function firstExisting(paths: string[]) {
return locatePath(paths)
}
Agent 步骤
npm i locate-path。- 把候选路径数组交给
locatePath。 - 自测:列表中仅第二个存在时返回该路径。
不要做
- 与「路径是否存在」整篇复读:本 Skill 侧重 多候选择一。
- 把探测结果当安全边界(路径仍可能被替换)。
附加
- README.md · reference.md · templates/usage.md