matterbridge 3.1.4 → 3.1.5-dev-20250718-054cd80
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/CHANGELOG.md +20 -0
- package/README-DEV.md +59 -0
- package/dist/cli.js +2 -91
- package/dist/cliEmitter.js +0 -30
- package/dist/clusters/export.js +0 -2
- package/dist/defaultConfigSchema.js +0 -24
- package/dist/deviceManager.js +1 -94
- package/dist/devices/batteryStorage.js +1 -48
- package/dist/devices/evse.js +10 -74
- package/dist/devices/export.js +0 -2
- package/dist/devices/heatPump.js +2 -50
- package/dist/devices/laundryDryer.js +6 -83
- package/dist/devices/laundryWasher.js +7 -91
- package/dist/devices/roboticVacuumCleaner.js +6 -89
- package/dist/devices/solarPower.js +0 -38
- package/dist/devices/waterHeater.js +2 -82
- package/dist/frontend.js +21 -429
- package/dist/globalMatterbridge.js +0 -47
- package/dist/helpers.js +0 -53
- package/dist/index.js +1 -30
- package/dist/logger/export.js +0 -1
- package/dist/matter/behaviors.js +0 -2
- package/dist/matter/clusters.js +0 -2
- package/dist/matter/devices.js +0 -2
- package/dist/matter/endpoints.js +0 -2
- package/dist/matter/export.js +0 -3
- package/dist/matter/types.js +0 -3
- package/dist/matterbridge.js +66 -802
- package/dist/matterbridgeAccessoryPlatform.js +0 -36
- package/dist/matterbridgeBehaviors.js +1 -61
- package/dist/matterbridgeDeviceTypes.js +15 -579
- package/dist/matterbridgeDynamicPlatform.js +0 -36
- package/dist/matterbridgeEndpoint.js +42 -1106
- package/dist/matterbridgeEndpointHelpers.js +12 -322
- package/dist/matterbridgePlatform.js +0 -233
- package/dist/matterbridgeTypes.js +0 -25
- package/dist/pluginManager.js +46 -350
- package/dist/shelly.js +7 -168
- package/dist/storage/export.js +0 -1
- package/dist/update.js +0 -54
- package/dist/utils/colorUtils.js +2 -263
- package/dist/utils/commandLine.js +0 -54
- package/dist/utils/copyDirectory.js +1 -38
- package/dist/utils/createDirectory.js +0 -33
- package/dist/utils/createZip.js +2 -47
- package/dist/utils/deepCopy.js +0 -39
- package/dist/utils/deepEqual.js +1 -72
- package/dist/utils/error.js +9 -0
- package/dist/utils/export.js +0 -1
- package/dist/utils/hex.js +0 -58
- package/dist/utils/isvalid.js +0 -101
- package/dist/utils/network.js +5 -81
- package/dist/utils/spawn.js +0 -18
- package/dist/utils/wait.js +9 -62
- package/npm-shrinkwrap.json +5 -5
- package/package.json +1 -2
- package/dist/cli.d.ts +0 -26
- package/dist/cli.d.ts.map +0 -1
- package/dist/cli.js.map +0 -1
- package/dist/cliEmitter.d.ts +0 -34
- package/dist/cliEmitter.d.ts.map +0 -1
- package/dist/cliEmitter.js.map +0 -1
- package/dist/clusters/export.d.ts +0 -2
- package/dist/clusters/export.d.ts.map +0 -1
- package/dist/clusters/export.js.map +0 -1
- package/dist/defaultConfigSchema.d.ts +0 -28
- package/dist/defaultConfigSchema.d.ts.map +0 -1
- package/dist/defaultConfigSchema.js.map +0 -1
- package/dist/deviceManager.d.ts +0 -112
- package/dist/deviceManager.d.ts.map +0 -1
- package/dist/deviceManager.js.map +0 -1
- package/dist/devices/batteryStorage.d.ts +0 -48
- package/dist/devices/batteryStorage.d.ts.map +0 -1
- package/dist/devices/batteryStorage.js.map +0 -1
- package/dist/devices/evse.d.ts +0 -75
- package/dist/devices/evse.d.ts.map +0 -1
- package/dist/devices/evse.js.map +0 -1
- package/dist/devices/export.d.ts +0 -9
- package/dist/devices/export.d.ts.map +0 -1
- package/dist/devices/export.js.map +0 -1
- package/dist/devices/heatPump.d.ts +0 -47
- package/dist/devices/heatPump.d.ts.map +0 -1
- package/dist/devices/heatPump.js.map +0 -1
- package/dist/devices/laundryDryer.d.ts +0 -87
- package/dist/devices/laundryDryer.d.ts.map +0 -1
- package/dist/devices/laundryDryer.js.map +0 -1
- package/dist/devices/laundryWasher.d.ts +0 -242
- package/dist/devices/laundryWasher.d.ts.map +0 -1
- package/dist/devices/laundryWasher.js.map +0 -1
- package/dist/devices/roboticVacuumCleaner.d.ts +0 -110
- package/dist/devices/roboticVacuumCleaner.d.ts.map +0 -1
- package/dist/devices/roboticVacuumCleaner.js.map +0 -1
- package/dist/devices/solarPower.d.ts +0 -40
- package/dist/devices/solarPower.d.ts.map +0 -1
- package/dist/devices/solarPower.js.map +0 -1
- package/dist/devices/waterHeater.d.ts +0 -111
- package/dist/devices/waterHeater.d.ts.map +0 -1
- package/dist/devices/waterHeater.js.map +0 -1
- package/dist/frontend.d.ts +0 -304
- package/dist/frontend.d.ts.map +0 -1
- package/dist/frontend.js.map +0 -1
- package/dist/globalMatterbridge.d.ts +0 -59
- package/dist/globalMatterbridge.d.ts.map +0 -1
- package/dist/globalMatterbridge.js.map +0 -1
- package/dist/helpers.d.ts +0 -48
- package/dist/helpers.d.ts.map +0 -1
- package/dist/helpers.js.map +0 -1
- package/dist/index.d.ts +0 -33
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/logger/export.d.ts +0 -2
- package/dist/logger/export.d.ts.map +0 -1
- package/dist/logger/export.js.map +0 -1
- package/dist/matter/behaviors.d.ts +0 -2
- package/dist/matter/behaviors.d.ts.map +0 -1
- package/dist/matter/behaviors.js.map +0 -1
- package/dist/matter/clusters.d.ts +0 -2
- package/dist/matter/clusters.d.ts.map +0 -1
- package/dist/matter/clusters.js.map +0 -1
- package/dist/matter/devices.d.ts +0 -2
- package/dist/matter/devices.d.ts.map +0 -1
- package/dist/matter/devices.js.map +0 -1
- package/dist/matter/endpoints.d.ts +0 -2
- package/dist/matter/endpoints.d.ts.map +0 -1
- package/dist/matter/endpoints.js.map +0 -1
- package/dist/matter/export.d.ts +0 -5
- package/dist/matter/export.d.ts.map +0 -1
- package/dist/matter/export.js.map +0 -1
- package/dist/matter/types.d.ts +0 -3
- package/dist/matter/types.d.ts.map +0 -1
- package/dist/matter/types.js.map +0 -1
- package/dist/matterbridge.d.ts +0 -444
- package/dist/matterbridge.d.ts.map +0 -1
- package/dist/matterbridge.js.map +0 -1
- package/dist/matterbridgeAccessoryPlatform.d.ts +0 -42
- package/dist/matterbridgeAccessoryPlatform.d.ts.map +0 -1
- package/dist/matterbridgeAccessoryPlatform.js.map +0 -1
- package/dist/matterbridgeBehaviors.d.ts +0 -1340
- package/dist/matterbridgeBehaviors.d.ts.map +0 -1
- package/dist/matterbridgeBehaviors.js.map +0 -1
- package/dist/matterbridgeDeviceTypes.d.ts +0 -709
- package/dist/matterbridgeDeviceTypes.d.ts.map +0 -1
- package/dist/matterbridgeDeviceTypes.js.map +0 -1
- package/dist/matterbridgeDynamicPlatform.d.ts +0 -42
- package/dist/matterbridgeDynamicPlatform.d.ts.map +0 -1
- package/dist/matterbridgeDynamicPlatform.js.map +0 -1
- package/dist/matterbridgeEndpoint.d.ts +0 -1250
- package/dist/matterbridgeEndpoint.d.ts.map +0 -1
- package/dist/matterbridgeEndpoint.js.map +0 -1
- package/dist/matterbridgeEndpointHelpers.d.ts +0 -3198
- package/dist/matterbridgeEndpointHelpers.d.ts.map +0 -1
- package/dist/matterbridgeEndpointHelpers.js.map +0 -1
- package/dist/matterbridgePlatform.d.ts +0 -310
- package/dist/matterbridgePlatform.d.ts.map +0 -1
- package/dist/matterbridgePlatform.js.map +0 -1
- package/dist/matterbridgeTypes.d.ts +0 -195
- package/dist/matterbridgeTypes.d.ts.map +0 -1
- package/dist/matterbridgeTypes.js.map +0 -1
- package/dist/pluginManager.d.ts +0 -291
- package/dist/pluginManager.d.ts.map +0 -1
- package/dist/pluginManager.js.map +0 -1
- package/dist/shelly.d.ts +0 -174
- package/dist/shelly.d.ts.map +0 -1
- package/dist/shelly.js.map +0 -1
- package/dist/storage/export.d.ts +0 -2
- package/dist/storage/export.d.ts.map +0 -1
- package/dist/storage/export.js.map +0 -1
- package/dist/update.d.ts +0 -59
- package/dist/update.d.ts.map +0 -1
- package/dist/update.js.map +0 -1
- package/dist/utils/colorUtils.d.ts +0 -117
- package/dist/utils/colorUtils.d.ts.map +0 -1
- package/dist/utils/colorUtils.js.map +0 -1
- package/dist/utils/commandLine.d.ts +0 -59
- package/dist/utils/commandLine.d.ts.map +0 -1
- package/dist/utils/commandLine.js.map +0 -1
- package/dist/utils/copyDirectory.d.ts +0 -33
- package/dist/utils/copyDirectory.d.ts.map +0 -1
- package/dist/utils/copyDirectory.js.map +0 -1
- package/dist/utils/createDirectory.d.ts +0 -34
- package/dist/utils/createDirectory.d.ts.map +0 -1
- package/dist/utils/createDirectory.js.map +0 -1
- package/dist/utils/createZip.d.ts +0 -39
- package/dist/utils/createZip.d.ts.map +0 -1
- package/dist/utils/createZip.js.map +0 -1
- package/dist/utils/deepCopy.d.ts +0 -32
- package/dist/utils/deepCopy.d.ts.map +0 -1
- package/dist/utils/deepCopy.js.map +0 -1
- package/dist/utils/deepEqual.d.ts +0 -54
- package/dist/utils/deepEqual.d.ts.map +0 -1
- package/dist/utils/deepEqual.js.map +0 -1
- package/dist/utils/export.d.ts +0 -12
- package/dist/utils/export.d.ts.map +0 -1
- package/dist/utils/export.js.map +0 -1
- package/dist/utils/hex.d.ts +0 -49
- package/dist/utils/hex.d.ts.map +0 -1
- package/dist/utils/hex.js.map +0 -1
- package/dist/utils/isvalid.d.ts +0 -103
- package/dist/utils/isvalid.d.ts.map +0 -1
- package/dist/utils/isvalid.js.map +0 -1
- package/dist/utils/network.d.ts +0 -74
- package/dist/utils/network.d.ts.map +0 -1
- package/dist/utils/network.js.map +0 -1
- package/dist/utils/spawn.d.ts +0 -11
- package/dist/utils/spawn.d.ts.map +0 -1
- package/dist/utils/spawn.js.map +0 -1
- package/dist/utils/wait.d.ts +0 -56
- package/dist/utils/wait.d.ts.map +0 -1
- package/dist/utils/wait.js.map +0 -1
package/dist/pluginManager.js
CHANGED
|
@@ -1,42 +1,15 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This file contains the Plugins class.
|
|
3
|
-
*
|
|
4
|
-
* @file plugins.ts
|
|
5
|
-
* @author Luca Liguori
|
|
6
|
-
* @created 2024-07-14
|
|
7
|
-
* @version 1.1.2
|
|
8
|
-
* @license Apache-2.0
|
|
9
|
-
*
|
|
10
|
-
* Copyright 2024, 2025, 2026 Luca Liguori.
|
|
11
|
-
*
|
|
12
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
13
|
-
* you may not use this file except in compliance with the License.
|
|
14
|
-
* You may obtain a copy of the License at
|
|
15
|
-
*
|
|
16
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
17
|
-
*
|
|
18
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
19
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
20
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
21
|
-
* See the License for the specific language governing permissions and
|
|
22
|
-
* limitations under the License.
|
|
23
|
-
*/
|
|
24
|
-
// Node.js import
|
|
25
1
|
import EventEmitter from 'node:events';
|
|
26
|
-
// AnsiLogger module
|
|
27
2
|
import { AnsiLogger, UNDERLINE, UNDERLINEOFF, BLUE, db, er, nf, nt, rs, wr } from 'node-ansi-logger';
|
|
28
3
|
import { plg, typ } from './matterbridgeTypes.js';
|
|
4
|
+
import { logError } from './utils/error.js';
|
|
29
5
|
export class PluginManager extends EventEmitter {
|
|
30
6
|
_plugins = new Map();
|
|
31
|
-
nodeContext;
|
|
32
7
|
matterbridge;
|
|
33
8
|
log;
|
|
34
9
|
constructor(matterbridge) {
|
|
35
10
|
super();
|
|
36
11
|
this.matterbridge = matterbridge;
|
|
37
|
-
|
|
38
|
-
this.nodeContext = matterbridge.nodeContext;
|
|
39
|
-
this.log = new AnsiLogger({ logName: 'PluginManager', logTimestampFormat: 4 /* TimestampFormat.TIME_MILLIS */, logLevel: matterbridge.log.logLevel });
|
|
12
|
+
this.log = new AnsiLogger({ logName: 'PluginManager', logTimestampFormat: 4, logLevel: matterbridge.log.logLevel });
|
|
40
13
|
this.log.debug('Matterbridge plugin manager starting...');
|
|
41
14
|
}
|
|
42
15
|
get length() {
|
|
@@ -72,8 +45,7 @@ export class PluginManager extends EventEmitter {
|
|
|
72
45
|
await callback(plugin);
|
|
73
46
|
}
|
|
74
47
|
catch (err) {
|
|
75
|
-
this.log
|
|
76
|
-
// throw error;
|
|
48
|
+
logError(this.log, `Error processing forEach plugin ${plg}${plugin.name}${er}`, err);
|
|
77
49
|
}
|
|
78
50
|
});
|
|
79
51
|
await Promise.all(tasks);
|
|
@@ -81,31 +53,19 @@ export class PluginManager extends EventEmitter {
|
|
|
81
53
|
set logLevel(logLevel) {
|
|
82
54
|
this.log.logLevel = logLevel;
|
|
83
55
|
}
|
|
84
|
-
/**
|
|
85
|
-
* Loads registered plugins from storage.
|
|
86
|
-
*
|
|
87
|
-
* This method retrieves an array of registered plugins from the storage and converts it
|
|
88
|
-
* into a map where the plugin names are the keys and the plugin objects are the values.
|
|
89
|
-
*
|
|
90
|
-
* @returns {Promise<RegisteredPlugin[]>} A promise that resolves to an array of registered plugins.
|
|
91
|
-
*/
|
|
92
56
|
async loadFromStorage() {
|
|
93
|
-
|
|
94
|
-
|
|
57
|
+
if (!this.matterbridge.nodeContext) {
|
|
58
|
+
throw new Error('loadFromStorage: node context is not available.');
|
|
59
|
+
}
|
|
60
|
+
const pluginsArray = await this.matterbridge.nodeContext.get('plugins', []);
|
|
95
61
|
for (const plugin of pluginsArray)
|
|
96
62
|
this._plugins.set(plugin.name, plugin);
|
|
97
63
|
return pluginsArray;
|
|
98
64
|
}
|
|
99
|
-
/**
|
|
100
|
-
* Loads registered plugins from storage.
|
|
101
|
-
*
|
|
102
|
-
* This method retrieves an array of registered plugins from the storage and converts it
|
|
103
|
-
* into a map where the plugin names are the keys and the plugin objects are the values.
|
|
104
|
-
*
|
|
105
|
-
* @returns {Promise<RegisteredPlugin[]>} A promise that resolves to an array of registered plugins.
|
|
106
|
-
*/
|
|
107
65
|
async saveToStorage() {
|
|
108
|
-
|
|
66
|
+
if (!this.matterbridge.nodeContext) {
|
|
67
|
+
throw new Error('loadFromStorage: node context is not available.');
|
|
68
|
+
}
|
|
109
69
|
const plugins = [];
|
|
110
70
|
const pluginArrayFromMap = Array.from(this._plugins.values());
|
|
111
71
|
for (const plugin of pluginArrayFromMap) {
|
|
@@ -119,25 +79,17 @@ export class PluginManager extends EventEmitter {
|
|
|
119
79
|
enabled: plugin.enabled,
|
|
120
80
|
});
|
|
121
81
|
}
|
|
122
|
-
await this.nodeContext.set('plugins', plugins);
|
|
82
|
+
await this.matterbridge.nodeContext.set('plugins', plugins);
|
|
123
83
|
this.log.debug(`Saved ${BLUE}${plugins.length}${db} plugins to storage`);
|
|
124
84
|
return plugins.length;
|
|
125
85
|
}
|
|
126
|
-
/**
|
|
127
|
-
* Resolves the name of a plugin by loading and parsing its package.json file.
|
|
128
|
-
*
|
|
129
|
-
* @param {string} pluginPath - The path to the plugin or the path to the plugin's package.json file.
|
|
130
|
-
* @returns {Promise<string | null>} A promise that resolves to the path of the plugin's package.json file or null if it could not be resolved.
|
|
131
|
-
*/
|
|
132
86
|
async resolve(pluginPath) {
|
|
133
87
|
const { default: path } = await import('node:path');
|
|
134
88
|
const { promises } = await import('node:fs');
|
|
135
89
|
if (!pluginPath.endsWith('package.json'))
|
|
136
90
|
pluginPath = path.join(pluginPath, 'package.json');
|
|
137
|
-
// Resolve the package.json of the plugin
|
|
138
91
|
let packageJsonPath = path.resolve(pluginPath);
|
|
139
92
|
this.log.debug(`Resolving plugin path ${plg}${packageJsonPath}${db}`);
|
|
140
|
-
// Check if the package.json file exists
|
|
141
93
|
try {
|
|
142
94
|
await promises.access(packageJsonPath);
|
|
143
95
|
}
|
|
@@ -147,9 +99,7 @@ export class PluginManager extends EventEmitter {
|
|
|
147
99
|
this.log.debug(`Trying at ${plg}${packageJsonPath}${db}`);
|
|
148
100
|
}
|
|
149
101
|
try {
|
|
150
|
-
// Load the package.json of the plugin
|
|
151
102
|
const packageJson = JSON.parse(await promises.readFile(packageJsonPath, 'utf8'));
|
|
152
|
-
// Check for main issues
|
|
153
103
|
if (!packageJson.name) {
|
|
154
104
|
this.log.error(`Package.json name not found at ${packageJsonPath}`);
|
|
155
105
|
return null;
|
|
@@ -162,7 +112,6 @@ export class PluginManager extends EventEmitter {
|
|
|
162
112
|
this.log.error(`Plugin at ${packageJsonPath} has no main entrypoint in package.json`);
|
|
163
113
|
return null;
|
|
164
114
|
}
|
|
165
|
-
// Check for @project-chip and @matter packages in dependencies and devDependencies
|
|
166
115
|
const checkForProjectChipPackages = (dependencies) => {
|
|
167
116
|
return Object.keys(dependencies).filter((pkg) => pkg.startsWith('@project-chip') || pkg.startsWith('@matter'));
|
|
168
117
|
};
|
|
@@ -184,7 +133,6 @@ export class PluginManager extends EventEmitter {
|
|
|
184
133
|
this.log.error(`Please open an issue on the plugin repository to remove them.`);
|
|
185
134
|
return null;
|
|
186
135
|
}
|
|
187
|
-
// Check for matterbridge package in dependencies and devDependencies
|
|
188
136
|
const checkForMatterbridgePackage = (dependencies) => {
|
|
189
137
|
return Object.keys(dependencies).filter((pkg) => pkg === 'matterbridge');
|
|
190
138
|
};
|
|
@@ -210,16 +158,10 @@ export class PluginManager extends EventEmitter {
|
|
|
210
158
|
return packageJsonPath;
|
|
211
159
|
}
|
|
212
160
|
catch (err) {
|
|
213
|
-
this.log
|
|
161
|
+
logError(this.log, `Failed to resolve plugin path ${plg}${pluginPath}${er}`, err);
|
|
214
162
|
return null;
|
|
215
163
|
}
|
|
216
164
|
}
|
|
217
|
-
/**
|
|
218
|
-
* Get the author of a plugin from its package.json.
|
|
219
|
-
*
|
|
220
|
-
* @param {Record<string, string | number | Record<string, string | number | object>>} packageJson - The package.json object of the plugin.
|
|
221
|
-
* @returns {string} The author of the plugin, or 'Unknown author' if not found.
|
|
222
|
-
*/
|
|
223
165
|
getAuthor(packageJson) {
|
|
224
166
|
if (packageJson.author && typeof packageJson.author === 'string')
|
|
225
167
|
return packageJson.author;
|
|
@@ -227,12 +169,6 @@ export class PluginManager extends EventEmitter {
|
|
|
227
169
|
return packageJson.author.name;
|
|
228
170
|
return 'Unknown author';
|
|
229
171
|
}
|
|
230
|
-
/**
|
|
231
|
-
* Get the homepage of a plugin from its package.json.
|
|
232
|
-
*
|
|
233
|
-
* @param {Record<string, string | number | Record<string, string | number | object>>} packageJson - The package.json object of the plugin.
|
|
234
|
-
* @returns {string | undefined} The homepage of the plugin, or undefined if not found.
|
|
235
|
-
*/
|
|
236
172
|
getHomepage(packageJson) {
|
|
237
173
|
if (packageJson.homepage && typeof packageJson.homepage === 'string' && packageJson.homepage.includes('http')) {
|
|
238
174
|
return packageJson.homepage.replace('git+', '').replace('.git', '');
|
|
@@ -241,14 +177,7 @@ export class PluginManager extends EventEmitter {
|
|
|
241
177
|
return packageJson.repository.url.replace('git+', '').replace('.git', '');
|
|
242
178
|
}
|
|
243
179
|
}
|
|
244
|
-
/**
|
|
245
|
-
* Get the help URL of a plugin from its package.json.
|
|
246
|
-
*
|
|
247
|
-
* @param {Record<string, string | number | Record<string, string | number | object>>} packageJson - The package.json object of the plugin.
|
|
248
|
-
* @returns {string | undefined} The URL to the help page or to the README file, or undefined if not found.
|
|
249
|
-
*/
|
|
250
180
|
getHelp(packageJson) {
|
|
251
|
-
// If there's a help field that looks like a URL, return it.
|
|
252
181
|
if (packageJson.help && typeof packageJson.help === 'string' && packageJson.help.startsWith('http')) {
|
|
253
182
|
return packageJson.help;
|
|
254
183
|
}
|
|
@@ -259,14 +188,7 @@ export class PluginManager extends EventEmitter {
|
|
|
259
188
|
return packageJson.homepage.replace('git+', '').replace('.git', '');
|
|
260
189
|
}
|
|
261
190
|
}
|
|
262
|
-
/**
|
|
263
|
-
* Get the changelog URL of a plugin from its package.json.
|
|
264
|
-
*
|
|
265
|
-
* @param {Record<string, string | number | Record<string, string | number | object>>} packageJson - The package.json object of the plugin.
|
|
266
|
-
* @returns {string | undefined} The URL to the CHANGELOG file, or undefined if not found.
|
|
267
|
-
*/
|
|
268
191
|
getChangelog(packageJson) {
|
|
269
|
-
// If there's a changelog field that looks like a URL, return it.
|
|
270
192
|
if (packageJson.changelog && typeof packageJson.changelog === 'string' && packageJson.changelog.startsWith('http')) {
|
|
271
193
|
return packageJson.changelog;
|
|
272
194
|
}
|
|
@@ -277,13 +199,6 @@ export class PluginManager extends EventEmitter {
|
|
|
277
199
|
return packageJson.homepage.replace('git+', '').replace('.git', '');
|
|
278
200
|
}
|
|
279
201
|
}
|
|
280
|
-
/**
|
|
281
|
-
* Get the first funding URL(s) of a plugin from its package.json.
|
|
282
|
-
*
|
|
283
|
-
* @param {Record<string, any>} packageJson - The package.json object of the plugin.
|
|
284
|
-
* @returns {string | undefined} The first funding URLs, or undefined if not found.
|
|
285
|
-
*/
|
|
286
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
287
202
|
getFunding(packageJson) {
|
|
288
203
|
const funding = packageJson.funding;
|
|
289
204
|
if (!funding)
|
|
@@ -292,25 +207,16 @@ export class PluginManager extends EventEmitter {
|
|
|
292
207
|
return;
|
|
293
208
|
if (typeof funding === 'string' && funding.startsWith('http'))
|
|
294
209
|
return funding;
|
|
295
|
-
// Normalize funding into an array.
|
|
296
210
|
const fundingEntries = Array.isArray(funding) ? funding : [funding];
|
|
297
211
|
for (const entry of fundingEntries) {
|
|
298
212
|
if (entry && typeof entry === 'string' && entry.startsWith('http')) {
|
|
299
|
-
// If the funding entry is a string, assume it is a URL.
|
|
300
213
|
return entry;
|
|
301
214
|
}
|
|
302
215
|
else if (entry && typeof entry === 'object' && typeof entry.url === 'string' && entry.url.startsWith('http')) {
|
|
303
|
-
// If it's an object with a 'url' property, use that.
|
|
304
216
|
return entry.url;
|
|
305
217
|
}
|
|
306
218
|
}
|
|
307
219
|
}
|
|
308
|
-
/**
|
|
309
|
-
* Loads and parse the plugin package.json and returns it.
|
|
310
|
-
*
|
|
311
|
-
* @param {RegisteredPlugin} plugin - The plugin to load the package from.
|
|
312
|
-
* @returns {Promise<Record<string, string | number | object> | null>} A promise that resolves to the parsed package.json object or null if it could not be parsed.
|
|
313
|
-
*/
|
|
314
220
|
async parse(plugin) {
|
|
315
221
|
const { promises } = await import('node:fs');
|
|
316
222
|
try {
|
|
@@ -340,7 +246,6 @@ export class PluginManager extends EventEmitter {
|
|
|
340
246
|
plugin.funding = this.getFunding(packageJson);
|
|
341
247
|
if (!plugin.type)
|
|
342
248
|
this.log.warn(`Plugin ${plg}${plugin.name}${wr} has no type`);
|
|
343
|
-
// Check for @project-chip and @matter packages in dependencies and devDependencies
|
|
344
249
|
const checkForProjectChipPackages = (dependencies) => {
|
|
345
250
|
return Object.keys(dependencies).filter((pkg) => pkg.startsWith('@project-chip') || pkg.startsWith('@matter'));
|
|
346
251
|
};
|
|
@@ -362,7 +267,6 @@ export class PluginManager extends EventEmitter {
|
|
|
362
267
|
this.log.error(`Please open an issue on the plugin repository to remove them.`);
|
|
363
268
|
return null;
|
|
364
269
|
}
|
|
365
|
-
// Check for matterbridge package in dependencies and devDependencies
|
|
366
270
|
const checkForMatterbridgePackage = (dependencies) => {
|
|
367
271
|
return Object.keys(dependencies).filter((pkg) => pkg === 'matterbridge');
|
|
368
272
|
};
|
|
@@ -387,21 +291,11 @@ export class PluginManager extends EventEmitter {
|
|
|
387
291
|
return packageJson;
|
|
388
292
|
}
|
|
389
293
|
catch (err) {
|
|
390
|
-
this.log
|
|
294
|
+
logError(this.log, `Failed to parse package.json of plugin ${plg}${plugin.name}${er}`, err);
|
|
391
295
|
plugin.error = true;
|
|
392
296
|
return null;
|
|
393
297
|
}
|
|
394
298
|
}
|
|
395
|
-
/**
|
|
396
|
-
* Enables a plugin by its name or path.
|
|
397
|
-
*
|
|
398
|
-
* This method enables a plugin by setting its `enabled` property to `true` and saving the updated
|
|
399
|
-
* plugin information to storage. It first checks if the plugin is already registered in the `_plugins` map.
|
|
400
|
-
* If not, it attempts to resolve the plugin's `package.json` file to retrieve its name and enable it.
|
|
401
|
-
*
|
|
402
|
-
* @param {string} nameOrPath - The name or path of the plugin to enable.
|
|
403
|
-
* @returns {Promise<RegisteredPlugin | null>} A promise that resolves to the enabled plugin object, or null if the plugin could not be enabled.
|
|
404
|
-
*/
|
|
405
299
|
async enable(nameOrPath) {
|
|
406
300
|
const { promises } = await import('node:fs');
|
|
407
301
|
if (!nameOrPath)
|
|
@@ -433,20 +327,10 @@ export class PluginManager extends EventEmitter {
|
|
|
433
327
|
return plugin;
|
|
434
328
|
}
|
|
435
329
|
catch (err) {
|
|
436
|
-
this.log
|
|
330
|
+
logError(this.log, `Failed to parse package.json of plugin ${plg}${nameOrPath}${er}`, err);
|
|
437
331
|
return null;
|
|
438
332
|
}
|
|
439
333
|
}
|
|
440
|
-
/**
|
|
441
|
-
* Enables a plugin by its name or path.
|
|
442
|
-
*
|
|
443
|
-
* This method enables a plugin by setting its `enabled` property to `true` and saving the updated
|
|
444
|
-
* plugin information to storage. It first checks if the plugin is already registered in the `_plugins` map.
|
|
445
|
-
* If not, it attempts to resolve the plugin's `package.json` file to retrieve its name and enable it.
|
|
446
|
-
*
|
|
447
|
-
* @param {string} nameOrPath - The name or path of the plugin to enable.
|
|
448
|
-
* @returns {Promise<RegisteredPlugin | null>} A promise that resolves to the enabled plugin object, or null if the plugin could not be enabled.
|
|
449
|
-
*/
|
|
450
334
|
async disable(nameOrPath) {
|
|
451
335
|
const { promises } = await import('node:fs');
|
|
452
336
|
if (!nameOrPath)
|
|
@@ -478,20 +362,10 @@ export class PluginManager extends EventEmitter {
|
|
|
478
362
|
return plugin;
|
|
479
363
|
}
|
|
480
364
|
catch (err) {
|
|
481
|
-
this.log
|
|
365
|
+
logError(this.log, `Failed to parse package.json of plugin ${plg}${nameOrPath}${er}`, err);
|
|
482
366
|
return null;
|
|
483
367
|
}
|
|
484
368
|
}
|
|
485
|
-
/**
|
|
486
|
-
* Removes a plugin by its name or path.
|
|
487
|
-
*
|
|
488
|
-
* This method removes a plugin from the `_plugins` map and saves the updated plugin information to storage.
|
|
489
|
-
* It first checks if the plugin is already registered in the `_plugins` map. If not, it attempts to resolve
|
|
490
|
-
* the plugin's `package.json` file to retrieve its name and remove it.
|
|
491
|
-
*
|
|
492
|
-
* @param {string} nameOrPath - The name or path of the plugin to remove.
|
|
493
|
-
* @returns {Promise<RegisteredPlugin | null>} A promise that resolves to the removed plugin object, or null if the plugin could not be removed.
|
|
494
|
-
*/
|
|
495
369
|
async remove(nameOrPath) {
|
|
496
370
|
const { promises } = await import('node:fs');
|
|
497
371
|
if (!nameOrPath)
|
|
@@ -523,21 +397,10 @@ export class PluginManager extends EventEmitter {
|
|
|
523
397
|
return plugin;
|
|
524
398
|
}
|
|
525
399
|
catch (err) {
|
|
526
|
-
this.log
|
|
400
|
+
logError(this.log, `Failed to parse package.json of plugin ${plg}${nameOrPath}${er}`, err);
|
|
527
401
|
return null;
|
|
528
402
|
}
|
|
529
403
|
}
|
|
530
|
-
/**
|
|
531
|
-
* Adds a plugin by its name or path.
|
|
532
|
-
*
|
|
533
|
-
* This method adds a plugin to the plugins map and saves the updated plugin information to storage.
|
|
534
|
-
* It first resolves the plugin's `package.json` file to retrieve its details. If the plugin is already
|
|
535
|
-
* registered, it logs an info message and returns null. Otherwise, it registers the plugin, enables it,
|
|
536
|
-
* and saves the updated plugin information to storage.
|
|
537
|
-
*
|
|
538
|
-
* @param {string} nameOrPath - The name or path of the plugin to add.
|
|
539
|
-
* @returns {Promise<RegisteredPlugin | null>} A promise that resolves to the added plugin object, or null if the plugin could not be added.
|
|
540
|
-
*/
|
|
541
404
|
async add(nameOrPath) {
|
|
542
405
|
const { promises } = await import('node:fs');
|
|
543
406
|
if (!nameOrPath)
|
|
@@ -569,94 +432,10 @@ export class PluginManager extends EventEmitter {
|
|
|
569
432
|
return plugin || null;
|
|
570
433
|
}
|
|
571
434
|
catch (err) {
|
|
572
|
-
this.log
|
|
435
|
+
logError(this.log, `Failed to parse package.json of plugin ${plg}${nameOrPath}${er}`, err);
|
|
573
436
|
return null;
|
|
574
437
|
}
|
|
575
438
|
}
|
|
576
|
-
/**
|
|
577
|
-
* Installs a plugin by its name.
|
|
578
|
-
*
|
|
579
|
-
* This method first uninstalls any existing version of the plugin, then installs the plugin globally using npm.
|
|
580
|
-
* It logs the installation process and retrieves the installed version of the plugin.
|
|
581
|
-
*
|
|
582
|
-
* @param {string} name - The name of the plugin to install.
|
|
583
|
-
* @returns {Promise<string | undefined>} A promise that resolves to the installed version of the plugin, or undefined if the installation failed.
|
|
584
|
-
*/
|
|
585
|
-
async install(name) {
|
|
586
|
-
const { exec } = await import('node:child_process');
|
|
587
|
-
await this.uninstall(name);
|
|
588
|
-
this.log.info(`Installing plugin ${plg}${name}${nf}`);
|
|
589
|
-
return new Promise((resolve) => {
|
|
590
|
-
exec(`npm install -g ${name} --omit=dev`, (error, stdout, stderr) => {
|
|
591
|
-
if (error) {
|
|
592
|
-
this.log.error(`Failed to install plugin ${plg}${name}${er}: ${error}`);
|
|
593
|
-
this.log.debug(`Failed to install plugin ${plg}${name}${db}: ${stderr}`);
|
|
594
|
-
resolve(undefined);
|
|
595
|
-
}
|
|
596
|
-
else {
|
|
597
|
-
this.log.info(`Installed plugin ${plg}${name}${nf}`);
|
|
598
|
-
this.log.debug(`Installed plugin ${plg}${name}${db}: ${stdout}`);
|
|
599
|
-
// Get the installed version
|
|
600
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
601
|
-
exec(`npm list -g ${name} --depth=0`, (listError, listStdout, listStderr) => {
|
|
602
|
-
if (listError) {
|
|
603
|
-
this.log.error(`List error: ${listError}`);
|
|
604
|
-
resolve(undefined);
|
|
605
|
-
}
|
|
606
|
-
// Clean the output to get only the package name and version
|
|
607
|
-
const lines = listStdout.split('\n');
|
|
608
|
-
const versionLine = lines.find((line) => line.includes(`${name}@`));
|
|
609
|
-
if (versionLine) {
|
|
610
|
-
const version = versionLine.split('@')[1].trim();
|
|
611
|
-
this.log.info(`Installed plugin ${plg}${name}@${version}${nf}`);
|
|
612
|
-
this.emit('installed', name, version);
|
|
613
|
-
resolve(version);
|
|
614
|
-
}
|
|
615
|
-
else {
|
|
616
|
-
resolve(undefined);
|
|
617
|
-
}
|
|
618
|
-
});
|
|
619
|
-
}
|
|
620
|
-
});
|
|
621
|
-
});
|
|
622
|
-
}
|
|
623
|
-
/**
|
|
624
|
-
* Uninstalls a plugin by its name.
|
|
625
|
-
*
|
|
626
|
-
* This method uninstalls a globally installed plugin using npm. It logs the uninstallation process
|
|
627
|
-
* and returns the name of the uninstalled plugin if successful, or undefined if the uninstallation failed.
|
|
628
|
-
*
|
|
629
|
-
* @param {string} name - The name of the plugin to uninstall.
|
|
630
|
-
* @returns {Promise<string | undefined>} A promise that resolves to the name of the uninstalled plugin, or undefined if the uninstallation failed.
|
|
631
|
-
*/
|
|
632
|
-
async uninstall(name) {
|
|
633
|
-
const { exec } = await import('node:child_process');
|
|
634
|
-
this.log.info(`Uninstalling plugin ${plg}${name}${nf}`);
|
|
635
|
-
return new Promise((resolve) => {
|
|
636
|
-
exec(`npm uninstall -g ${name}`, (error, stdout, stderr) => {
|
|
637
|
-
if (error) {
|
|
638
|
-
this.log.error(`Failed to uninstall plugin ${plg}${name}${er}: ${error}`);
|
|
639
|
-
this.log.debug(`Failed to uninstall plugin ${plg}${name}${db}: ${stderr}`);
|
|
640
|
-
resolve(undefined);
|
|
641
|
-
}
|
|
642
|
-
else {
|
|
643
|
-
this.log.info(`Uninstalled plugin ${plg}${name}${nf}`);
|
|
644
|
-
this.log.debug(`Uninstalled plugin ${plg}${name}${db}: ${stdout}`);
|
|
645
|
-
this.emit('uninstalled', name);
|
|
646
|
-
resolve(name);
|
|
647
|
-
}
|
|
648
|
-
});
|
|
649
|
-
});
|
|
650
|
-
}
|
|
651
|
-
/**
|
|
652
|
-
* Loads a plugin and returns the corresponding MatterbridgePlatform instance.
|
|
653
|
-
*
|
|
654
|
-
* @param {RegisteredPlugin} plugin - The plugin to load.
|
|
655
|
-
* @param {boolean} start - Optional flag indicating whether to start the plugin after loading. Default is false.
|
|
656
|
-
* @param {string} message - Optional message to pass to the plugin when starting.
|
|
657
|
-
* @param {boolean} configure - Optional flag indicating whether to configure the plugin after loading. Default is false.
|
|
658
|
-
* @returns {Promise<MatterbridgePlatform | undefined>} A Promise that resolves to the loaded MatterbridgePlatform instance or undefined.
|
|
659
|
-
*/
|
|
660
439
|
async load(plugin, start = false, message = '', configure = false) {
|
|
661
440
|
const { promises } = await import('node:fs');
|
|
662
441
|
const { default: path } = await import('node:path');
|
|
@@ -670,20 +449,15 @@ export class PluginManager extends EventEmitter {
|
|
|
670
449
|
}
|
|
671
450
|
this.log.info(`Loading plugin ${plg}${plugin.name}${nf} type ${typ}${plugin.type}${nf}`);
|
|
672
451
|
try {
|
|
673
|
-
// Load the package.json of the plugin
|
|
674
452
|
const packageJson = JSON.parse(await promises.readFile(plugin.path, 'utf8'));
|
|
675
|
-
// Resolve the main module path relative to package.json
|
|
676
453
|
const pluginEntry = path.resolve(path.dirname(plugin.path), packageJson.main);
|
|
677
|
-
// Dynamically import the plugin
|
|
678
454
|
const { pathToFileURL } = await import('node:url');
|
|
679
455
|
const pluginUrl = pathToFileURL(pluginEntry);
|
|
680
456
|
this.log.debug(`Importing plugin ${plg}${plugin.name}${db} from ${pluginUrl.href}`);
|
|
681
457
|
const pluginInstance = await import(pluginUrl.href);
|
|
682
458
|
this.log.debug(`Imported plugin ${plg}${plugin.name}${db} from ${pluginUrl.href}`);
|
|
683
|
-
// Call the default export function of the plugin, passing this MatterBridge instance, the log and the config
|
|
684
459
|
if (pluginInstance.default) {
|
|
685
460
|
const config = await this.loadConfig(plugin);
|
|
686
|
-
// Preset the plugin properties here in case the plugin throws an error during loading. In this case the user can change the config and restart the plugin.
|
|
687
461
|
plugin.name = packageJson.name;
|
|
688
462
|
plugin.description = packageJson.description ?? 'No description';
|
|
689
463
|
plugin.version = packageJson.version;
|
|
@@ -692,7 +466,7 @@ export class PluginManager extends EventEmitter {
|
|
|
692
466
|
plugin.schemaJson = await this.loadSchema(plugin);
|
|
693
467
|
config.name = plugin.name;
|
|
694
468
|
config.version = packageJson.version;
|
|
695
|
-
const log = new AnsiLogger({ logName: plugin.description
|
|
469
|
+
const log = new AnsiLogger({ logName: plugin.description, logTimestampFormat: 4, logLevel: config.debug ? "debug" : this.matterbridge.log.logLevel });
|
|
696
470
|
const platform = pluginInstance.default(this.matterbridge, log, config);
|
|
697
471
|
config.type = platform.type;
|
|
698
472
|
platform.name = packageJson.name;
|
|
@@ -711,7 +485,7 @@ export class PluginManager extends EventEmitter {
|
|
|
711
485
|
plugin.loaded = true;
|
|
712
486
|
plugin.registeredDevices = 0;
|
|
713
487
|
plugin.addedDevices = 0;
|
|
714
|
-
await this.saveToStorage();
|
|
488
|
+
await this.saveToStorage();
|
|
715
489
|
this.log.notice(`Loaded plugin ${plg}${plugin.name}${nt} type ${typ}${platform.type}${nt} (entrypoint ${UNDERLINE}${pluginEntry}${UNDERLINEOFF})`);
|
|
716
490
|
this.emit('loaded', plugin.name);
|
|
717
491
|
if (start)
|
|
@@ -726,19 +500,11 @@ export class PluginManager extends EventEmitter {
|
|
|
726
500
|
}
|
|
727
501
|
}
|
|
728
502
|
catch (err) {
|
|
729
|
-
this.log
|
|
503
|
+
logError(this.log, `Failed to load plugin ${plg}${plugin.name}${er}`, err);
|
|
730
504
|
plugin.error = true;
|
|
731
505
|
}
|
|
732
506
|
return undefined;
|
|
733
507
|
}
|
|
734
|
-
/**
|
|
735
|
-
* Starts a plugin.
|
|
736
|
-
*
|
|
737
|
-
* @param {RegisteredPlugin} plugin - The plugin to start.
|
|
738
|
-
* @param {string} [message] - Optional message to pass to the plugin's onStart method.
|
|
739
|
-
* @param {boolean} [configure] - Indicates whether to configure the plugin after starting (default false).
|
|
740
|
-
* @returns {Promise<RegisteredPlugin | undefined>} A promise that resolves when the plugin is started successfully, or rejects with an error if starting the plugin fails.
|
|
741
|
-
*/
|
|
742
508
|
async start(plugin, message, configure = false) {
|
|
743
509
|
if (!plugin.loaded) {
|
|
744
510
|
this.log.error(`Plugin ${plg}${plugin.name}${er} not loaded`);
|
|
@@ -765,16 +531,10 @@ export class PluginManager extends EventEmitter {
|
|
|
765
531
|
}
|
|
766
532
|
catch (err) {
|
|
767
533
|
plugin.error = true;
|
|
768
|
-
this.log
|
|
534
|
+
logError(this.log, `Failed to start plugin ${plg}${plugin.name}${er}`, err);
|
|
769
535
|
}
|
|
770
536
|
return undefined;
|
|
771
537
|
}
|
|
772
|
-
/**
|
|
773
|
-
* Configures a plugin.
|
|
774
|
-
*
|
|
775
|
-
* @param {RegisteredPlugin} plugin - The plugin to configure.
|
|
776
|
-
* @returns {Promise<RegisteredPlugin | undefined>} A promise that resolves when the plugin is configured successfully, or rejects with an error if configuration fails.
|
|
777
|
-
*/
|
|
778
538
|
async configure(plugin) {
|
|
779
539
|
if (!plugin.loaded) {
|
|
780
540
|
this.log.error(`Plugin ${plg}${plugin.name}${er} not loaded`);
|
|
@@ -802,22 +562,10 @@ export class PluginManager extends EventEmitter {
|
|
|
802
562
|
}
|
|
803
563
|
catch (err) {
|
|
804
564
|
plugin.error = true;
|
|
805
|
-
this.log
|
|
565
|
+
logError(this.log, `Failed to configure plugin ${plg}${plugin.name}${er}`, err);
|
|
806
566
|
}
|
|
807
567
|
return undefined;
|
|
808
568
|
}
|
|
809
|
-
/**
|
|
810
|
-
* Shuts down a plugin.
|
|
811
|
-
*
|
|
812
|
-
* This method shuts down a plugin by calling its `onShutdown` method and resetting its state.
|
|
813
|
-
* It logs the shutdown process and optionally removes all devices associated with the plugin.
|
|
814
|
-
*
|
|
815
|
-
* @param {RegisteredPlugin} plugin - The plugin to shut down.
|
|
816
|
-
* @param {string} [reason] - The reason for shutting down the plugin.
|
|
817
|
-
* @param {boolean} [removeAllDevices] - Whether to remove all devices associated with the plugin.
|
|
818
|
-
* @param {boolean} [force] - Whether to force the shutdown even if the plugin is not loaded or started.
|
|
819
|
-
* @returns {Promise<RegisteredPlugin | undefined>} A promise that resolves to the shut down plugin object, or undefined if the shutdown failed.
|
|
820
|
-
*/
|
|
821
569
|
async shutdown(plugin, reason, removeAllDevices = false, force = false) {
|
|
822
570
|
this.log.debug(`Shutting down plugin ${plg}${plugin.name}${db}`);
|
|
823
571
|
if (!plugin.loaded) {
|
|
@@ -857,31 +605,21 @@ export class PluginManager extends EventEmitter {
|
|
|
857
605
|
return plugin;
|
|
858
606
|
}
|
|
859
607
|
catch (err) {
|
|
860
|
-
this.log
|
|
608
|
+
logError(this.log, `Failed to shut down plugin ${plg}${plugin.name}${er}`, err);
|
|
861
609
|
}
|
|
862
610
|
return undefined;
|
|
863
611
|
}
|
|
864
|
-
/**
|
|
865
|
-
* Loads the configuration for a plugin.
|
|
866
|
-
* If the configuration file exists, it reads the file and returns the parsed JSON data.
|
|
867
|
-
* If the configuration file does not exist, it creates a new file with default configuration and returns it.
|
|
868
|
-
* If any error occurs during file access or creation, it logs an error and return un empty config.
|
|
869
|
-
*
|
|
870
|
-
* @param {RegisteredPlugin} plugin - The plugin for which to load the configuration.
|
|
871
|
-
* @returns {Promise<PlatformConfig>} A promise that resolves to the loaded or created configuration.
|
|
872
|
-
*/
|
|
873
612
|
async loadConfig(plugin) {
|
|
874
613
|
const { default: path } = await import('node:path');
|
|
875
614
|
const { promises } = await import('node:fs');
|
|
876
615
|
const { shelly_config, somfytahoma_config, zigbee2mqtt_config } = await import('./defaultConfigSchema.js');
|
|
877
616
|
const configFile = path.join(this.matterbridge.matterbridgeDirectory, `${plugin.name}.config.json`);
|
|
617
|
+
const defaultConfigFile = plugin.path.replace('package.json', `${plugin.name}.config.json`);
|
|
878
618
|
try {
|
|
879
619
|
await promises.access(configFile);
|
|
880
620
|
const data = await promises.readFile(configFile, 'utf8');
|
|
881
621
|
const config = JSON.parse(data);
|
|
882
622
|
this.log.debug(`Loaded config file ${configFile} for plugin ${plg}${plugin.name}${db}.`);
|
|
883
|
-
// this.log.debug(`Loaded config file ${configFile} for plugin ${plg}${plugin.name}${db}.\nConfig:${rs}\n`, config);
|
|
884
|
-
// The first time a plugin is added to the system, the config file is created with the plugin name and type "AnyPlatform".
|
|
885
623
|
config.name = plugin.name;
|
|
886
624
|
config.type = plugin.type;
|
|
887
625
|
if (config.debug === undefined)
|
|
@@ -893,45 +631,41 @@ export class PluginManager extends EventEmitter {
|
|
|
893
631
|
catch (err) {
|
|
894
632
|
const nodeErr = err;
|
|
895
633
|
if (nodeErr.code === 'ENOENT') {
|
|
634
|
+
this.log.debug(`Config file ${configFile} for plugin ${plg}${plugin.name}${db} does not exist, creating new config file...`);
|
|
896
635
|
let config;
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
config =
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
config
|
|
636
|
+
try {
|
|
637
|
+
await promises.access(defaultConfigFile);
|
|
638
|
+
const data = await promises.readFile(defaultConfigFile, 'utf8');
|
|
639
|
+
config = JSON.parse(data);
|
|
640
|
+
this.log.debug(`Loaded default config file ${defaultConfigFile} for plugin ${plg}${plugin.name}${db}.`);
|
|
641
|
+
}
|
|
642
|
+
catch (_err) {
|
|
643
|
+
this.log.debug(`Default config file ${defaultConfigFile} for plugin ${plg}${plugin.name}${db} does not exist, creating new config file...`);
|
|
644
|
+
if (plugin.name === 'matterbridge-zigbee2mqtt')
|
|
645
|
+
config = zigbee2mqtt_config;
|
|
646
|
+
else if (plugin.name === 'matterbridge-somfy-tahoma')
|
|
647
|
+
config = somfytahoma_config;
|
|
648
|
+
else if (plugin.name === 'matterbridge-shelly')
|
|
649
|
+
config = shelly_config;
|
|
650
|
+
else
|
|
651
|
+
config = { name: plugin.name, type: plugin.type, debug: false, unregisterOnShutdown: false };
|
|
652
|
+
}
|
|
905
653
|
try {
|
|
906
654
|
await promises.writeFile(configFile, JSON.stringify(config, null, 2), 'utf8');
|
|
907
655
|
this.log.debug(`Created config file ${configFile} for plugin ${plg}${plugin.name}${db}.`);
|
|
908
|
-
// this.log.debug(`Created config file ${configFile} for plugin ${plg}${plugin.name}${db}.\nConfig:${rs}\n`, config);
|
|
909
656
|
return config;
|
|
910
657
|
}
|
|
911
658
|
catch (err) {
|
|
912
|
-
this.log
|
|
659
|
+
logError(this.log, `Error creating config file ${configFile} for plugin ${plg}${plugin.name}${er}`, err);
|
|
913
660
|
return config;
|
|
914
661
|
}
|
|
915
662
|
}
|
|
916
663
|
else {
|
|
917
|
-
this.log
|
|
664
|
+
logError(this.log, `Error accessing config file ${configFile} for plugin ${plg}${plugin.name}${er}`, err);
|
|
918
665
|
return { name: plugin.name, type: plugin.type, debug: false, unregisterOnShutdown: false };
|
|
919
666
|
}
|
|
920
667
|
}
|
|
921
668
|
}
|
|
922
|
-
/**
|
|
923
|
-
* Saves the configuration of a plugin to a file.
|
|
924
|
-
*
|
|
925
|
-
* This method saves the configuration of the specified plugin to a JSON file in the matterbridge directory.
|
|
926
|
-
* If the plugin's configuration is not found, it logs an error and rejects the promise. If the configuration
|
|
927
|
-
* is successfully saved, it logs a debug message. If an error occurs during the file write operation, it logs
|
|
928
|
-
* the error and rejects the promise.
|
|
929
|
-
*
|
|
930
|
-
* @param {RegisteredPlugin} plugin - The plugin whose configuration is to be saved.
|
|
931
|
-
* @param {boolean} [restartRequired] - Indicates whether a restart is required after saving the configuration.
|
|
932
|
-
* @returns {Promise<void>} A promise that resolves when the configuration is successfully saved, or rejects if an error occurs.
|
|
933
|
-
* @throws {Error} If the plugin's configuration is not found.
|
|
934
|
-
*/
|
|
935
669
|
async saveConfigFromPlugin(plugin, restartRequired = false) {
|
|
936
670
|
const { default: path } = await import('node:path');
|
|
937
671
|
const { promises } = await import('node:fs');
|
|
@@ -946,28 +680,13 @@ export class PluginManager extends EventEmitter {
|
|
|
946
680
|
if (restartRequired)
|
|
947
681
|
plugin.restartRequired = true;
|
|
948
682
|
this.log.debug(`Saved config file ${configFile} for plugin ${plg}${plugin.name}${db}`);
|
|
949
|
-
// this.log.debug(`Saved config file ${configFile} for plugin ${plg}${plugin.name}${db}.\nConfig:${rs}\n`, plugin.platform.config);
|
|
950
683
|
return Promise.resolve();
|
|
951
684
|
}
|
|
952
685
|
catch (err) {
|
|
953
|
-
this.log
|
|
686
|
+
logError(this.log, `Error saving config file ${configFile} for plugin ${plg}${plugin.name}${er}`, err);
|
|
954
687
|
return Promise.reject(err);
|
|
955
688
|
}
|
|
956
689
|
}
|
|
957
|
-
/**
|
|
958
|
-
* Saves the configuration of a plugin from a JSON object to a file.
|
|
959
|
-
*
|
|
960
|
-
* This method saves the provided configuration of the specified plugin to a JSON file in the matterbridge directory.
|
|
961
|
-
* It first checks if the configuration data is valid by ensuring it contains the correct name and type, and matches
|
|
962
|
-
* the plugin's name. If the configuration data is invalid, it logs an error and returns. If the configuration is
|
|
963
|
-
* successfully saved, it updates the plugin's `configJson` property and logs a debug message. If an error occurs
|
|
964
|
-
* during the file write operation, it logs the error and returns.
|
|
965
|
-
*
|
|
966
|
-
* @param {RegisteredPlugin} plugin - The plugin whose configuration is to be saved.
|
|
967
|
-
* @param {PlatformConfig} config - The configuration data to be saved.
|
|
968
|
-
* @param {boolean} [restartRequired] - Indicates whether a restart is required after saving the configuration.
|
|
969
|
-
* @returns {Promise<void>} A promise that resolves when the configuration is successfully saved, or returns if an error occurs.
|
|
970
|
-
*/
|
|
971
690
|
async saveConfigFromJson(plugin, config, restartRequired = false) {
|
|
972
691
|
const { default: path } = await import('node:path');
|
|
973
692
|
const { promises } = await import('node:fs');
|
|
@@ -986,23 +705,12 @@ export class PluginManager extends EventEmitter {
|
|
|
986
705
|
plugin.platform.onConfigChanged(config).catch((err) => this.log.error(`Error calling onConfigChanged for plugin ${plg}${plugin.name}${er}: ${err}`));
|
|
987
706
|
}
|
|
988
707
|
this.log.debug(`Saved config file ${configFile} for plugin ${plg}${plugin.name}${db}`);
|
|
989
|
-
// this.log.debug(`Saved config file ${configFile} for plugin ${plg}${plugin.name}${db}.\nConfig:${rs}\n`, config);
|
|
990
708
|
}
|
|
991
709
|
catch (err) {
|
|
992
|
-
this.log
|
|
710
|
+
logError(this.log, `Error saving config file ${configFile} for plugin ${plg}${plugin.name}${er}`, err);
|
|
993
711
|
return;
|
|
994
712
|
}
|
|
995
713
|
}
|
|
996
|
-
/**
|
|
997
|
-
* Loads the schema for a plugin.
|
|
998
|
-
*
|
|
999
|
-
* This method attempts to load the schema file for the specified plugin. If the schema file is found,
|
|
1000
|
-
* it reads and parses the file, updates the schema's title and description, and logs the process.
|
|
1001
|
-
* If the schema file is not found, it logs the event and loads a default schema for the plugin.
|
|
1002
|
-
*
|
|
1003
|
-
* @param {RegisteredPlugin} plugin - The plugin whose schema is to be loaded.
|
|
1004
|
-
* @returns {Promise<PlatformSchema>} A promise that resolves to the loaded schema object, or the default schema if the schema file is not found.
|
|
1005
|
-
*/
|
|
1006
714
|
async loadSchema(plugin) {
|
|
1007
715
|
const { promises } = await import('node:fs');
|
|
1008
716
|
const schemaFile = plugin.path.replace('package.json', `${plugin.name}.schema.json`);
|
|
@@ -1013,7 +721,6 @@ export class PluginManager extends EventEmitter {
|
|
|
1013
721
|
schema.title = plugin.description;
|
|
1014
722
|
schema.description = plugin.name + ' v. ' + plugin.version + ' by ' + plugin.author;
|
|
1015
723
|
this.log.debug(`Loaded schema file ${schemaFile} for plugin ${plg}${plugin.name}${db}.`);
|
|
1016
|
-
// this.log.debug(`Loaded schema file ${schemaFile} for plugin ${plg}${plugin.name}${db}.\nSchema:${rs}\n`, schema);
|
|
1017
724
|
return schema;
|
|
1018
725
|
}
|
|
1019
726
|
catch (_err) {
|
|
@@ -1021,16 +728,6 @@ export class PluginManager extends EventEmitter {
|
|
|
1021
728
|
return this.getDefaultSchema(plugin);
|
|
1022
729
|
}
|
|
1023
730
|
}
|
|
1024
|
-
/**
|
|
1025
|
-
* Returns the default schema for a plugin.
|
|
1026
|
-
*
|
|
1027
|
-
* This method generates a default schema object for the specified plugin. The schema includes
|
|
1028
|
-
* metadata such as the plugin's title, description, version, and author. It also defines the
|
|
1029
|
-
* properties of the schema, including the plugin's name, type, debug flag, and unregisterOnShutdown flag.
|
|
1030
|
-
*
|
|
1031
|
-
* @param {RegisteredPlugin} plugin - The plugin for which the default schema is to be generated.
|
|
1032
|
-
* @returns {PlatformSchema} The default schema object for the plugin.
|
|
1033
|
-
*/
|
|
1034
731
|
getDefaultSchema(plugin) {
|
|
1035
732
|
return {
|
|
1036
733
|
title: plugin.description,
|
|
@@ -1061,4 +758,3 @@ export class PluginManager extends EventEmitter {
|
|
|
1061
758
|
};
|
|
1062
759
|
}
|
|
1063
760
|
}
|
|
1064
|
-
//# sourceMappingURL=pluginManager.js.map
|