Autowire Module
This file defines the Autowire
class, which is responsible for scanning, registering, and initializing modules and plugins in the @sodacore/core
library. It automates the discovery and setup of application modules based on metadata, supporting plugin verification, module ordering, and lifecycle management.
Features
- Module Scanning: Automatically scans the source directory for
.ts
and.js
files to discover modules. - Plugin Verification: Checks plugin dependencies and ensures all required plugins are present before initialization.
- Module Registration: Registers modules and plugins based on their metadata.
- Ordering: Reorders modules according to their
order
metadata to control initialization sequence. - Initialization: Instantiates and initializes modules, calling their
onInit
method if present. - Registry Integration: Registers modules and their instances in the global
Registry
for easy access.
Usage
import Autowire from './module/autowire';
import Application from './module/application';
const autowire = new Autowire(config, application);
await autowire.init();
API
Constructor
constructor(
config: IConfig,
private application: Application,
)
- config: The application configuration object.
- application: The main
Application
instance.
Methods
public async init(): Promise<void>
Initializes the autowire module:
- Verifies plugins and their dependencies.
- Scans for module files.
- Imports and registers modules.
- Reorders modules by priority.
- Loads and initializes modules.
public async verify(): Promise<void>
Checks that all plugin dependencies are satisfied. Logs errors and exits the process if any dependencies are missing.
public async install(plugin: IPlugin): Promise<void>
Installs a plugin by calling its install
method (if present) and adds it to the plugins list.
public async register(module: any): Promise<void>
Registers a module class (not an instance) for later initialization.
Private Methods
private async scanFiles(): Promise<string[]>
Scans the source directory (or custom base path) for .ts
and .js
files, excluding the currently executing file.
private async import(files: string[]): Promise<void>
Imports each file, checks for exported classes with autowire metadata, and registers them as modules.
private async reorder(): Promise<void>
Sorts the registered modules based on their order
metadata (default: 50).
private async load(): Promise<void>
Instantiates each module, registers it in the Registry
, and calls its onInit
method if available. For modules of type 'thread'
, only registers the class without instantiation.
Notes
- The autowire system relies on metadata set by decorators (e.g.,
@Service
,@Task
,@Controller
, etc.). - Plugins must implement the
IPlugin
interface and can specify dependencies. - The module scanning uses Bun's
Glob
utility for file discovery. - The
Registry
is used for global module and instance registration. - The autowire process ensures modules are initialized in the correct order and with all dependencies satisfied.