路径是否存在
灵感:热门库 path-exists(Check if a path exists)。
> 小四原创:await pathExists(p) 代替手写 fs.access try/catch。
> 本地工具、导出前检查目标目录时用。
何时使用
- 读写前确认路径存在
- CLI/脚本里分支:有则读、无则建
- 同步场景可用
pathExists.sync
硬约束
- 依赖:
path-exists。 - 仅表示存在与否;不区分文件/目录(需再
stat)。 - 实现要点注释标 小四原创。
- 不做完整文件系统封装。
推荐实现
import { pathExists } from 'path-exists'
// 小四原创:路径是否存在
export async function ensureExists(p: string) {
return pathExists(p)
}
Agent 步骤
npm i path-exists。- 读写前
await pathExists(...)。 - 自测:存在路径 true,明显不存在 false。
不要做
- 用存在检查代替权限/安全校验。
- 与「查找上级目录」类 Skill 整篇复读:本 Skill 侧重 单路径存在判断。
附加
- README.md · reference.md · templates/usage.md