mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 18:04:59 +00:00
Memory: share ENOENT helpers
This commit is contained in:
committed by
Vignesh
parent
14a3af212d
commit
5542a43623
31
src/memory/fs-utils.ts
Normal file
31
src/memory/fs-utils.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import type { Stats } from "node:fs";
|
||||
import fs from "node:fs/promises";
|
||||
|
||||
export type RegularFileStatResult = { missing: true } | { missing: false; stat: Stats };
|
||||
|
||||
export function isFileMissingError(
|
||||
err: unknown,
|
||||
): err is NodeJS.ErrnoException & { code: "ENOENT" } {
|
||||
return Boolean(
|
||||
err &&
|
||||
typeof err === "object" &&
|
||||
"code" in err &&
|
||||
(err as Partial<NodeJS.ErrnoException>).code === "ENOENT",
|
||||
);
|
||||
}
|
||||
|
||||
export async function statRegularFile(absPath: string): Promise<RegularFileStatResult> {
|
||||
let stat: Stats;
|
||||
try {
|
||||
stat = await fs.lstat(absPath);
|
||||
} catch (err) {
|
||||
if (isFileMissingError(err)) {
|
||||
return { missing: true };
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
if (stat.isSymbolicLink() || !stat.isFile()) {
|
||||
throw new Error("path required");
|
||||
}
|
||||
return { missing: false, stat };
|
||||
}
|
||||
Reference in New Issue
Block a user