Files
beekeeper-studio/apps/studio/tests/integration/plugins/utils/fileManager.ts
Mohammad Azmi 3d062dc72a chores
2025-08-15 22:08:42 +07:00

51 lines
1.2 KiB
TypeScript

import PluginFileManager from "@/services/plugin/PluginFileManager";
import fs from "fs";
import path from "path";
import { uuidv4 } from "@/lib/uuid";
import { tmpdir } from "os";
/**
* Create a file manager for plugin manager.
*
* Usage:
*
* ```js
* import { createFileManager, cleanFileManager } from "./fileManager";
*
* it("can do it", async () => {
* const fileManager = createFileManager();
*
* const manager = new PluginManager({ fileManager });
* await manager.initialize();
*
* // tests here ...
*
* cleanFileManager(fileManager);
* })
* ```
*/
export function createFileManager(): PluginFileManager {
return new PluginFileManager({
downloadDirectory: tmpDir(),
pluginsDirectory: tmpDir(),
});
}
/** Erase plugins data. */
export function cleanFileManager(manager: PluginFileManager) {
cleanTmpDir(manager.options.downloadDirectory);
cleanTmpDir(manager.options.pluginsDirectory);
}
function tmpDir() {
const randomDir = path.join(tmpdir(), uuidv4());
fs.mkdirSync(randomDir, { recursive: true });
return randomDir;
}
function cleanTmpDir(dir?: string) {
if (dir && fs.existsSync(dir)) {
fs.rmSync(dir, { recursive: true, force: true });
}
}