mirror of
https://github.com/beekeeper-studio/beekeeper-studio.git
synced 2026-03-13 10:12:54 +08:00
51 lines
1.2 KiB
TypeScript
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 });
|
|
}
|
|
}
|