diff --git a/packages/cli/src/util/pruneOldFilesInDir.ts b/packages/cli/src/util/pruneOldFilesInDir.ts index 3d5cc2b60bcb..8b8dfe754059 100644 --- a/packages/cli/src/util/pruneOldFilesInDir.ts +++ b/packages/cli/src/util/pruneOldFilesInDir.ts @@ -2,6 +2,10 @@ import fs from "node:fs"; import path from "node:path"; export function pruneOldFilesInDir(dirpath: string, maxAgeMs: number): number { + if (!fs.existsSync(dirpath)) { + return 0; // Nothing to prune + } + let deletedFileCount = 0; for (const entryName of fs.readdirSync(dirpath)) { const entryPath = path.join(dirpath, entryName); diff --git a/packages/cli/test/unit/util/pruneOldFilesInDir.test.ts b/packages/cli/test/unit/util/pruneOldFilesInDir.test.ts index a50c59547688..ee5532a358ee 100644 --- a/packages/cli/test/unit/util/pruneOldFilesInDir.test.ts +++ b/packages/cli/test/unit/util/pruneOldFilesInDir.test.ts @@ -55,6 +55,10 @@ describe("pruneOldFilesInDir", () => { expect(fs.existsSync(emptyDir)).toBe(false); }); + it("should handle missing directories", () => { + expect(() => pruneOldFilesInDir(path.join(dataDir, "does-not-exist"), DAYS_TO_MS)).not.toThrowError(); + }); + function createFileWithAge(path: string, ageInDays: number): void { // Create a new empty file fs.closeSync(fs.openSync(path, "w"));