Skip to content

Commit

Permalink
Added parsePlugin to load the updated data from the plugin even when …
Browse files Browse the repository at this point in the history
…is disabled
  • Loading branch information
Luligu committed Jul 2, 2024
1 parent 16ff7ce commit 6f2bc0e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ If you like this project and find it useful, please consider giving it a star on
### Added

- [fabric]: Added sanitized fabricInfo to matterbridge in bridge mode and to the plugins in childbridge mode.
- [matterbridge]: Added parsePlugin to load the updated data from the plugin even when is disabled.

<a href="https://www.buymeacoffee.com/luligugithub">
<img src="./yellow-button.png" alt="Buy me a coffee" width="120">
Expand Down
40 changes: 40 additions & 0 deletions src/matterbridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,13 @@ export class Matterbridge extends EventEmitter {
// Get the plugins from node storage and create the plugin node storage contexts
this.registeredPlugins = await this.nodeContext.get<RegisteredPlugin[]>('plugins', []);
for (const plugin of this.registeredPlugins) {
const packageJson = await this.parsePlugin(plugin);
if (packageJson) {
plugin.name = packageJson.name as string;
plugin.version = packageJson.version as string;
plugin.description = packageJson.description as string;
plugin.author = packageJson.author as string;
}
this.log.debug(`Creating node storage context for plugin ${plugin.name}`);
plugin.nodeContext = await this.nodeStorage.createStorage(plugin.name);
await plugin.nodeContext.set<string>('name', plugin.name);
Expand Down Expand Up @@ -657,6 +664,14 @@ export class Matterbridge extends EventEmitter {
for (const plugin of this.registeredPlugins) {
plugin.configJson = await this.loadPluginConfig(plugin);
plugin.schemaJson = await this.loadPluginSchema(plugin);
// Check if the plugin is available
if (!(await this.resolvePluginName(plugin.path))) {
this.log.error(`Plugin ${plg}${plugin.name}${er} not found. Disabling it.`);
plugin.enabled = false;
plugin.error = true;
continue;
}
// Check if the plugin has a new version
this.getPluginLatestVersion(plugin);
if (!plugin.enabled) {
this.log.info(`Plugin ${plg}${plugin.name}${nf} not enabled`);
Expand Down Expand Up @@ -693,6 +708,14 @@ export class Matterbridge extends EventEmitter {
for (const plugin of this.registeredPlugins) {
plugin.configJson = await this.loadPluginConfig(plugin);
plugin.schemaJson = await this.loadPluginSchema(plugin);
// Check if the plugin is available
if (!(await this.resolvePluginName(plugin.path))) {
this.log.error(`Plugin ${plg}${plugin.name}${er} not found. Disabling it.`);
plugin.enabled = false;
plugin.error = true;
continue;
}
// Check if the plugin has a new version
this.getPluginLatestVersion(plugin);
if (!plugin.enabled) {
this.log.info(`Plugin ${plg}${plugin.name}${nf} not enabled`);
Expand Down Expand Up @@ -1588,6 +1611,23 @@ export class Matterbridge extends EventEmitter {
}
}

/**
* Loads and parse the plugin package.json and returns it.
* @param plugin - The plugin to load the package from.
* @returns A Promise that resolves to the package.json object or undefined if the package.json could not be loaded.
*/
private async parsePlugin(plugin: RegisteredPlugin): Promise<Record<string, string | number | object> | undefined> {
this.log.debug(`Parsing package.json of plugin ${plg}${plugin.name}${nf} type ${typ}${plugin.type}${nf}`);
try {
const packageJson = JSON.parse(await fs.readFile(plugin.path, 'utf8'));
return packageJson;
} catch (err) {
this.log.error(`Failed to parse plugin ${plg}${plugin.name}${er} package.json: ${err}`);
plugin.error = true;
return undefined;
}
}

/**
* Loads a plugin and returns the corresponding MatterbridgePlatform instance.
* @param plugin - The plugin to load.
Expand Down

0 comments on commit 6f2bc0e

Please sign in to comment.