From 3524077952735a0d6fab88a4bc2f66d53c1e9f67 Mon Sep 17 00:00:00 2001 From: Lonny Wong Date: Fri, 6 Oct 2023 20:35:45 +0800 Subject: [PATCH] fs.exists deprecated --- src/nodefs.ts | 4 ++-- test/nodefs.test.ts | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/nodefs.ts b/src/nodefs.ts index 3896952..fbf7e01 100644 --- a/src/nodefs.ts +++ b/src/nodefs.ts @@ -35,8 +35,8 @@ function promisify(fs: any, funcs: string[]) { promisify(fs, ["stat", "access", "mkdir", "readdir", "close", "open", "realpath", "write"]); -async function fsExists(path: string) { - return new Promise((resolve) => fs.exists(path, (exists: boolean) => resolve(exists))); +async function fsExists(path: string): Promise { + return new Promise((resolve) => fs.access(path, (err: Error) => resolve(!err))); } async function fsRead( diff --git a/test/nodefs.test.ts b/test/nodefs.test.ts index f99c728..59ae5fc 100644 --- a/test/nodefs.test.ts +++ b/test/nodefs.test.ts @@ -145,10 +145,10 @@ test("open save file success", async () => { tfr.closeFile(); expect(fs.readFileSync(path.join(tmpDir, "save.txt")).toString()).toBe("test file content"); - const exists = fs.exists; - fs.exists = (_path: string, callback: Function) => callback(true); + const access = fs.access; + fs.access = (_path: string, mode: Function, callback: Function) => (callback ? callback(null) : mode(null)); await expect(openSaveFile(saveParam, "save.txt", false, false)).rejects.toThrowError("Fail to assign new file name"); - fs.exists = exists; + fs.access = access; }); test("open save file error", async () => {