Skip to content

Commit 18b126f

Browse files
authored
fix: remove autoremoval functionality (#79)
Since there is an option to delete a diagram within UI, there is no reason of having autoremoval now
1 parent 33e5562 commit 18b126f

File tree

4 files changed

+2
-158
lines changed

4 files changed

+2
-158
lines changed

src/constants.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,6 @@ export const UNIX_SYSTEM_PATHS = [
103103

104104
export const WINDOWS_SYSTEM_PATHS = ["C:\\Windows", "C:\\Program Files"] as const;
105105

106-
// ===== Timeouts and Intervals =====
107-
export const TIMEOUTS = {
108-
CLEANUP_MAX_AGE_MS: 7 * 24 * 60 * 60 * 1000, // 7 days in milliseconds
109-
} as const;
110-
111106
// ===== WebSocket Messages =====
112107
export const WS_MESSAGES = {
113108
RELOAD: "reload",

src/file-utils.ts

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
import { readdir, unlink, stat, mkdir, readFile, writeFile, rmdir } from "fs/promises";
1+
import { readdir, unlink, readFile, writeFile, rmdir } from "fs/promises";
22
import { join, resolve } from "path";
33
import { tmpdir } from "os";
44
import {
55
APP_NAME,
66
PREVIEW_ID_REGEX,
77
UNIX_SYSTEM_PATHS,
88
WINDOWS_SYSTEM_PATHS,
9-
TIMEOUTS,
109
FILE_NAMES,
1110
DIR_NAMES,
1211
} from "./constants.js";
1312
import type { DiagramOptions } from "./types.js";
14-
import { webLogger } from "./logger.js";
1513

1614
export function getConfigDir(): string {
1715
const xdg = process.env.XDG_CONFIG_HOME;
@@ -152,46 +150,3 @@ export function getOpenCommand(): string {
152150
? "start"
153151
: "xdg-open";
154152
}
155-
156-
export async function cleanupOldDiagrams(
157-
maxAgeMs: number = TIMEOUTS.CLEANUP_MAX_AGE_MS
158-
): Promise<number> {
159-
try {
160-
const liveDir = getLiveDir();
161-
await mkdir(liveDir, { recursive: true });
162-
163-
const entries = await readdir(liveDir, { withFileTypes: true });
164-
const now = Date.now();
165-
let cleanedCount = 0;
166-
167-
for (const entry of entries) {
168-
if (entry.isDirectory()) {
169-
const dirPath = join(liveDir, entry.name);
170-
const sourcePath = join(dirPath, FILE_NAMES.DIAGRAM_SOURCE);
171-
172-
try {
173-
const stats = await stat(sourcePath);
174-
const age = now - stats.mtimeMs;
175-
176-
if (age > maxAgeMs) {
177-
await deleteDiagramDirectory(dirPath);
178-
webLogger.info(`Cleaned up old diagram: ${entry.name}`);
179-
cleanedCount++;
180-
}
181-
} catch (error) {
182-
// Log but continue if individual diagram cleanup fails
183-
webLogger.debug(`Skipping diagram cleanup: ${entry.name}`, {
184-
error: error instanceof Error ? error.message : String(error),
185-
});
186-
}
187-
}
188-
}
189-
190-
return cleanedCount;
191-
} catch (error) {
192-
webLogger.warn("Diagram cleanup failed", {
193-
error: error instanceof Error ? error.message : String(error),
194-
});
195-
return 0;
196-
}
197-
}

src/index.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
import { readFile } from "fs/promises";
1111
import { join, dirname } from "path";
1212
import { fileURLToPath } from "url";
13-
import { cleanupOldDiagrams } from "./file-utils.js";
1413
import { handleMermaidPreview, handleMermaidSave } from "./handlers.js";
1514
import { mcpLogger } from "./logger.js";
1615

@@ -170,11 +169,6 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
170169
async function main() {
171170
mcpLogger.info("MCP Server starting", { version: VERSION });
172171

173-
const cleanedCount = await cleanupOldDiagrams();
174-
if (cleanedCount > 0) {
175-
mcpLogger.info(`Cleaned up ${cleanedCount} old diagrams`);
176-
}
177-
178172
const transport = new StdioServerTransport();
179173
await server.connect(transport);
180174
mcpLogger.info("MCP Server connected via stdio");

test/file-utils.test.ts

Lines changed: 1 addition & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ import {
88
saveDiagramSource,
99
loadDiagramSource,
1010
loadDiagramOptions,
11-
cleanupOldDiagrams,
1211
getConfigDir,
1312
validateSavePath,
1413
} from "../src/file-utils.js";
15-
import { writeFile, unlink, mkdir, utimes, rmdir, readdir, mkdtemp } from "fs/promises";
14+
import { writeFile, unlink, mkdir, rmdir, readdir, mkdtemp } from "fs/promises";
1615
import { tmpdir } from "os";
1716
import { join } from "path";
1817

@@ -259,105 +258,6 @@ describe("File Utilities", () => {
259258
});
260259
});
261260

262-
describe("cleanupOldDiagrams", () => {
263-
let tempDir: string;
264-
let testFiles: string[];
265-
266-
beforeEach(async () => {
267-
// Use a test-specific directory
268-
tempDir = join(tmpdir(), "claude-mermaid-test-cleanup", Date.now().toString());
269-
await mkdir(tempDir, { recursive: true });
270-
testFiles = [];
271-
});
272-
273-
afterEach(async () => {
274-
// Clean up test files
275-
for (const file of testFiles) {
276-
try {
277-
await unlink(file);
278-
} catch {
279-
// Ignore if file doesn't exist
280-
}
281-
}
282-
});
283-
284-
it("should return 0 when no files to clean", async () => {
285-
// Test with very short max age to simulate old files
286-
const count = await cleanupOldDiagrams(0);
287-
expect(count).toBeGreaterThanOrEqual(0);
288-
});
289-
290-
it("should clean up files older than max age", async () => {
291-
const oldFile = join(tempDir, "old-diagram.svg");
292-
const newFile = join(tempDir, "new-diagram.svg");
293-
294-
// Create old file
295-
await writeFile(oldFile, "<svg>old</svg>", "utf-8");
296-
testFiles.push(oldFile);
297-
298-
// Set file modification time to 8 days ago
299-
const eightDaysAgo = new Date(Date.now() - 8 * 24 * 60 * 60 * 1000);
300-
await utimes(oldFile, eightDaysAgo, eightDaysAgo);
301-
302-
// Create new file
303-
await writeFile(newFile, "<svg>new</svg>", "utf-8");
304-
testFiles.push(newFile);
305-
306-
// This test verifies the cleanup logic works conceptually
307-
// In practice, it cleans the actual live directory, not our test dir
308-
const maxAge = 7 * 24 * 60 * 60 * 1000; // 7 days
309-
expect(maxAge).toBe(604800000);
310-
});
311-
312-
it("should clean up directories with all their files", async () => {
313-
const testPreviewId = "test-cleanup-dir";
314-
const testDir = getPreviewDir(testPreviewId);
315-
316-
await mkdir(testDir, { recursive: true });
317-
await writeFile(join(testDir, "diagram.svg"), "<svg>test</svg>", "utf-8");
318-
await writeFile(join(testDir, "diagram.mmd"), "graph TD; A-->B", "utf-8");
319-
await writeFile(join(testDir, "options.json"), "{}", "utf-8");
320-
321-
const files = await readdir(testDir);
322-
expect(files.length).toBeGreaterThan(0);
323-
324-
// Cleanup test directory
325-
for (const file of files) {
326-
await unlink(join(testDir, file));
327-
}
328-
await rmdir(testDir);
329-
});
330-
331-
it("should handle different max age values", () => {
332-
const oneDayMs = 24 * 60 * 60 * 1000;
333-
const sevenDaysMs = 7 * 24 * 60 * 60 * 1000;
334-
const thirtyDaysMs = 30 * 24 * 60 * 60 * 1000;
335-
336-
expect(oneDayMs).toBe(86400000);
337-
expect(sevenDaysMs).toBe(604800000);
338-
expect(thirtyDaysMs).toBe(2592000000);
339-
});
340-
341-
it("should use 7 days as default max age", () => {
342-
const defaultMaxAge = 7 * 24 * 60 * 60 * 1000;
343-
expect(defaultMaxAge).toBe(604800000);
344-
});
345-
346-
it("should not throw errors when directory does not exist", async () => {
347-
// The function creates the directory if it doesn't exist
348-
await expect(cleanupOldDiagrams()).resolves.not.toThrow();
349-
});
350-
351-
it("should calculate file age correctly", () => {
352-
const now = Date.now();
353-
const eightDaysAgo = now - 8 * 24 * 60 * 60 * 1000;
354-
const age = now - eightDaysAgo;
355-
const sevenDaysMs = 7 * 24 * 60 * 60 * 1000;
356-
357-
expect(age).toBeGreaterThan(sevenDaysMs);
358-
});
359-
});
360-
361261
describe("validateSavePath", () => {
362262
it("should allow valid relative paths", () => {
363263
expect(() => validateSavePath("./diagrams/test.svg")).not.toThrow();

0 commit comments

Comments
 (0)