-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
166 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,14 @@ | ||
.idea/ | ||
!.idea/modules.xml | ||
!.idea/vcs.xml | ||
logs/ | ||
|
||
node_modules/ | ||
dist/ | ||
|
||
logs/* | ||
!logs/.keep | ||
plugins/* | ||
!plugins/.keep | ||
|
||
.cert.pfx | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import type { Client } from "logos/client"; | ||
import type pino from "pino"; | ||
|
||
interface Plugin { | ||
readonly filename: string; | ||
readonly load: (client: Client) => Promise<void>; | ||
readonly unload?: (client: Client) => Promise<void>; | ||
} | ||
|
||
class PluginStore { | ||
readonly log: pino.Logger; | ||
|
||
readonly #client: Client; | ||
readonly #plugins: Plugin[]; | ||
|
||
constructor(client: Client) { | ||
this.log = client.log.child({ name: "PluginStore" }); | ||
|
||
this.#client = client; | ||
this.#plugins = []; | ||
} | ||
|
||
async setup(): Promise<void> { | ||
this.log.info("Looking for plugins to load..."); | ||
|
||
const filenames = await PluginStore.#getPluginFilenames(); | ||
if (filenames.length === 0) { | ||
this.log.info("There were no plugins to load."); | ||
return; | ||
} | ||
|
||
this.log.info(`Found ${filenames.length} plugins. Loading...`); | ||
|
||
await Promise.all(filenames.map((filename) => this.#loadPlugin(filename))); | ||
|
||
this.log.info(`Loaded ${filenames.length} plugin(s).`); | ||
} | ||
|
||
async teardown(): Promise<void> { | ||
this.log.info("Tearing down plugin store..."); | ||
|
||
if (this.#plugins.length > 0) { | ||
this.log.info(`There are ${this.#plugins.length} plugin(s) to unload. Unloading...`); | ||
|
||
await Promise.all(this.#plugins.map((plugin) => this.#unloadPlugin(plugin))); | ||
|
||
this.log.info(`Unloaded ${this.#plugins.length} plugin(s).`); | ||
} else { | ||
this.log.info("No plugins to unload."); | ||
} | ||
|
||
this.log.info("Plugin store torn down."); | ||
} | ||
|
||
static async #getPluginFilenames(): Promise<string[]> { | ||
return Array.fromAsync(new Bun.Glob("*.ts").scan(constants.directories.plugins)); | ||
} | ||
|
||
async #loadPlugin(filename: string): Promise<void> { | ||
const module = await import(`../../../${constants.directories.plugins}/${filename}`); | ||
const plugin = { filename, ...module }; | ||
|
||
this.#plugins.push(plugin); | ||
|
||
this.log.info(`Loading plugin ${plugin.filename}...`); | ||
|
||
await plugin.load(this.#client); | ||
|
||
this.log.info(`Loaded plugin ${plugin.filename}.`); | ||
} | ||
|
||
async #unloadPlugin(plugin: Plugin): Promise<void> { | ||
this.log.info(`Unloading plugin ${plugin.filename}...`); | ||
|
||
await plugin.unload?.(this.#client); | ||
|
||
this.log.info(`Unloaded plugin ${plugin.filename}.`); | ||
} | ||
} | ||
|
||
export { PluginStore }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters