From dc11b5c0ff35669a4d4f9bbf42d284e84fd50dd0 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Tue, 7 Apr 2026 23:31:44 +0800 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Refactor=20directory=20cre?= =?UTF-8?q?ation=20logic=20to=20use=20a=20new=20`makeDirectoryIfNotExists`?= =?UTF-8?q?=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libraries/deno-commons-mod.ts | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/libraries/deno-commons-mod.ts b/libraries/deno-commons-mod.ts index b74697c..e9a052f 100644 --- a/libraries/deno-commons-mod.ts +++ b/libraries/deno-commons-mod.ts @@ -909,9 +909,7 @@ export async function writeStringToFile( } } else { const parentDirname = dirname(newFilename); - if (!await existsPath(parentDirname)) { - await makeDirectory(parentDirname); - } + await makeDirectoryIfNotExists(parentDirname); if (isDeno()) { await Deno.writeTextFile(newFilename, data); } else { @@ -936,6 +934,15 @@ export async function removePath(path: string): Promise { } } +export async function makeDirectoryIfNotExists( + directory: string, + recursive?: boolean, +): Promise { + if (!await existsPath(directory)) { + await makeDirectory(directory, recursive); + } +} + export async function makeDirectory( directory: string, recursive?: boolean, @@ -1126,9 +1133,7 @@ export async function readWithLocalCache( cacheKeyHash.substring(0, 2), cacheKeyHash.substring(2, 4), ); - if (!await existsPath(cacheDir)) { - await makeDirectory(cacheDir, true); - } + await makeDirectoryIfNotExists(cacheDir, true); const cacheFile = joinPath(cacheDir, cacheKeyHash); log.debug(`key: ${cacheKey} --> ${cacheFile}`); const cachedContent = await readFileToString(cacheFile); @@ -1426,9 +1431,7 @@ export async function fetchFileWithCache( } } - if (!await existsPath(fileCacheDir)) { - await makeDirectory(fileCacheDir); - } + await makeDirectoryIfNotExists(fileCacheDir); try { const fetchResponse = await fetchDataWithTimeout(url, {