♻️ 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 {
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<void> {
}
}
export async function makeDirectoryIfNotExists(
directory: string,
recursive?: boolean,
): Promise<void> {
if (!await existsPath(directory)) {
await makeDirectory(directory, recursive);
}
}
export async function makeDirectory(
directory: string,
recursive?: boolean,
@@ -1126,9 +1133,7 @@ export async function readWithLocalCache<T>(
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, {