Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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