Skip to content

Commit

Permalink
fix: do not throw error when trying to prune missing directory (#7257)
Browse files Browse the repository at this point in the history
  • Loading branch information
nflaig authored Nov 29, 2024
1 parent 48dea55 commit aaac34a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/cli/src/util/pruneOldFilesInDir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions packages/cli/test/unit/util/pruneOldFilesInDir.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down

0 comments on commit aaac34a

Please sign in to comment.