@yumerijs/loader 2.1.1 → 2.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,10 +1,11 @@
1
- import { Core, Config, Logger, Context, PluginStatus } from '@yumerijs/core';
1
+ import { Core, Config, Logger, Context, PluginStatus, Schema } from '@yumerijs/core';
2
2
  interface Plugin {
3
3
  apply: (ctx: Context, config: Config) => Promise<void>;
4
4
  disable: (ctx: Context) => Promise<void>;
5
5
  depend: Array<string>;
6
6
  provide: Array<string>;
7
7
  render?: string;
8
+ config?: Schema<any>;
8
9
  }
9
10
  export declare class PluginLoader {
10
11
  private pluginsDir;
@@ -29,11 +30,11 @@ export declare class PluginLoader {
29
30
  * This does NOT reload any plugins.
30
31
  */
31
32
  reloadConfigFile(): Promise<void>;
33
+ saveConfig(): Promise<void>;
32
34
  getCore(): Core;
33
35
  getContext(pluginName: string, injections?: Record<string, any>): Context;
34
36
  unregall(pluginName: string): void;
35
37
  loadConfig(configPath: string): Promise<void>;
36
- getPluginConfig(pluginName: string): Promise<Config>;
37
38
  loadPlugins(): Promise<void>;
38
39
  loadSinglePlugin(pluginName: string, triggerPendingCheck?: boolean, onlypending?: boolean): Promise<boolean>;
39
40
  private _loadPendingPlugins;
package/dist/index.js CHANGED
@@ -67,27 +67,16 @@ class PluginLoader {
67
67
  * This does NOT reload any plugins.
68
68
  */
69
69
  async reloadConfigFile() {
70
- this.logger.info('Reloading config file...');
70
+ this.core.coreConfig = this.config.core || {};
71
+ this.core.emit('config-reloaded', this.config);
72
+ }
73
+ async saveConfig() {
71
74
  try {
72
- const ext = path.extname(this.configPath).toLowerCase();
73
- const fileContent = fs.readFileSync(this.configPath, 'utf8');
74
- let doc;
75
- if (ext === '.yaml' || ext === '.yml') {
76
- doc = yaml.load(fileContent);
77
- }
78
- else if (ext === '.json') {
79
- doc = JSON.parse(fileContent);
80
- }
81
- else {
82
- throw new Error(`Unsupported config file extension: ${ext}`);
83
- }
84
- this.config = doc;
85
- this.core.coreConfig = this.config.core || {};
86
- this.core.emit('config-reloaded', this.config);
87
- this.logger.info('Config file reloaded successfully.');
75
+ const jsonConfig = JSON.stringify(this.config, null, 2);
76
+ fs.writeFileSync(this.configPath, jsonConfig, 'utf8');
88
77
  }
89
78
  catch (e) {
90
- this.logger.error('Failed to reload config file:', e);
79
+ this.logger.error('Failed to save config file:', e);
91
80
  }
92
81
  }
93
82
  getCore() {
@@ -127,13 +116,6 @@ class PluginLoader {
127
116
  throw e;
128
117
  }
129
118
  }
130
- async getPluginConfig(pluginName) {
131
- const actualPluginName = pluginName.startsWith('~') ? pluginName.substring(1) : pluginName;
132
- if (!this.config.plugins[actualPluginName]) {
133
- return new core_1.Config(actualPluginName);
134
- }
135
- return new core_1.Config(actualPluginName, this.config.plugins[actualPluginName]);
136
- }
137
119
  async loadPlugins() {
138
120
  if (!this.config || typeof this.config.plugins !== 'object' || this.config.plugins === null) {
139
121
  this.logger.info('No plugins configuration found. No plugins to load.');
@@ -230,9 +212,15 @@ class PluginLoader {
230
212
  for (const injection of depend) {
231
213
  injections[injection] = this.core.getComponent(injection);
232
214
  }
233
- const pluginConfig = await this.getPluginConfig(pluginName);
215
+ // ### NEW CONFIG LOGIC ###
216
+ const rawConfig = (this.config.plugins && this.config.plugins[pluginName]) || {};
217
+ const schema = pluginInstance.config; // The schema is exported as 'config'
218
+ const finalConfig = (0, core_1.fallback)(schema, rawConfig);
219
+ // Update the in-memory config with the fully resolved one
220
+ this.config.plugins[pluginName] = finalConfig;
221
+ // ### END NEW CONFIG LOGIC ###
234
222
  const context = this.getContext(pluginName, injections);
235
- await this.core.plugin(pluginInstance, context, pluginConfig);
223
+ await this.core.plugin(pluginInstance, context, finalConfig);
236
224
  this.pluginStatus[pluginName] = "enabled" /* PluginStatus.ENABLED */;
237
225
  if (triggerPendingCheck) {
238
226
  await this._loadPendingPlugins();
@@ -329,20 +317,15 @@ class PluginLoader {
329
317
  */
330
318
  async reloadPlugin(pluginName) {
331
319
  this.logger.info(`Reloading plugin: "${pluginName}"...`);
332
- // Clear the module cache for the plugin. This is critical for hot-reloading.
333
320
  try {
334
321
  const resolvedPath = require.resolve(pluginName);
335
322
  this.clearRequireCache(resolvedPath, new Set());
336
- this.logger.info(`Cache cleared for plugin "${pluginName}".`);
337
323
  }
338
324
  catch (e) {
339
325
  this.logger.error(`Could not resolve path for plugin ${pluginName} to clear cache.`, e);
340
326
  }
341
- // Reload the configuration from disk to catch any changes.
342
327
  await this.reloadConfigFile();
343
- // Unload the plugin and its dependents.
344
328
  await this.unloadPlugin(pluginName, true);
345
- // Load the plugin again. This will also trigger a check for other pending plugins.
346
329
  const success = await this.loadSinglePlugin(pluginName);
347
330
  if (success) {
348
331
  this.logger.info(`Plugin "${pluginName}" reloaded successfully.`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yumerijs/loader",
3
- "version": "2.1.1",
3
+ "version": "2.1.2",
4
4
  "description": "Module loader for yumeri",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -40,6 +40,6 @@
40
40
  "js-yaml": "^4.1.0"
41
41
  },
42
42
  "peerDependencies": {
43
- "@yumerijs/core": "^2.0.2"
43
+ "@yumerijs/core": "^2.1.1"
44
44
  }
45
45
  }