2025-06-11 15:33:09 -04:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright 2025 Google LLC
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
2025-08-25 22:11:27 +02:00
|
|
|
import { promises as fs } from 'node:fs';
|
|
|
|
|
import { join } from 'node:path';
|
2025-08-20 10:55:47 +09:00
|
|
|
import { Storage } from '@google/gemini-cli-core';
|
2025-06-11 15:33:09 -04:00
|
|
|
|
2025-08-08 15:38:30 +00:00
|
|
|
const cleanupFunctions: Array<(() => void) | (() => Promise<void>)> = [];
|
2025-07-12 15:42:47 -07:00
|
|
|
|
2025-08-08 15:38:30 +00:00
|
|
|
export function registerCleanup(fn: (() => void) | (() => Promise<void>)) {
|
2025-07-12 15:42:47 -07:00
|
|
|
cleanupFunctions.push(fn);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-08 15:38:30 +00:00
|
|
|
export async function runExitCleanup() {
|
2025-07-12 15:42:47 -07:00
|
|
|
for (const fn of cleanupFunctions) {
|
|
|
|
|
try {
|
2025-08-08 15:38:30 +00:00
|
|
|
await fn();
|
2025-07-12 15:42:47 -07:00
|
|
|
} catch (_) {
|
|
|
|
|
// Ignore errors during cleanup.
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
cleanupFunctions.length = 0; // Clear the array
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-11 15:33:09 -04:00
|
|
|
export async function cleanupCheckpoints() {
|
2025-08-20 10:55:47 +09:00
|
|
|
const storage = new Storage(process.cwd());
|
|
|
|
|
const tempDir = storage.getProjectTempDir();
|
2025-06-20 00:39:15 -04:00
|
|
|
const checkpointsDir = join(tempDir, 'checkpoints');
|
2025-06-11 15:33:09 -04:00
|
|
|
try {
|
|
|
|
|
await fs.rm(checkpointsDir, { recursive: true, force: true });
|
|
|
|
|
} catch {
|
|
|
|
|
// Ignore errors if the directory doesn't exist or fails to delete.
|
|
|
|
|
}
|
|
|
|
|
}
|