@yumerijs/loader 2.0.1 → 2.0.3

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,5 +1,4 @@
1
1
  import { Core, Config, Logger, Context, PluginStatus } from '@yumerijs/core';
2
- import 'esbuild-register';
3
2
  interface Plugin {
4
3
  apply: (ctx: Context, config: Config) => Promise<void>;
5
4
  disable: (ctx: Context) => Promise<void>;
@@ -24,17 +23,25 @@ export declare class PluginLoader {
24
23
  private pluginContexts;
25
24
  private isDev;
26
25
  constructor(core?: Core, pluginsDir?: string);
26
+ /**
27
+ * Reloads the config file from disk into memory and emits a 'config-reloaded' event.
28
+ * This does NOT reload any plugins.
29
+ */
30
+ reloadConfigFile(): Promise<void>;
27
31
  getCore(): Core;
28
32
  getContext(pluginName: string): Context;
29
33
  unregall(pluginName: string): void;
30
34
  loadConfig(configPath: string): Promise<void>;
31
- private watchConfig;
32
35
  getPluginConfig(pluginName: string): Promise<Config>;
33
36
  loadPlugins(): Promise<void>;
34
37
  loadSinglePlugin(pluginName: string, triggerPendingCheck?: boolean, onlypending?: boolean): Promise<boolean>;
35
38
  private _loadPendingPlugins;
36
39
  unloadPlugin(pluginNameToUnload: string, ispending?: boolean): Promise<void>;
37
40
  private _unloadSinglePlugin;
41
+ /**
42
+ * Reloads a single plugin's code by clearing the require cache and then reloading it.
43
+ * @param pluginName The name of the plugin to reload.
44
+ */
38
45
  reloadPlugin(pluginName: string): Promise<void>;
39
46
  private watchPlugin;
40
47
  loadModule(pluginName: string): Promise<Plugin>;
package/dist/index.js CHANGED
@@ -41,7 +41,6 @@ const util_1 = require("util");
41
41
  const child_process_1 = require("child_process");
42
42
  const yaml = __importStar(require("js-yaml"));
43
43
  const chokidar = __importStar(require("chokidar"));
44
- require("esbuild-register");
45
44
  const execAsync = (0, util_1.promisify)(child_process_1.exec);
46
45
  class PluginLoader {
47
46
  pluginsDir;
@@ -57,9 +56,28 @@ class PluginLoader {
57
56
  isDev = false;
58
57
  constructor(core, pluginsDir = 'plugins') {
59
58
  this.pluginsDir = pluginsDir;
60
- this.core = core || new core_1.Core(undefined, false);
59
+ this.core = core || new core_1.Core(this, undefined, false);
60
+ this.core.uuid = Math.random().toString(36).substring(2);
61
+ this.logger.info(`[DIAG] Loader created/received Core instance with UUID: ${this.core.uuid}`);
61
62
  this.isDev = process.env.NODE_ENV === 'development';
62
- core_1.Logger.setCore(this.core); // Set the core instance for the logger
63
+ core_1.Logger.setCore(this.core);
64
+ }
65
+ /**
66
+ * Reloads the config file from disk into memory and emits a 'config-reloaded' event.
67
+ * This does NOT reload any plugins.
68
+ */
69
+ async reloadConfigFile() {
70
+ this.logger.info('Reloading config file...');
71
+ 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.');
77
+ }
78
+ catch (e) {
79
+ this.logger.error('Failed to reload config file:', e);
80
+ }
63
81
  }
64
82
  getCore() {
65
83
  return this.core;
@@ -83,9 +101,6 @@ class PluginLoader {
83
101
  const doc = yaml.load(fs.readFileSync(configPath, 'utf8'));
84
102
  this.config = doc;
85
103
  this.logger.info('Config loaded.');
86
- if (this.isDev) {
87
- this.watchConfig(configPath);
88
- }
89
104
  this.core.coreConfig = this.config.core || {};
90
105
  this.core.i18n = new (require('@yumerijs/core').I18n)(this.core.coreConfig.lang || ['zh', 'en']);
91
106
  }
@@ -94,42 +109,8 @@ class PluginLoader {
94
109
  throw e;
95
110
  }
96
111
  }
97
- watchConfig(configPath) {
98
- const watcher = chokidar.watch(configPath, {
99
- persistent: true,
100
- ignoreInitial: true,
101
- awaitWriteFinish: {
102
- stabilityThreshold: 300,
103
- pollInterval: 100,
104
- },
105
- });
106
- watcher.on('change', async () => {
107
- try {
108
- const doc = yaml.load(fs.readFileSync(configPath, 'utf8'));
109
- if (doc && doc.plugins) {
110
- this.config.plugins = doc.plugins;
111
- }
112
- await this.core.emit('config-changed', this.config);
113
- }
114
- catch (error) {
115
- this.logger.error('Failed to reload config:', error);
116
- }
117
- });
118
- this.logger.info(`Watching for config changes at ${configPath}`);
119
- }
120
112
  async getPluginConfig(pluginName) {
121
113
  const actualPluginName = pluginName.startsWith('~') ? pluginName.substring(1) : pluginName;
122
- if (this.configPath && this.isDev) {
123
- try {
124
- const doc = yaml.load(fs.readFileSync(this.configPath, 'utf8'));
125
- if (doc && doc.plugins) {
126
- this.config.plugins = doc.plugins;
127
- }
128
- }
129
- catch (error) {
130
- this.logger.warn('Failed to refresh config for hot reload:', error);
131
- }
132
- }
133
114
  if (!this.config.plugins[actualPluginName]) {
134
115
  return new core_1.Config(actualPluginName);
135
116
  }
@@ -201,7 +182,6 @@ class PluginLoader {
201
182
  this.plugins[pluginName] = pluginInstance;
202
183
  const pluginConfig = await this.getPluginConfig(pluginName);
203
184
  const context = this.getContext(pluginName);
204
- // The new API
205
185
  await this.core.plugin(pluginInstance, context, pluginConfig);
206
186
  this.pluginStatus[pluginName] = "enabled" /* PluginStatus.ENABLED */;
207
187
  if (triggerPendingCheck) {
@@ -293,7 +273,13 @@ class PluginLoader {
293
273
  this.logger.error(`Failed to unload plugin "${pluginName}":`, error);
294
274
  }
295
275
  }
276
+ /**
277
+ * Reloads a single plugin's code by clearing the require cache and then reloading it.
278
+ * @param pluginName The name of the plugin to reload.
279
+ */
296
280
  async reloadPlugin(pluginName) {
281
+ this.logger.info(`Reloading plugin code for "${pluginName}"...`);
282
+ this.clearRequireCache(pluginName, new Set());
297
283
  await this.unloadPlugin(pluginName);
298
284
  const success = await this.loadSinglePlugin(pluginName);
299
285
  if (success) {
@@ -332,9 +318,6 @@ class PluginLoader {
332
318
  this.pluginWatchers[pluginName] = watcher;
333
319
  }
334
320
  async loadModule(pluginName) {
335
- if (this.isDev) {
336
- this.clearRequireCache(pluginName, new Set());
337
- }
338
321
  const plugin = await require(pluginName);
339
322
  return plugin.default || plugin;
340
323
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yumerijs/loader",
3
- "version": "2.0.1",
3
+ "version": "2.0.3",
4
4
  "description": "Module loader for yumeri",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -27,14 +27,17 @@
27
27
  "devDependencies": {
28
28
  "@types/js-yaml": "^4.0.9",
29
29
  "@types/node": "^22.13.10",
30
+ "@yumerijs/core": "^2.0.1",
30
31
  "esbuild": "^0.25.9",
31
32
  "esbuild-register": "^3.6.0",
32
33
  "typescript": "^5.8.2"
33
34
  },
34
35
  "dependencies": {
35
36
  "@types/js-yaml": "^4.0.9",
36
- "@yumerijs/core": "^2.0.1",
37
37
  "chokidar": "^4.0.3",
38
38
  "js-yaml": "^4.1.0"
39
+ },
40
+ "peerDependencies": {
41
+ "@yumerijs/core": "^2.0.2"
39
42
  }
40
43
  }