Skip to content

Commit aaac34a

Browse files
authored
fix: do not throw error when trying to prune missing directory (#7257)
1 parent 48dea55 commit aaac34a

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

packages/cli/src/util/pruneOldFilesInDir.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import fs from "node:fs";
22
import path from "node:path";
33

44
export function pruneOldFilesInDir(dirpath: string, maxAgeMs: number): number {
5+
if (!fs.existsSync(dirpath)) {
6+
return 0; // Nothing to prune
7+
}
8+
59
let deletedFileCount = 0;
610
for (const entryName of fs.readdirSync(dirpath)) {
711
const entryPath = path.join(dirpath, entryName);

packages/cli/test/unit/util/pruneOldFilesInDir.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ describe("pruneOldFilesInDir", () => {
5555
expect(fs.existsSync(emptyDir)).toBe(false);
5656
});
5757

58+
it("should handle missing directories", () => {
59+
expect(() => pruneOldFilesInDir(path.join(dataDir, "does-not-exist"), DAYS_TO_MS)).not.toThrowError();
60+
});
61+
5862
function createFileWithAge(path: string, ageInDays: number): void {
5963
// Create a new empty file
6064
fs.closeSync(fs.openSync(path, "w"));

0 commit comments

Comments
 (0)