matterbridge 3.1.4-dev-20250717-d36e252 → 3.1.4

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.
Files changed (206) hide show
  1. package/dist/cli.d.ts +26 -0
  2. package/dist/cli.d.ts.map +1 -0
  3. package/dist/cli.js +91 -2
  4. package/dist/cli.js.map +1 -0
  5. package/dist/cliEmitter.d.ts +34 -0
  6. package/dist/cliEmitter.d.ts.map +1 -0
  7. package/dist/cliEmitter.js +30 -0
  8. package/dist/cliEmitter.js.map +1 -0
  9. package/dist/clusters/export.d.ts +2 -0
  10. package/dist/clusters/export.d.ts.map +1 -0
  11. package/dist/clusters/export.js +2 -0
  12. package/dist/clusters/export.js.map +1 -0
  13. package/dist/defaultConfigSchema.d.ts +28 -0
  14. package/dist/defaultConfigSchema.d.ts.map +1 -0
  15. package/dist/defaultConfigSchema.js +24 -0
  16. package/dist/defaultConfigSchema.js.map +1 -0
  17. package/dist/deviceManager.d.ts +112 -0
  18. package/dist/deviceManager.d.ts.map +1 -0
  19. package/dist/deviceManager.js +94 -1
  20. package/dist/deviceManager.js.map +1 -0
  21. package/dist/devices/batteryStorage.d.ts +48 -0
  22. package/dist/devices/batteryStorage.d.ts.map +1 -0
  23. package/dist/devices/batteryStorage.js +48 -1
  24. package/dist/devices/batteryStorage.js.map +1 -0
  25. package/dist/devices/evse.d.ts +75 -0
  26. package/dist/devices/evse.d.ts.map +1 -0
  27. package/dist/devices/evse.js +74 -10
  28. package/dist/devices/evse.js.map +1 -0
  29. package/dist/devices/export.d.ts +9 -0
  30. package/dist/devices/export.d.ts.map +1 -0
  31. package/dist/devices/export.js +2 -0
  32. package/dist/devices/export.js.map +1 -0
  33. package/dist/devices/heatPump.d.ts +47 -0
  34. package/dist/devices/heatPump.d.ts.map +1 -0
  35. package/dist/devices/heatPump.js +50 -2
  36. package/dist/devices/heatPump.js.map +1 -0
  37. package/dist/devices/laundryDryer.d.ts +87 -0
  38. package/dist/devices/laundryDryer.d.ts.map +1 -0
  39. package/dist/devices/laundryDryer.js +83 -6
  40. package/dist/devices/laundryDryer.js.map +1 -0
  41. package/dist/devices/laundryWasher.d.ts +242 -0
  42. package/dist/devices/laundryWasher.d.ts.map +1 -0
  43. package/dist/devices/laundryWasher.js +91 -7
  44. package/dist/devices/laundryWasher.js.map +1 -0
  45. package/dist/devices/roboticVacuumCleaner.d.ts +110 -0
  46. package/dist/devices/roboticVacuumCleaner.d.ts.map +1 -0
  47. package/dist/devices/roboticVacuumCleaner.js +89 -6
  48. package/dist/devices/roboticVacuumCleaner.js.map +1 -0
  49. package/dist/devices/solarPower.d.ts +40 -0
  50. package/dist/devices/solarPower.d.ts.map +1 -0
  51. package/dist/devices/solarPower.js +38 -0
  52. package/dist/devices/solarPower.js.map +1 -0
  53. package/dist/devices/waterHeater.d.ts +111 -0
  54. package/dist/devices/waterHeater.d.ts.map +1 -0
  55. package/dist/devices/waterHeater.js +82 -2
  56. package/dist/devices/waterHeater.js.map +1 -0
  57. package/dist/frontend.d.ts +304 -0
  58. package/dist/frontend.d.ts.map +1 -0
  59. package/dist/frontend.js +429 -21
  60. package/dist/frontend.js.map +1 -0
  61. package/dist/globalMatterbridge.d.ts +59 -0
  62. package/dist/globalMatterbridge.d.ts.map +1 -0
  63. package/dist/globalMatterbridge.js +47 -0
  64. package/dist/globalMatterbridge.js.map +1 -0
  65. package/dist/helpers.d.ts +48 -0
  66. package/dist/helpers.d.ts.map +1 -0
  67. package/dist/helpers.js +53 -0
  68. package/dist/helpers.js.map +1 -0
  69. package/dist/index.d.ts +33 -0
  70. package/dist/index.d.ts.map +1 -0
  71. package/dist/index.js +30 -1
  72. package/dist/index.js.map +1 -0
  73. package/dist/logger/export.d.ts +2 -0
  74. package/dist/logger/export.d.ts.map +1 -0
  75. package/dist/logger/export.js +1 -0
  76. package/dist/logger/export.js.map +1 -0
  77. package/dist/matter/behaviors.d.ts +2 -0
  78. package/dist/matter/behaviors.d.ts.map +1 -0
  79. package/dist/matter/behaviors.js +2 -0
  80. package/dist/matter/behaviors.js.map +1 -0
  81. package/dist/matter/clusters.d.ts +2 -0
  82. package/dist/matter/clusters.d.ts.map +1 -0
  83. package/dist/matter/clusters.js +2 -0
  84. package/dist/matter/clusters.js.map +1 -0
  85. package/dist/matter/devices.d.ts +2 -0
  86. package/dist/matter/devices.d.ts.map +1 -0
  87. package/dist/matter/devices.js +2 -0
  88. package/dist/matter/devices.js.map +1 -0
  89. package/dist/matter/endpoints.d.ts +2 -0
  90. package/dist/matter/endpoints.d.ts.map +1 -0
  91. package/dist/matter/endpoints.js +2 -0
  92. package/dist/matter/endpoints.js.map +1 -0
  93. package/dist/matter/export.d.ts +5 -0
  94. package/dist/matter/export.d.ts.map +1 -0
  95. package/dist/matter/export.js +3 -0
  96. package/dist/matter/export.js.map +1 -0
  97. package/dist/matter/types.d.ts +3 -0
  98. package/dist/matter/types.d.ts.map +1 -0
  99. package/dist/matter/types.js +3 -0
  100. package/dist/matter/types.js.map +1 -0
  101. package/dist/matterbridge.d.ts +444 -0
  102. package/dist/matterbridge.d.ts.map +1 -0
  103. package/dist/matterbridge.js +785 -51
  104. package/dist/matterbridge.js.map +1 -0
  105. package/dist/matterbridgeAccessoryPlatform.d.ts +42 -0
  106. package/dist/matterbridgeAccessoryPlatform.d.ts.map +1 -0
  107. package/dist/matterbridgeAccessoryPlatform.js +36 -0
  108. package/dist/matterbridgeAccessoryPlatform.js.map +1 -0
  109. package/dist/matterbridgeBehaviors.d.ts +1340 -0
  110. package/dist/matterbridgeBehaviors.d.ts.map +1 -0
  111. package/dist/matterbridgeBehaviors.js +61 -1
  112. package/dist/matterbridgeBehaviors.js.map +1 -0
  113. package/dist/matterbridgeDeviceTypes.d.ts +709 -0
  114. package/dist/matterbridgeDeviceTypes.d.ts.map +1 -0
  115. package/dist/matterbridgeDeviceTypes.js +579 -15
  116. package/dist/matterbridgeDeviceTypes.js.map +1 -0
  117. package/dist/matterbridgeDynamicPlatform.d.ts +42 -0
  118. package/dist/matterbridgeDynamicPlatform.d.ts.map +1 -0
  119. package/dist/matterbridgeDynamicPlatform.js +36 -0
  120. package/dist/matterbridgeDynamicPlatform.js.map +1 -0
  121. package/dist/matterbridgeEndpoint.d.ts +1250 -0
  122. package/dist/matterbridgeEndpoint.d.ts.map +1 -0
  123. package/dist/matterbridgeEndpoint.js +1106 -42
  124. package/dist/matterbridgeEndpoint.js.map +1 -0
  125. package/dist/matterbridgeEndpointHelpers.d.ts +3198 -0
  126. package/dist/matterbridgeEndpointHelpers.d.ts.map +1 -0
  127. package/dist/matterbridgeEndpointHelpers.js +322 -12
  128. package/dist/matterbridgeEndpointHelpers.js.map +1 -0
  129. package/dist/matterbridgePlatform.d.ts +310 -0
  130. package/dist/matterbridgePlatform.d.ts.map +1 -0
  131. package/dist/matterbridgePlatform.js +233 -0
  132. package/dist/matterbridgePlatform.js.map +1 -0
  133. package/dist/matterbridgeTypes.d.ts +195 -0
  134. package/dist/matterbridgeTypes.d.ts.map +1 -0
  135. package/dist/matterbridgeTypes.js +25 -0
  136. package/dist/matterbridgeTypes.js.map +1 -0
  137. package/dist/pluginManager.d.ts +291 -0
  138. package/dist/pluginManager.d.ts.map +1 -0
  139. package/dist/pluginManager.js +269 -3
  140. package/dist/pluginManager.js.map +1 -0
  141. package/dist/shelly.d.ts +174 -0
  142. package/dist/shelly.d.ts.map +1 -0
  143. package/dist/shelly.js +168 -7
  144. package/dist/shelly.js.map +1 -0
  145. package/dist/storage/export.d.ts +2 -0
  146. package/dist/storage/export.d.ts.map +1 -0
  147. package/dist/storage/export.js +1 -0
  148. package/dist/storage/export.js.map +1 -0
  149. package/dist/update.d.ts +59 -0
  150. package/dist/update.d.ts.map +1 -0
  151. package/dist/update.js +54 -0
  152. package/dist/update.js.map +1 -0
  153. package/dist/utils/colorUtils.d.ts +117 -0
  154. package/dist/utils/colorUtils.d.ts.map +1 -0
  155. package/dist/utils/colorUtils.js +263 -2
  156. package/dist/utils/colorUtils.js.map +1 -0
  157. package/dist/utils/commandLine.d.ts +59 -0
  158. package/dist/utils/commandLine.d.ts.map +1 -0
  159. package/dist/utils/commandLine.js +54 -0
  160. package/dist/utils/commandLine.js.map +1 -0
  161. package/dist/utils/copyDirectory.d.ts +33 -0
  162. package/dist/utils/copyDirectory.d.ts.map +1 -0
  163. package/dist/utils/copyDirectory.js +38 -1
  164. package/dist/utils/copyDirectory.js.map +1 -0
  165. package/dist/utils/createDirectory.d.ts +34 -0
  166. package/dist/utils/createDirectory.d.ts.map +1 -0
  167. package/dist/utils/createDirectory.js +33 -0
  168. package/dist/utils/createDirectory.js.map +1 -0
  169. package/dist/utils/createZip.d.ts +39 -0
  170. package/dist/utils/createZip.d.ts.map +1 -0
  171. package/dist/utils/createZip.js +47 -2
  172. package/dist/utils/createZip.js.map +1 -0
  173. package/dist/utils/deepCopy.d.ts +32 -0
  174. package/dist/utils/deepCopy.d.ts.map +1 -0
  175. package/dist/utils/deepCopy.js +39 -0
  176. package/dist/utils/deepCopy.js.map +1 -0
  177. package/dist/utils/deepEqual.d.ts +54 -0
  178. package/dist/utils/deepEqual.d.ts.map +1 -0
  179. package/dist/utils/deepEqual.js +72 -1
  180. package/dist/utils/deepEqual.js.map +1 -0
  181. package/dist/utils/export.d.ts +12 -0
  182. package/dist/utils/export.d.ts.map +1 -0
  183. package/dist/utils/export.js +1 -0
  184. package/dist/utils/export.js.map +1 -0
  185. package/dist/utils/hex.d.ts +49 -0
  186. package/dist/utils/hex.d.ts.map +1 -0
  187. package/dist/utils/hex.js +58 -0
  188. package/dist/utils/hex.js.map +1 -0
  189. package/dist/utils/isvalid.d.ts +103 -0
  190. package/dist/utils/isvalid.d.ts.map +1 -0
  191. package/dist/utils/isvalid.js +101 -0
  192. package/dist/utils/isvalid.js.map +1 -0
  193. package/dist/utils/network.d.ts +74 -0
  194. package/dist/utils/network.d.ts.map +1 -0
  195. package/dist/utils/network.js +81 -5
  196. package/dist/utils/network.js.map +1 -0
  197. package/dist/utils/spawn.d.ts +11 -0
  198. package/dist/utils/spawn.d.ts.map +1 -0
  199. package/dist/utils/spawn.js +18 -0
  200. package/dist/utils/spawn.js.map +1 -0
  201. package/dist/utils/wait.d.ts +56 -0
  202. package/dist/utils/wait.d.ts.map +1 -0
  203. package/dist/utils/wait.js +62 -9
  204. package/dist/utils/wait.js.map +1 -0
  205. package/npm-shrinkwrap.json +2 -2
  206. package/package.json +2 -1
@@ -1,4 +1,29 @@
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
1
25
  import EventEmitter from 'node:events';
26
+ // AnsiLogger module
2
27
  import { AnsiLogger, UNDERLINE, UNDERLINEOFF, BLUE, db, er, nf, nt, rs, wr } from 'node-ansi-logger';
3
28
  import { plg, typ } from './matterbridgeTypes.js';
4
29
  export class PluginManager extends EventEmitter {
@@ -9,8 +34,9 @@ export class PluginManager extends EventEmitter {
9
34
  constructor(matterbridge) {
10
35
  super();
11
36
  this.matterbridge = matterbridge;
37
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
12
38
  this.nodeContext = matterbridge.nodeContext;
13
- this.log = new AnsiLogger({ logName: 'PluginManager', logTimestampFormat: 4, logLevel: matterbridge.log.logLevel });
39
+ this.log = new AnsiLogger({ logName: 'PluginManager', logTimestampFormat: 4 /* TimestampFormat.TIME_MILLIS */, logLevel: matterbridge.log.logLevel });
14
40
  this.log.debug('Matterbridge plugin manager starting...');
15
41
  }
16
42
  get length() {
@@ -47,6 +73,7 @@ export class PluginManager extends EventEmitter {
47
73
  }
48
74
  catch (err) {
49
75
  this.log.error(`Error processing forEach plugin ${plg}${plugin.name}${er}: ${err instanceof Error ? err.message + '\n' + err.stack : err}`);
76
+ // throw error;
50
77
  }
51
78
  });
52
79
  await Promise.all(tasks);
@@ -54,13 +81,31 @@ export class PluginManager extends EventEmitter {
54
81
  set logLevel(logLevel) {
55
82
  this.log.logLevel = logLevel;
56
83
  }
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
+ */
57
92
  async loadFromStorage() {
93
+ // Load the array from storage and convert it to a map
58
94
  const pluginsArray = await this.nodeContext.get('plugins', []);
59
95
  for (const plugin of pluginsArray)
60
96
  this._plugins.set(plugin.name, plugin);
61
97
  return pluginsArray;
62
98
  }
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
+ */
63
107
  async saveToStorage() {
108
+ // Convert the map to an array
64
109
  const plugins = [];
65
110
  const pluginArrayFromMap = Array.from(this._plugins.values());
66
111
  for (const plugin of pluginArrayFromMap) {
@@ -78,13 +123,21 @@ export class PluginManager extends EventEmitter {
78
123
  this.log.debug(`Saved ${BLUE}${plugins.length}${db} plugins to storage`);
79
124
  return plugins.length;
80
125
  }
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
+ */
81
132
  async resolve(pluginPath) {
82
133
  const { default: path } = await import('node:path');
83
134
  const { promises } = await import('node:fs');
84
135
  if (!pluginPath.endsWith('package.json'))
85
136
  pluginPath = path.join(pluginPath, 'package.json');
137
+ // Resolve the package.json of the plugin
86
138
  let packageJsonPath = path.resolve(pluginPath);
87
139
  this.log.debug(`Resolving plugin path ${plg}${packageJsonPath}${db}`);
140
+ // Check if the package.json file exists
88
141
  try {
89
142
  await promises.access(packageJsonPath);
90
143
  }
@@ -94,7 +147,9 @@ export class PluginManager extends EventEmitter {
94
147
  this.log.debug(`Trying at ${plg}${packageJsonPath}${db}`);
95
148
  }
96
149
  try {
150
+ // Load the package.json of the plugin
97
151
  const packageJson = JSON.parse(await promises.readFile(packageJsonPath, 'utf8'));
152
+ // Check for main issues
98
153
  if (!packageJson.name) {
99
154
  this.log.error(`Package.json name not found at ${packageJsonPath}`);
100
155
  return null;
@@ -107,6 +162,7 @@ export class PluginManager extends EventEmitter {
107
162
  this.log.error(`Plugin at ${packageJsonPath} has no main entrypoint in package.json`);
108
163
  return null;
109
164
  }
165
+ // Check for @project-chip and @matter packages in dependencies and devDependencies
110
166
  const checkForProjectChipPackages = (dependencies) => {
111
167
  return Object.keys(dependencies).filter((pkg) => pkg.startsWith('@project-chip') || pkg.startsWith('@matter'));
112
168
  };
@@ -128,6 +184,7 @@ export class PluginManager extends EventEmitter {
128
184
  this.log.error(`Please open an issue on the plugin repository to remove them.`);
129
185
  return null;
130
186
  }
187
+ // Check for matterbridge package in dependencies and devDependencies
131
188
  const checkForMatterbridgePackage = (dependencies) => {
132
189
  return Object.keys(dependencies).filter((pkg) => pkg === 'matterbridge');
133
190
  };
@@ -157,6 +214,12 @@ export class PluginManager extends EventEmitter {
157
214
  return null;
158
215
  }
159
216
  }
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
+ */
160
223
  getAuthor(packageJson) {
161
224
  if (packageJson.author && typeof packageJson.author === 'string')
162
225
  return packageJson.author;
@@ -164,6 +227,12 @@ export class PluginManager extends EventEmitter {
164
227
  return packageJson.author.name;
165
228
  return 'Unknown author';
166
229
  }
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
+ */
167
236
  getHomepage(packageJson) {
168
237
  if (packageJson.homepage && typeof packageJson.homepage === 'string' && packageJson.homepage.includes('http')) {
169
238
  return packageJson.homepage.replace('git+', '').replace('.git', '');
@@ -172,7 +241,14 @@ export class PluginManager extends EventEmitter {
172
241
  return packageJson.repository.url.replace('git+', '').replace('.git', '');
173
242
  }
174
243
  }
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
+ */
175
250
  getHelp(packageJson) {
251
+ // If there's a help field that looks like a URL, return it.
176
252
  if (packageJson.help && typeof packageJson.help === 'string' && packageJson.help.startsWith('http')) {
177
253
  return packageJson.help;
178
254
  }
@@ -183,7 +259,14 @@ export class PluginManager extends EventEmitter {
183
259
  return packageJson.homepage.replace('git+', '').replace('.git', '');
184
260
  }
185
261
  }
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
+ */
186
268
  getChangelog(packageJson) {
269
+ // If there's a changelog field that looks like a URL, return it.
187
270
  if (packageJson.changelog && typeof packageJson.changelog === 'string' && packageJson.changelog.startsWith('http')) {
188
271
  return packageJson.changelog;
189
272
  }
@@ -194,6 +277,13 @@ export class PluginManager extends EventEmitter {
194
277
  return packageJson.homepage.replace('git+', '').replace('.git', '');
195
278
  }
196
279
  }
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
197
287
  getFunding(packageJson) {
198
288
  const funding = packageJson.funding;
199
289
  if (!funding)
@@ -202,16 +292,25 @@ export class PluginManager extends EventEmitter {
202
292
  return;
203
293
  if (typeof funding === 'string' && funding.startsWith('http'))
204
294
  return funding;
295
+ // Normalize funding into an array.
205
296
  const fundingEntries = Array.isArray(funding) ? funding : [funding];
206
297
  for (const entry of fundingEntries) {
207
298
  if (entry && typeof entry === 'string' && entry.startsWith('http')) {
299
+ // If the funding entry is a string, assume it is a URL.
208
300
  return entry;
209
301
  }
210
302
  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.
211
304
  return entry.url;
212
305
  }
213
306
  }
214
307
  }
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
+ */
215
314
  async parse(plugin) {
216
315
  const { promises } = await import('node:fs');
217
316
  try {
@@ -241,6 +340,7 @@ export class PluginManager extends EventEmitter {
241
340
  plugin.funding = this.getFunding(packageJson);
242
341
  if (!plugin.type)
243
342
  this.log.warn(`Plugin ${plg}${plugin.name}${wr} has no type`);
343
+ // Check for @project-chip and @matter packages in dependencies and devDependencies
244
344
  const checkForProjectChipPackages = (dependencies) => {
245
345
  return Object.keys(dependencies).filter((pkg) => pkg.startsWith('@project-chip') || pkg.startsWith('@matter'));
246
346
  };
@@ -262,6 +362,7 @@ export class PluginManager extends EventEmitter {
262
362
  this.log.error(`Please open an issue on the plugin repository to remove them.`);
263
363
  return null;
264
364
  }
365
+ // Check for matterbridge package in dependencies and devDependencies
265
366
  const checkForMatterbridgePackage = (dependencies) => {
266
367
  return Object.keys(dependencies).filter((pkg) => pkg === 'matterbridge');
267
368
  };
@@ -291,6 +392,16 @@ export class PluginManager extends EventEmitter {
291
392
  return null;
292
393
  }
293
394
  }
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
+ */
294
405
  async enable(nameOrPath) {
295
406
  const { promises } = await import('node:fs');
296
407
  if (!nameOrPath)
@@ -326,6 +437,16 @@ export class PluginManager extends EventEmitter {
326
437
  return null;
327
438
  }
328
439
  }
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
+ */
329
450
  async disable(nameOrPath) {
330
451
  const { promises } = await import('node:fs');
331
452
  if (!nameOrPath)
@@ -361,6 +482,16 @@ export class PluginManager extends EventEmitter {
361
482
  return null;
362
483
  }
363
484
  }
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
+ */
364
495
  async remove(nameOrPath) {
365
496
  const { promises } = await import('node:fs');
366
497
  if (!nameOrPath)
@@ -396,6 +527,17 @@ export class PluginManager extends EventEmitter {
396
527
  return null;
397
528
  }
398
529
  }
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
+ */
399
541
  async add(nameOrPath) {
400
542
  const { promises } = await import('node:fs');
401
543
  if (!nameOrPath)
@@ -431,6 +573,15 @@ export class PluginManager extends EventEmitter {
431
573
  return null;
432
574
  }
433
575
  }
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
+ */
434
585
  async install(name) {
435
586
  const { exec } = await import('node:child_process');
436
587
  await this.uninstall(name);
@@ -445,11 +596,14 @@ export class PluginManager extends EventEmitter {
445
596
  else {
446
597
  this.log.info(`Installed plugin ${plg}${name}${nf}`);
447
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
448
601
  exec(`npm list -g ${name} --depth=0`, (listError, listStdout, listStderr) => {
449
602
  if (listError) {
450
603
  this.log.error(`List error: ${listError}`);
451
604
  resolve(undefined);
452
605
  }
606
+ // Clean the output to get only the package name and version
453
607
  const lines = listStdout.split('\n');
454
608
  const versionLine = lines.find((line) => line.includes(`${name}@`));
455
609
  if (versionLine) {
@@ -466,6 +620,15 @@ export class PluginManager extends EventEmitter {
466
620
  });
467
621
  });
468
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
+ */
469
632
  async uninstall(name) {
470
633
  const { exec } = await import('node:child_process');
471
634
  this.log.info(`Uninstalling plugin ${plg}${name}${nf}`);
@@ -485,6 +648,15 @@ export class PluginManager extends EventEmitter {
485
648
  });
486
649
  });
487
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
+ */
488
660
  async load(plugin, start = false, message = '', configure = false) {
489
661
  const { promises } = await import('node:fs');
490
662
  const { default: path } = await import('node:path');
@@ -498,15 +670,20 @@ export class PluginManager extends EventEmitter {
498
670
  }
499
671
  this.log.info(`Loading plugin ${plg}${plugin.name}${nf} type ${typ}${plugin.type}${nf}`);
500
672
  try {
673
+ // Load the package.json of the plugin
501
674
  const packageJson = JSON.parse(await promises.readFile(plugin.path, 'utf8'));
675
+ // Resolve the main module path relative to package.json
502
676
  const pluginEntry = path.resolve(path.dirname(plugin.path), packageJson.main);
677
+ // Dynamically import the plugin
503
678
  const { pathToFileURL } = await import('node:url');
504
679
  const pluginUrl = pathToFileURL(pluginEntry);
505
680
  this.log.debug(`Importing plugin ${plg}${plugin.name}${db} from ${pluginUrl.href}`);
506
681
  const pluginInstance = await import(pluginUrl.href);
507
682
  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
508
684
  if (pluginInstance.default) {
509
685
  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.
510
687
  plugin.name = packageJson.name;
511
688
  plugin.description = packageJson.description ?? 'No description';
512
689
  plugin.version = packageJson.version;
@@ -515,7 +692,7 @@ export class PluginManager extends EventEmitter {
515
692
  plugin.schemaJson = await this.loadSchema(plugin);
516
693
  config.name = plugin.name;
517
694
  config.version = packageJson.version;
518
- const log = new AnsiLogger({ logName: plugin.description ?? 'No description', logTimestampFormat: 4, logLevel: config.debug ? "debug" : this.matterbridge.log.logLevel });
695
+ const log = new AnsiLogger({ logName: plugin.description ?? 'No description', logTimestampFormat: 4 /* TimestampFormat.TIME_MILLIS */, logLevel: config.debug ? "debug" /* LogLevel.DEBUG */ : this.matterbridge.log.logLevel });
519
696
  const platform = pluginInstance.default(this.matterbridge, log, config);
520
697
  config.type = platform.type;
521
698
  platform.name = packageJson.name;
@@ -534,7 +711,7 @@ export class PluginManager extends EventEmitter {
534
711
  plugin.loaded = true;
535
712
  plugin.registeredDevices = 0;
536
713
  plugin.addedDevices = 0;
537
- await this.saveToStorage();
714
+ await this.saveToStorage(); // Save the plugin to storage
538
715
  this.log.notice(`Loaded plugin ${plg}${plugin.name}${nt} type ${typ}${platform.type}${nt} (entrypoint ${UNDERLINE}${pluginEntry}${UNDERLINEOFF})`);
539
716
  this.emit('loaded', plugin.name);
540
717
  if (start)
@@ -554,6 +731,14 @@ export class PluginManager extends EventEmitter {
554
731
  }
555
732
  return undefined;
556
733
  }
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
+ */
557
742
  async start(plugin, message, configure = false) {
558
743
  if (!plugin.loaded) {
559
744
  this.log.error(`Plugin ${plg}${plugin.name}${er} not loaded`);
@@ -584,6 +769,12 @@ export class PluginManager extends EventEmitter {
584
769
  }
585
770
  return undefined;
586
771
  }
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
+ */
587
778
  async configure(plugin) {
588
779
  if (!plugin.loaded) {
589
780
  this.log.error(`Plugin ${plg}${plugin.name}${er} not loaded`);
@@ -615,6 +806,18 @@ export class PluginManager extends EventEmitter {
615
806
  }
616
807
  return undefined;
617
808
  }
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
+ */
618
821
  async shutdown(plugin, reason, removeAllDevices = false, force = false) {
619
822
  this.log.debug(`Shutting down plugin ${plg}${plugin.name}${db}`);
620
823
  if (!plugin.loaded) {
@@ -658,6 +861,15 @@ export class PluginManager extends EventEmitter {
658
861
  }
659
862
  return undefined;
660
863
  }
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
+ */
661
873
  async loadConfig(plugin) {
662
874
  const { default: path } = await import('node:path');
663
875
  const { promises } = await import('node:fs');
@@ -668,6 +880,8 @@ export class PluginManager extends EventEmitter {
668
880
  const data = await promises.readFile(configFile, 'utf8');
669
881
  const config = JSON.parse(data);
670
882
  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".
671
885
  config.name = plugin.name;
672
886
  config.type = plugin.type;
673
887
  if (config.debug === undefined)
@@ -691,6 +905,7 @@ export class PluginManager extends EventEmitter {
691
905
  try {
692
906
  await promises.writeFile(configFile, JSON.stringify(config, null, 2), 'utf8');
693
907
  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);
694
909
  return config;
695
910
  }
696
911
  catch (err) {
@@ -704,6 +919,19 @@ export class PluginManager extends EventEmitter {
704
919
  }
705
920
  }
706
921
  }
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
+ */
707
935
  async saveConfigFromPlugin(plugin, restartRequired = false) {
708
936
  const { default: path } = await import('node:path');
709
937
  const { promises } = await import('node:fs');
@@ -718,6 +946,7 @@ export class PluginManager extends EventEmitter {
718
946
  if (restartRequired)
719
947
  plugin.restartRequired = true;
720
948
  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);
721
950
  return Promise.resolve();
722
951
  }
723
952
  catch (err) {
@@ -725,6 +954,20 @@ export class PluginManager extends EventEmitter {
725
954
  return Promise.reject(err);
726
955
  }
727
956
  }
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
+ */
728
971
  async saveConfigFromJson(plugin, config, restartRequired = false) {
729
972
  const { default: path } = await import('node:path');
730
973
  const { promises } = await import('node:fs');
@@ -743,12 +986,23 @@ export class PluginManager extends EventEmitter {
743
986
  plugin.platform.onConfigChanged(config).catch((err) => this.log.error(`Error calling onConfigChanged for plugin ${plg}${plugin.name}${er}: ${err}`));
744
987
  }
745
988
  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);
746
990
  }
747
991
  catch (err) {
748
992
  this.log.error(`Error saving config file ${configFile} for plugin ${plg}${plugin.name}${er}: ${err instanceof Error ? err.message + '\n' + err.stack : err}`);
749
993
  return;
750
994
  }
751
995
  }
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
+ */
752
1006
  async loadSchema(plugin) {
753
1007
  const { promises } = await import('node:fs');
754
1008
  const schemaFile = plugin.path.replace('package.json', `${plugin.name}.schema.json`);
@@ -759,6 +1013,7 @@ export class PluginManager extends EventEmitter {
759
1013
  schema.title = plugin.description;
760
1014
  schema.description = plugin.name + ' v. ' + plugin.version + ' by ' + plugin.author;
761
1015
  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);
762
1017
  return schema;
763
1018
  }
764
1019
  catch (_err) {
@@ -766,6 +1021,16 @@ export class PluginManager extends EventEmitter {
766
1021
  return this.getDefaultSchema(plugin);
767
1022
  }
768
1023
  }
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
+ */
769
1034
  getDefaultSchema(plugin) {
770
1035
  return {
771
1036
  title: plugin.description,
@@ -796,3 +1061,4 @@ export class PluginManager extends EventEmitter {
796
1061
  };
797
1062
  }
798
1063
  }
1064
+ //# sourceMappingURL=pluginManager.js.map