feat: advance the plugin system and create example plugin

This commit is contained in:
Jo 2024-06-28 02:44:36 +02:00
parent 8eeddf4d25
commit 20cdba55f6
18 changed files with 88 additions and 57 deletions

1
.gitignore vendored
View file

@ -1,3 +1,4 @@
.devenv
.devenv.flake.nix
.idea
node_modules

View file

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DiscordProjectSettings">
<option name="show" value="ASK" />
<option name="show" value="PROJECT_FILES" />
<option name="description" value="" />
</component>
</project>

View file

@ -2,6 +2,5 @@
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
<mapping directory="$PROJECT_DIR$/engine" vcs="Git" />
</component>
</project>

View file

@ -16,6 +16,7 @@
"@nestjs/core": "^10.0.0",
"@nestjs/platform-express": "^10.0.0",
"@nestjs/typeorm": "^10.0.1",
"glob": "^10.4.2",
"mongodb": "^6.7.0",
"pg": "^8.11.3",
"typeorm": "^0.3.17"

View file

@ -1,12 +0,0 @@
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
@Get()
createUser() {
return this.appService.createUser();
}
}

View file

@ -1,8 +1,7 @@
import { Module } from '@nestjs/common';
import { Module, type Type } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { User } from './entities/user.entity';
import { PluginModule } from "./common/plugins/plugin.module";
const userForFeature = TypeOrmModule.forFeature([User]);
@ -10,18 +9,20 @@ const userForFeature = TypeOrmModule.forFeature([User]);
imports: [
TypeOrmModule.forRoot({
type: "mongodb",
host: "localhost",
host: "127.0.0.1",
port: 27017,
username: "homespace",
password: "homespace",
database: "homespace",
database: "test",
authSource: "admin",
synchronize: true,
logging: false,
entities: [User],
}),
userForFeature,
PluginModule.register()
],
controllers: [AppController],
providers: [AppService],
controllers: [],
providers: [],
})
export class AppModule {}

View file

@ -1,23 +0,0 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import type { Repository } from 'typeorm';
import { User } from './entities/user.entity';
@Injectable()
export class AppService {
constructor(
@InjectRepository(User)
private readonly userRepository: Repository<User>,
) {}
async createUser() {
const user = this.userRepository.create({
firstName: 'Johannes',
lastName: 'Reckers',
age: 18,
});
const entity = await this.userRepository.save(user);
return entity;
}
}

View file

@ -1,3 +1,5 @@
import {INestApplication} from "@nestjs/common";
export interface Plugin {
register(): void
register(app: INestApplication): void
}

View file

@ -0,0 +1,20 @@
import { glob } from 'glob';
import { join } from 'path';
export async function loadPlugins(): Promise<unknown> {
return new Promise(async (resolve, reject) => {
const files = await glob('homespace-shared/plugins/**/*.plugin.{ts,js}');
console.log(files)
const plugins = [];
for (const file of files) {
const filePath = join(process.cwd(), file);
const pluginModule = await import(filePath);
const pluginClass = Object.values(pluginModule)[0];
plugins.push(pluginClass);
}
resolve(plugins);
});
}

View file

@ -0,0 +1,25 @@
import { Module, DynamicModule, Global, type Type } from '@nestjs/common';
import { PluginService } from './plugin.service';
import { type Plugin } from './plugin.interface';
import { loadPlugins } from './plugin.loader';
@Global()
@Module({})
export class PluginModule {
static async register(): Promise<DynamicModule> {
const plugins = await loadPlugins();
return {
module: PluginModule,
providers: [
PluginService,
...plugins,
{
provide: 'PLUGIN_PROVIDERS',
useValue: plugins,
},
],
exports: [PluginService],
};
}
}

View file

@ -1,15 +1,18 @@
import { Injectable, OnModuleInit } from "@nestjs/common";
import {Injectable, OnModuleInit, Inject, type Type, INestApplication} from '@nestjs/common';
import { type Plugin } from './plugin.interface';
@Injectable()
export class PluginService implements OnModuleInit {
private plugins: Plugin[] = [];
export class PluginService {
private plugins: Type<Plugin>[] = [];
public registerPlugin(plugin: Plugin) {
this.plugins.push(plugin);
constructor(@Inject('PLUGIN_PROVIDERS') private readonly pluginProviders: Type<Plugin>[]) {
this.plugins = pluginProviders;
}
onModuleInit() {
this.plugins.forEach((plugin) => plugin.register());
initializePlugins(app: INestApplication) {
this.plugins.forEach(plugin => {
const pluginInstance = new plugin();
pluginInstance.register(app);
});
}
}

View file

@ -1,12 +1,12 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { PluginService } from './common/plugins/plugin.service';
import { PluginService } from "./common/plugins/plugin.service";
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const pluginService = app.get(PluginService);
pluginService.onModuleInit(); // Initialize plugins
pluginService.initializePlugins(app);
await app.listen(4300);
}

7
apps/shared/package.json Normal file
View file

@ -0,0 +1,7 @@
{
"name": "homespace-shared",
"version": "0.0.1",
"workspaces": [
"plugins/*"
]
}

View file

@ -0,0 +1,8 @@
// Todo: create example plugin
import { type Plugin } from "homespace-backend/src/common/plugins/plugin.interface";
export class ExamplePlugin implements Plugin {
register() {
console.log("plugin registered: ExamplePlugin");
}
}

View file

@ -1,5 +1,5 @@
{
"name": "blmedia-example",
"name": "example-plugin",
"version": "0.0.1",
"workspaces": [
"apps/*"

BIN
bun.lockb

Binary file not shown.

View file

@ -1 +0,0 @@
// Todo: create example plugin