diff --git a/apps/main/src/init.ts b/apps/main/src/init.ts index 50d9e6a818..3c3bff55cf 100644 --- a/apps/main/src/init.ts +++ b/apps/main/src/init.ts @@ -9,7 +9,7 @@ import { app, nativeTheme, Notification, shell } from "electron" import contextMenu from "electron-context-menu" import { getIconPath } from "./helper" -import { clearCacheCronJob } from "./lib/cleaner" +import { checkAndCleanCodeCache, clearCacheCronJob } from "./lib/cleaner" import { t } from "./lib/i18n" import { store } from "./lib/store" import { updateNotificationsToken } from "./lib/user" @@ -61,6 +61,7 @@ export const initializeAppStage1 = () => { registerPushNotifications() clearCacheCronJob() + checkAndCleanCodeCache() } let contextMenuDisposer: () => void diff --git a/apps/main/src/lib/cleaner.ts b/apps/main/src/lib/cleaner.ts index d22a5a8d81..eb0d418c48 100644 --- a/apps/main/src/lib/cleaner.ts +++ b/apps/main/src/lib/cleaner.ts @@ -144,3 +144,25 @@ export const clearCacheCronJob = () => { timer = clearInterval(timer) } } + +export const checkAndCleanCodeCache = async () => { + const cachePath = path.join(app.getPath("userData"), "Code Cache") + + const size = await fastFolderSizeAsync(cachePath).catch((error) => { + logger.error(error) + }) + + if (!size) return + + const threshold = 1024 * 1024 * 100 // 100MB + if (size > threshold) { + await fsp + .rm(cachePath, { force: true, recursive: true }) + .then(() => { + logger.info(`Cleaned ${size} bytes code cache`) + }) + .catch((error) => { + logger.error(`clean code cache failed: ${error.message}`) + }) + } +}