@yumerijs/loader 2.1.0 → 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,16 +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 doc = yaml.load(fs.readFileSync(this.configPath, 'utf8'));
73
- this.config = doc;
74
- this.core.coreConfig = this.config.core || {};
75
- this.core.emit('config-reloaded', this.config);
76
- this.logger.info('Config file reloaded.');
75
+ const jsonConfig = JSON.stringify(this.config, null, 2);
76
+ fs.writeFileSync(this.configPath, jsonConfig, 'utf8');
77
77
  }
78
78
  catch (e) {
79
- this.logger.error('Failed to reload config file:', e);
79
+ this.logger.error('Failed to save config file:', e);
80
80
  }
81
81
  }
82
82
  getCore() {
@@ -98,7 +98,14 @@ class PluginLoader {
98
98
  async loadConfig(configPath) {
99
99
  try {
100
100
  this.configPath = configPath;
101
- const doc = yaml.load(fs.readFileSync(configPath, 'utf8'));
101
+ const fileContents = fs.readFileSync(configPath, 'utf8');
102
+ let doc;
103
+ if (path.extname(configPath) === '.json') {
104
+ doc = JSON.parse(fileContents);
105
+ }
106
+ else {
107
+ doc = yaml.load(fileContents);
108
+ }
102
109
  this.config = doc;
103
110
  this.logger.info('Config loaded.');
104
111
  this.core.coreConfig = this.config.core || {};
@@ -109,13 +116,6 @@ class PluginLoader {
109
116
  throw e;
110
117
  }
111
118
  }
112
- async getPluginConfig(pluginName) {
113
- const actualPluginName = pluginName.startsWith('~') ? pluginName.substring(1) : pluginName;
114
- if (!this.config.plugins[actualPluginName]) {
115
- return new core_1.Config(actualPluginName);
116
- }
117
- return new core_1.Config(actualPluginName, this.config.plugins[actualPluginName]);
118
- }
119
119
  async loadPlugins() {
120
120
  if (!this.config || typeof this.config.plugins !== 'object' || this.config.plugins === null) {
121
121
  this.logger.info('No plugins configuration found. No plugins to load.');
@@ -212,9 +212,15 @@ class PluginLoader {
212
212
  for (const injection of depend) {
213
213
  injections[injection] = this.core.getComponent(injection);
214
214
  }
215
- 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 ###
216
222
  const context = this.getContext(pluginName, injections);
217
- await this.core.plugin(pluginInstance, context, pluginConfig);
223
+ await this.core.plugin(pluginInstance, context, finalConfig);
218
224
  this.pluginStatus[pluginName] = "enabled" /* PluginStatus.ENABLED */;
219
225
  if (triggerPendingCheck) {
220
226
  await this._loadPendingPlugins();
@@ -311,20 +317,15 @@ class PluginLoader {
311
317
  */
312
318
  async reloadPlugin(pluginName) {
313
319
  this.logger.info(`Reloading plugin: "${pluginName}"...`);
314
- // Clear the module cache for the plugin. This is critical for hot-reloading.
315
320
  try {
316
321
  const resolvedPath = require.resolve(pluginName);
317
322
  this.clearRequireCache(resolvedPath, new Set());
318
- this.logger.info(`Cache cleared for plugin "${pluginName}".`);
319
323
  }
320
324
  catch (e) {
321
325
  this.logger.error(`Could not resolve path for plugin ${pluginName} to clear cache.`, e);
322
326
  }
323
- // Reload the configuration from disk to catch any changes.
324
327
  await this.reloadConfigFile();
325
- // Unload the plugin and its dependents.
326
328
  await this.unloadPlugin(pluginName, true);
327
- // Load the plugin again. This will also trigger a check for other pending plugins.
328
329
  const success = await this.loadSinglePlugin(pluginName);
329
330
  if (success) {
330
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.0",
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
  }