♻️ Refactor directory creation logic to use a new makeDirectoryIfNotExists function

This commit is contained in:
2026-04-07 23:31:44 +08:00
parent 08f91530d0
commit dc11b5c0ff

View File

@@ -909,9 +909,7 @@ export async function writeStringToFile(
} }
} else { } else {
const parentDirname = dirname(newFilename); const parentDirname = dirname(newFilename);
if (!await existsPath(parentDirname)) { await makeDirectoryIfNotExists(parentDirname);
await makeDirectory(parentDirname);
}
if (isDeno()) { if (isDeno()) {
await Deno.writeTextFile(newFilename, data); await Deno.writeTextFile(newFilename, data);
} else { } else {
@@ -936,6 +934,15 @@ export async function removePath(path: string): Promise<void> {
} }
} }
export async function makeDirectoryIfNotExists(
directory: string,
recursive?: boolean,
): Promise<void> {
if (!await existsPath(directory)) {
await makeDirectory(directory, recursive);
}
}
export async function makeDirectory( export async function makeDirectory(
directory: string, directory: string,
recursive?: boolean, recursive?: boolean,
@@ -1126,9 +1133,7 @@ export async function readWithLocalCache<T>(
cacheKeyHash.substring(0, 2), cacheKeyHash.substring(0, 2),
cacheKeyHash.substring(2, 4), cacheKeyHash.substring(2, 4),
); );
if (!await existsPath(cacheDir)) { await makeDirectoryIfNotExists(cacheDir, true);
await makeDirectory(cacheDir, true);
}
const cacheFile = joinPath(cacheDir, cacheKeyHash); const cacheFile = joinPath(cacheDir, cacheKeyHash);
log.debug(`key: ${cacheKey} --> ${cacheFile}`); log.debug(`key: ${cacheKey} --> ${cacheFile}`);
const cachedContent = await readFileToString(cacheFile); const cachedContent = await readFileToString(cacheFile);
@@ -1426,9 +1431,7 @@ export async function fetchFileWithCache(
} }
} }
if (!await existsPath(fileCacheDir)) { await makeDirectoryIfNotExists(fileCacheDir);
await makeDirectory(fileCacheDir);
}
try { try {
const fetchResponse = await fetchDataWithTimeout(url, { const fetchResponse = await fetchDataWithTimeout(url, {