|
| 1 | +import * as File from "fs"; |
| 2 | +import * as Path from "path"; |
| 3 | + |
| 4 | +import { LevilaminaPluginNotFoundError } from "./LevilaminaPluginNotFoundError"; |
| 5 | +import { PluginPackage } from "../packager/PluginPackage"; |
| 6 | + |
| 7 | + |
| 8 | +export class LevilaminaServer { |
| 9 | + private static deleteDirectory(basePath: string): void { |
| 10 | + if (File.existsSync(basePath)) { |
| 11 | + File.readdirSync(basePath).forEach((file: string): void => { |
| 12 | + const currentPath: string = Path.join(basePath, file); |
| 13 | + |
| 14 | + if (File.lstatSync(currentPath).isDirectory()) { |
| 15 | + LevilaminaServer.deleteDirectory(currentPath); |
| 16 | + } else { |
| 17 | + File.unlinkSync(currentPath); |
| 18 | + } |
| 19 | + }); |
| 20 | + |
| 21 | + File.rmdirSync(basePath); |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + private readonly pluginDirectory: string; |
| 26 | + |
| 27 | + public constructor(path: string) { |
| 28 | + this.pluginDirectory = Path.join(path, "plugins"); |
| 29 | + } |
| 30 | + |
| 31 | + public removePlugin(pluginName: string): void { |
| 32 | + const pluginPath: string = Path.join(this.pluginDirectory, pluginName); |
| 33 | + |
| 34 | + if (File.existsSync(pluginPath)) { |
| 35 | + LevilaminaServer.deleteDirectory(pluginPath); |
| 36 | + } else { |
| 37 | + throw new LevilaminaPluginNotFoundError(pluginName); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + public async importPlugin(pluginPackage: PluginPackage): Promise<string> { |
| 42 | + await pluginPackage.expand(this.pluginDirectory); |
| 43 | + |
| 44 | + return `Plugin ${pluginPackage.getName()} has been imported to ${this.pluginDirectory}.`; |
| 45 | + } |
| 46 | +} |
0 commit comments