matterbridge
Version:
Matterbridge plugin manager for Matter
271 lines • 15.5 kB
TypeScript
/**
* This file contains the Plugins class.
*
* @file plugins.ts
* @author Luca Liguori
* @date 2024-07-14
* @version 1.1.1
*
* Copyright 2024, 2025, 2026 Luca Liguori.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License. *
*/
import { LogLevel } from './logger/export.js';
import { Matterbridge } from './matterbridge.js';
import { MatterbridgePlatform, PlatformConfig, PlatformSchema } from './matterbridgePlatform.js';
import { RegisteredPlugin } from './matterbridgeTypes.js';
export declare class PluginManager {
private _plugins;
private nodeContext;
private matterbridge;
private log;
constructor(matterbridge: Matterbridge);
get length(): number;
get size(): number;
has(name: string): boolean;
get(name: string): RegisteredPlugin | undefined;
set(plugin: RegisteredPlugin): RegisteredPlugin;
clear(): void;
array(): RegisteredPlugin[];
[Symbol.iterator](): MapIterator<RegisteredPlugin>;
forEach(callback: (plugin: RegisteredPlugin) => Promise<void>): Promise<void>;
set logLevel(logLevel: LogLevel);
/**
* Loads registered plugins from storage.
*
* This method retrieves an array of registered plugins from the storage and converts it
* into a map where the plugin names are the keys and the plugin objects are the values.
*
* @returns {Promise<RegisteredPlugin[]>} A promise that resolves to an array of registered plugins.
*/
loadFromStorage(): Promise<RegisteredPlugin[]>;
/**
* Loads registered plugins from storage.
*
* This method retrieves an array of registered plugins from the storage and converts it
* into a map where the plugin names are the keys and the plugin objects are the values.
*
* @returns {Promise<RegisteredPlugin[]>} A promise that resolves to an array of registered plugins.
*/
saveToStorage(): Promise<number>;
/**
* Resolves the name of a plugin by loading and parsing its package.json file.
* @param pluginPath - The path to the plugin or the path to the plugin's package.json file.
* @returns The path to the resolved package.json file, or null if the package.json file is not found or does not contain a name.
*/
resolve(pluginPath: string): Promise<string | null>;
/**
* Get the author of a plugin from its package.json.
*
* @param {Record<string, string | number | Record<string, string | number | object>>} packageJson - The package.json object of the plugin.
* @returns {string} The author of the plugin, or 'Unknown author' if not found.
*/
getAuthor(packageJson: Record<string, string | number | Record<string, string | number | object>>): string;
/**
* Get the homepage of a plugin from its package.json.
*
* @param {Record<string, string | number | Record<string, string | number | object>>} packageJson - The package.json object of the plugin.
* @returns {string | undefined} The homepage of the plugin, or undefined if not found.
*/
getHomepage(packageJson: Record<string, string | number | Record<string, string | number | object>>): string | undefined;
/**
* Get the help URL of a plugin from its package.json.
*
* @param {Record<string, string | number | Record<string, string | number | object>>} packageJson - The package.json object of the plugin.
* @returns {string | undefined} The URL to the help page or to the README file, or undefined if not found.
*/
getHelp(packageJson: Record<string, string | number | Record<string, string | number | object>>): string | undefined;
/**
* Get the changelog URL of a plugin from its package.json.
*
* @param {Record<string, string | number | Record<string, string | number | object>>} packageJson - The package.json object of the plugin.
* @returns {string | undefined} The URL to the CHANGELOG file, or undefined if not found.
*/
getChangelog(packageJson: Record<string, string | number | Record<string, string | number | object>>): string | undefined;
/**
* Get the first funding URL(s) of a plugin from its package.json.
*
* @param {Record<string, any>} packageJson - The package.json object of the plugin.
* @returns {string | undefined} The first funding URLs, or undefined if not found.
*/
getFunding(packageJson: Record<string, any>): string | undefined;
/**
* 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.
*/
parse(plugin: RegisteredPlugin): Promise<Record<string, string | number | object> | null>;
/**
* Enables a plugin by its name or path.
*
* This method enables a plugin by setting its `enabled` property to `true` and saving the updated
* plugin information to storage. It first checks if the plugin is already registered in the `_plugins` map.
* If not, it attempts to resolve the plugin's `package.json` file to retrieve its name and enable it.
*
* @param {string} nameOrPath - The name or path of the plugin to enable.
* @returns {Promise<RegisteredPlugin | null>} A promise that resolves to the enabled plugin object, or null if the plugin could not be enabled.
*/
enable(nameOrPath: string): Promise<RegisteredPlugin | null>;
/**
* Enables a plugin by its name or path.
*
* This method enables a plugin by setting its `enabled` property to `true` and saving the updated
* plugin information to storage. It first checks if the plugin is already registered in the `_plugins` map.
* If not, it attempts to resolve the plugin's `package.json` file to retrieve its name and enable it.
*
* @param {string} nameOrPath - The name or path of the plugin to enable.
* @returns {Promise<RegisteredPlugin | null>} A promise that resolves to the enabled plugin object, or null if the plugin could not be enabled.
*/
disable(nameOrPath: string): Promise<RegisteredPlugin | null>;
/**
* Removes a plugin by its name or path.
*
* This method removes a plugin from the `_plugins` map and saves the updated plugin information to storage.
* It first checks if the plugin is already registered in the `_plugins` map. If not, it attempts to resolve
* the plugin's `package.json` file to retrieve its name and remove it.
*
* @param {string} nameOrPath - The name or path of the plugin to remove.
* @returns {Promise<RegisteredPlugin | null>} A promise that resolves to the removed plugin object, or null if the plugin could not be removed.
*/
remove(nameOrPath: string): Promise<RegisteredPlugin | null>;
/**
* Adds a plugin by its name or path.
*
* This method adds a plugin to the `_plugins` map and saves the updated plugin information to storage.
* It first resolves the plugin's `package.json` file to retrieve its details. If the plugin is already
* registered, it logs an info message and returns null. Otherwise, it registers the plugin, enables it,
* and saves the updated plugin information to storage.
*
* @param {string} nameOrPath - The name or path of the plugin to add.
* @returns {Promise<RegisteredPlugin | null>} A promise that resolves to the added plugin object, or null if the plugin could not be added.
*/
add(nameOrPath: string): Promise<RegisteredPlugin | null>;
/**
* Installs a plugin by its name.
*
* This method first uninstalls any existing version of the plugin, then installs the plugin globally using npm.
* It logs the installation process and retrieves the installed version of the plugin.
*
* @param {string} name - The name of the plugin to install.
* @returns {Promise<string | undefined>} A promise that resolves to the installed version of the plugin, or undefined if the installation failed.
*/
install(name: string): Promise<string | undefined>;
/**
* Uninstalls a plugin by its name.
*
* This method uninstalls a globally installed plugin using npm. It logs the uninstallation process
* and returns the name of the uninstalled plugin if successful, or undefined if the uninstallation failed.
*
* @param {string} name - The name of the plugin to uninstall.
* @returns {Promise<string | undefined>} A promise that resolves to the name of the uninstalled plugin, or undefined if the uninstallation failed.
*/
uninstall(name: string): Promise<string | undefined>;
/**
* Loads a plugin and returns the corresponding MatterbridgePlatform instance.
* @param plugin - The plugin to load.
* @param start - Optional flag indicating whether to start the plugin after loading. Default is false.
* @param message - Optional message to pass to the plugin when starting.
* @returns A Promise that resolves to the loaded MatterbridgePlatform instance.
* @throws An error if the plugin is not enabled, already loaded, or fails to load.
*/
load(plugin: RegisteredPlugin, start?: boolean, message?: string, configure?: boolean): Promise<MatterbridgePlatform | undefined>;
/**
* Starts a plugin.
*
* @param {RegisteredPlugin} plugin - The plugin to start.
* @param {string} [message] - Optional message to pass to the plugin's onStart method.
* @param {boolean} [configure] - Indicates whether to configure the plugin after starting (default false).
* @returns {Promise<RegisteredPlugin | undefined>} A promise that resolves when the plugin is started successfully, or rejects with an error if starting the plugin fails.
*/
start(plugin: RegisteredPlugin, message?: string, configure?: boolean): Promise<RegisteredPlugin | undefined>;
/**
* Configures a plugin.
*
* @param {RegisteredPlugin} plugin - The plugin to configure.
* @returns {Promise<void>} A promise that resolves when the plugin is configured successfully, or rejects with an error if configuration fails.
*/
configure(plugin: RegisteredPlugin): Promise<RegisteredPlugin | undefined>;
/**
* Shuts down a plugin.
*
* This method shuts down a plugin by calling its `onShutdown` method and resetting its state.
* It logs the shutdown process and optionally removes all devices associated with the plugin.
*
* @param {RegisteredPlugin} plugin - The plugin to shut down.
* @param {string} [reason] - The reason for shutting down the plugin.
* @param {boolean} [removeAllDevices=false] - Whether to remove all devices associated with the plugin.
* @param {boolean} [force=false] - Whether to force the shutdown even if the plugin is not loaded or started.
* @returns {Promise<RegisteredPlugin | undefined>} A promise that resolves to the shut down plugin object, or undefined if the shutdown failed.
*/
shutdown(plugin: RegisteredPlugin, reason?: string, removeAllDevices?: boolean, force?: boolean): Promise<RegisteredPlugin | undefined>;
/**
* Loads the configuration for a plugin.
* If the configuration file exists, it reads the file and returns the parsed JSON data.
* If the configuration file does not exist, it creates a new file with default configuration and returns it.
* If any error occurs during file access or creation, it logs an error and return un empty config.
*
* @param plugin - The plugin for which to load the configuration.
* @returns A promise that resolves to the loaded or created configuration.
*/
loadConfig(plugin: RegisteredPlugin): Promise<PlatformConfig>;
/**
* Saves the configuration of a plugin to a file.
*
* This method saves the configuration of the specified plugin to a JSON file in the matterbridge directory.
* If the plugin's configuration is not found, it logs an error and rejects the promise. If the configuration
* is successfully saved, it logs a debug message. If an error occurs during the file write operation, it logs
* the error and rejects the promise.
*
* @param {RegisteredPlugin} plugin - The plugin whose configuration is to be saved.
* @returns {Promise<void>} A promise that resolves when the configuration is successfully saved, or rejects if an error occurs.
* @throws {Error} If the plugin's configuration is not found.
*/
saveConfigFromPlugin(plugin: RegisteredPlugin): Promise<void>;
/**
* Saves the configuration of a plugin from a JSON object to a file.
*
* This method saves the provided configuration of the specified plugin to a JSON file in the matterbridge directory.
* It first checks if the configuration data is valid by ensuring it contains the correct name and type, and matches
* the plugin's name. If the configuration data is invalid, it logs an error and returns. If the configuration is
* successfully saved, it updates the plugin's `configJson` property and logs a debug message. If an error occurs
* during the file write operation, it logs the error and returns.
*
* @param {RegisteredPlugin} plugin - The plugin whose configuration is to be saved.
* @param {PlatformConfig} config - The configuration data to be saved.
* @returns {Promise<void>} A promise that resolves when the configuration is successfully saved, or returns if an error occurs.
*/
saveConfigFromJson(plugin: RegisteredPlugin, config: PlatformConfig): Promise<void>;
/**
* Loads the schema for a plugin.
*
* This method attempts to load the schema file for the specified plugin. If the schema file is found,
* it reads and parses the file, updates the schema's title and description, and logs the process.
* If the schema file is not found, it logs the event and loads a default schema for the plugin.
*
* @param {RegisteredPlugin} plugin - The plugin whose schema is to be loaded.
* @returns {Promise<PlatformSchema>} A promise that resolves to the loaded schema object, or the default schema if the schema file is not found.
*/
loadSchema(plugin: RegisteredPlugin): Promise<PlatformSchema>;
/**
* Returns the default schema for a plugin.
*
* This method generates a default schema object for the specified plugin. The schema includes
* metadata such as the plugin's title, description, version, and author. It also defines the
* properties of the schema, including the plugin's name, type, debug flag, and unregisterOnShutdown flag.
*
* @param {RegisteredPlugin} plugin - The plugin for which the default schema is to be generated.
* @returns {PlatformSchema} The default schema object for the plugin.
*/
getDefaultSchema(plugin: RegisteredPlugin): PlatformSchema;
}
//# sourceMappingURL=pluginManager.d.ts.map