matterbridge 3.1.0-dev-20250627-2b5adba → 3.1.0

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 (187) hide show
  1. package/CHANGELOG.md +4 -8
  2. package/dist/cli.d.ts +29 -0
  3. package/dist/cli.d.ts.map +1 -0
  4. package/dist/cli.js +91 -2
  5. package/dist/cli.js.map +1 -0
  6. package/dist/clusters/export.d.ts +2 -0
  7. package/dist/clusters/export.d.ts.map +1 -0
  8. package/dist/clusters/export.js +2 -0
  9. package/dist/clusters/export.js.map +1 -0
  10. package/dist/defaultConfigSchema.d.ts +28 -0
  11. package/dist/defaultConfigSchema.d.ts.map +1 -0
  12. package/dist/defaultConfigSchema.js +24 -0
  13. package/dist/defaultConfigSchema.js.map +1 -0
  14. package/dist/deviceManager.d.ts +112 -0
  15. package/dist/deviceManager.d.ts.map +1 -0
  16. package/dist/deviceManager.js +94 -1
  17. package/dist/deviceManager.js.map +1 -0
  18. package/dist/devices/export.d.ts +5 -0
  19. package/dist/devices/export.d.ts.map +1 -0
  20. package/dist/devices/export.js +2 -0
  21. package/dist/devices/export.js.map +1 -0
  22. package/dist/evse.d.ts +72 -0
  23. package/dist/evse.d.ts.map +1 -0
  24. package/dist/evse.js +70 -9
  25. package/dist/evse.js.map +1 -0
  26. package/dist/frontend.d.ts +285 -0
  27. package/dist/frontend.d.ts.map +1 -0
  28. package/dist/frontend.js +413 -16
  29. package/dist/frontend.js.map +1 -0
  30. package/dist/globalMatterbridge.d.ts +59 -0
  31. package/dist/globalMatterbridge.d.ts.map +1 -0
  32. package/dist/globalMatterbridge.js +47 -0
  33. package/dist/globalMatterbridge.js.map +1 -0
  34. package/dist/helpers.d.ts +48 -0
  35. package/dist/helpers.d.ts.map +1 -0
  36. package/dist/helpers.js +53 -0
  37. package/dist/helpers.js.map +1 -0
  38. package/dist/index.d.ts +38 -0
  39. package/dist/index.d.ts.map +1 -0
  40. package/dist/index.js +32 -1
  41. package/dist/index.js.map +1 -0
  42. package/dist/laundryWasher.d.ts +243 -0
  43. package/dist/laundryWasher.d.ts.map +1 -0
  44. package/dist/laundryWasher.js +92 -7
  45. package/dist/laundryWasher.js.map +1 -0
  46. package/dist/logger/export.d.ts +2 -0
  47. package/dist/logger/export.d.ts.map +1 -0
  48. package/dist/logger/export.js +1 -0
  49. package/dist/logger/export.js.map +1 -0
  50. package/dist/matter/behaviors.d.ts +2 -0
  51. package/dist/matter/behaviors.d.ts.map +1 -0
  52. package/dist/matter/behaviors.js +2 -0
  53. package/dist/matter/behaviors.js.map +1 -0
  54. package/dist/matter/clusters.d.ts +2 -0
  55. package/dist/matter/clusters.d.ts.map +1 -0
  56. package/dist/matter/clusters.js +2 -0
  57. package/dist/matter/clusters.js.map +1 -0
  58. package/dist/matter/devices.d.ts +2 -0
  59. package/dist/matter/devices.d.ts.map +1 -0
  60. package/dist/matter/devices.js +2 -0
  61. package/dist/matter/devices.js.map +1 -0
  62. package/dist/matter/endpoints.d.ts +2 -0
  63. package/dist/matter/endpoints.d.ts.map +1 -0
  64. package/dist/matter/endpoints.js +2 -0
  65. package/dist/matter/endpoints.js.map +1 -0
  66. package/dist/matter/export.d.ts +5 -0
  67. package/dist/matter/export.d.ts.map +1 -0
  68. package/dist/matter/export.js +3 -0
  69. package/dist/matter/export.js.map +1 -0
  70. package/dist/matter/types.d.ts +3 -0
  71. package/dist/matter/types.d.ts.map +1 -0
  72. package/dist/matter/types.js +3 -0
  73. package/dist/matter/types.js.map +1 -0
  74. package/dist/matterbridge.d.ts +450 -0
  75. package/dist/matterbridge.d.ts.map +1 -0
  76. package/dist/matterbridge.js +810 -56
  77. package/dist/matterbridge.js.map +1 -0
  78. package/dist/matterbridgeAccessoryPlatform.d.ts +42 -0
  79. package/dist/matterbridgeAccessoryPlatform.d.ts.map +1 -0
  80. package/dist/matterbridgeAccessoryPlatform.js +36 -0
  81. package/dist/matterbridgeAccessoryPlatform.js.map +1 -0
  82. package/dist/matterbridgeBehaviors.d.ts +1334 -0
  83. package/dist/matterbridgeBehaviors.d.ts.map +1 -0
  84. package/dist/matterbridgeBehaviors.js +55 -1
  85. package/dist/matterbridgeBehaviors.js.map +1 -0
  86. package/dist/matterbridgeDeviceTypes.d.ts +709 -0
  87. package/dist/matterbridgeDeviceTypes.d.ts.map +1 -0
  88. package/dist/matterbridgeDeviceTypes.js +579 -15
  89. package/dist/matterbridgeDeviceTypes.js.map +1 -0
  90. package/dist/matterbridgeDynamicPlatform.d.ts +42 -0
  91. package/dist/matterbridgeDynamicPlatform.d.ts.map +1 -0
  92. package/dist/matterbridgeDynamicPlatform.js +36 -0
  93. package/dist/matterbridgeDynamicPlatform.js.map +1 -0
  94. package/dist/matterbridgeEndpoint.d.ts +1173 -0
  95. package/dist/matterbridgeEndpoint.d.ts.map +1 -0
  96. package/dist/matterbridgeEndpoint.js +1023 -41
  97. package/dist/matterbridgeEndpoint.js.map +1 -0
  98. package/dist/matterbridgeEndpointHelpers.d.ts +3198 -0
  99. package/dist/matterbridgeEndpointHelpers.d.ts.map +1 -0
  100. package/dist/matterbridgeEndpointHelpers.js +322 -12
  101. package/dist/matterbridgeEndpointHelpers.js.map +1 -0
  102. package/dist/matterbridgePlatform.d.ts +310 -0
  103. package/dist/matterbridgePlatform.d.ts.map +1 -0
  104. package/dist/matterbridgePlatform.js +233 -0
  105. package/dist/matterbridgePlatform.js.map +1 -0
  106. package/dist/matterbridgeTypes.d.ts +184 -0
  107. package/dist/matterbridgeTypes.d.ts.map +1 -0
  108. package/dist/matterbridgeTypes.js +25 -0
  109. package/dist/matterbridgeTypes.js.map +1 -0
  110. package/dist/pluginManager.d.ts +291 -0
  111. package/dist/pluginManager.d.ts.map +1 -0
  112. package/dist/pluginManager.js +269 -3
  113. package/dist/pluginManager.js.map +1 -0
  114. package/dist/roboticVacuumCleaner.d.ts +104 -0
  115. package/dist/roboticVacuumCleaner.d.ts.map +1 -0
  116. package/dist/roboticVacuumCleaner.js +83 -6
  117. package/dist/roboticVacuumCleaner.js.map +1 -0
  118. package/dist/shelly.d.ts +174 -0
  119. package/dist/shelly.d.ts.map +1 -0
  120. package/dist/shelly.js +168 -7
  121. package/dist/shelly.js.map +1 -0
  122. package/dist/storage/export.d.ts +2 -0
  123. package/dist/storage/export.d.ts.map +1 -0
  124. package/dist/storage/export.js +1 -0
  125. package/dist/storage/export.js.map +1 -0
  126. package/dist/update.d.ts +59 -0
  127. package/dist/update.d.ts.map +1 -0
  128. package/dist/update.js +54 -0
  129. package/dist/update.js.map +1 -0
  130. package/dist/utils/colorUtils.d.ts +117 -0
  131. package/dist/utils/colorUtils.d.ts.map +1 -0
  132. package/dist/utils/colorUtils.js +263 -2
  133. package/dist/utils/colorUtils.js.map +1 -0
  134. package/dist/utils/commandLine.d.ts +59 -0
  135. package/dist/utils/commandLine.d.ts.map +1 -0
  136. package/dist/utils/commandLine.js +54 -0
  137. package/dist/utils/commandLine.js.map +1 -0
  138. package/dist/utils/copyDirectory.d.ts +33 -0
  139. package/dist/utils/copyDirectory.d.ts.map +1 -0
  140. package/dist/utils/copyDirectory.js +38 -1
  141. package/dist/utils/copyDirectory.js.map +1 -0
  142. package/dist/utils/createDirectory.d.ts +34 -0
  143. package/dist/utils/createDirectory.d.ts.map +1 -0
  144. package/dist/utils/createDirectory.js +33 -0
  145. package/dist/utils/createDirectory.js.map +1 -0
  146. package/dist/utils/createZip.d.ts +39 -0
  147. package/dist/utils/createZip.d.ts.map +1 -0
  148. package/dist/utils/createZip.js +47 -2
  149. package/dist/utils/createZip.js.map +1 -0
  150. package/dist/utils/deepCopy.d.ts +32 -0
  151. package/dist/utils/deepCopy.d.ts.map +1 -0
  152. package/dist/utils/deepCopy.js +39 -0
  153. package/dist/utils/deepCopy.js.map +1 -0
  154. package/dist/utils/deepEqual.d.ts +54 -0
  155. package/dist/utils/deepEqual.d.ts.map +1 -0
  156. package/dist/utils/deepEqual.js +72 -1
  157. package/dist/utils/deepEqual.js.map +1 -0
  158. package/dist/utils/export.d.ts +12 -0
  159. package/dist/utils/export.d.ts.map +1 -0
  160. package/dist/utils/export.js +1 -0
  161. package/dist/utils/export.js.map +1 -0
  162. package/dist/utils/hex.d.ts +49 -0
  163. package/dist/utils/hex.d.ts.map +1 -0
  164. package/dist/utils/hex.js +58 -0
  165. package/dist/utils/hex.js.map +1 -0
  166. package/dist/utils/isvalid.d.ts +103 -0
  167. package/dist/utils/isvalid.d.ts.map +1 -0
  168. package/dist/utils/isvalid.js +101 -0
  169. package/dist/utils/isvalid.js.map +1 -0
  170. package/dist/utils/network.d.ts +76 -0
  171. package/dist/utils/network.d.ts.map +1 -0
  172. package/dist/utils/network.js +83 -5
  173. package/dist/utils/network.js.map +1 -0
  174. package/dist/utils/spawn.d.ts +14 -0
  175. package/dist/utils/spawn.d.ts.map +1 -0
  176. package/dist/utils/spawn.js +18 -0
  177. package/dist/utils/spawn.js.map +1 -0
  178. package/dist/utils/wait.d.ts +56 -0
  179. package/dist/utils/wait.d.ts.map +1 -0
  180. package/dist/utils/wait.js +62 -9
  181. package/dist/utils/wait.js.map +1 -0
  182. package/dist/waterHeater.d.ts +106 -0
  183. package/dist/waterHeater.d.ts.map +1 -0
  184. package/dist/waterHeater.js +77 -2
  185. package/dist/waterHeater.js.map +1 -0
  186. package/npm-shrinkwrap.json +2 -2
  187. 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) {
@@ -80,13 +125,21 @@ export class PluginManager extends EventEmitter {
80
125
  this.log.debug(`Saved ${BLUE}${plugins.length}${db} plugins to storage`);
81
126
  return plugins.length;
82
127
  }
128
+ /**
129
+ * Resolves the name of a plugin by loading and parsing its package.json file.
130
+ *
131
+ * @param {string} pluginPath - The path to the plugin or the path to the plugin's package.json file.
132
+ * @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.
133
+ */
83
134
  async resolve(pluginPath) {
84
135
  const { default: path } = await import('node:path');
85
136
  const { promises } = await import('node:fs');
86
137
  if (!pluginPath.endsWith('package.json'))
87
138
  pluginPath = path.join(pluginPath, 'package.json');
139
+ // Resolve the package.json of the plugin
88
140
  let packageJsonPath = path.resolve(pluginPath);
89
141
  this.log.debug(`Resolving plugin path ${plg}${packageJsonPath}${db}`);
142
+ // Check if the package.json file exists
90
143
  try {
91
144
  await promises.access(packageJsonPath);
92
145
  }
@@ -96,7 +149,9 @@ export class PluginManager extends EventEmitter {
96
149
  this.log.debug(`Trying at ${plg}${packageJsonPath}${db}`);
97
150
  }
98
151
  try {
152
+ // Load the package.json of the plugin
99
153
  const packageJson = JSON.parse(await promises.readFile(packageJsonPath, 'utf8'));
154
+ // Check for main issues
100
155
  if (!packageJson.name) {
101
156
  this.log.error(`Package.json name not found at ${packageJsonPath}`);
102
157
  return null;
@@ -109,6 +164,7 @@ export class PluginManager extends EventEmitter {
109
164
  this.log.error(`Plugin at ${packageJsonPath} has no main entrypoint in package.json`);
110
165
  return null;
111
166
  }
167
+ // Check for @project-chip and @matter packages in dependencies and devDependencies
112
168
  const checkForProjectChipPackages = (dependencies) => {
113
169
  return Object.keys(dependencies).filter((pkg) => pkg.startsWith('@project-chip') || pkg.startsWith('@matter'));
114
170
  };
@@ -130,6 +186,7 @@ export class PluginManager extends EventEmitter {
130
186
  this.log.error(`Please open an issue on the plugin repository to remove them.`);
131
187
  return null;
132
188
  }
189
+ // Check for matterbridge package in dependencies and devDependencies
133
190
  const checkForMatterbridgePackage = (dependencies) => {
134
191
  return Object.keys(dependencies).filter((pkg) => pkg === 'matterbridge');
135
192
  };
@@ -159,6 +216,12 @@ export class PluginManager extends EventEmitter {
159
216
  return null;
160
217
  }
161
218
  }
219
+ /**
220
+ * Get the author of a plugin from its package.json.
221
+ *
222
+ * @param {Record<string, string | number | Record<string, string | number | object>>} packageJson - The package.json object of the plugin.
223
+ * @returns {string} The author of the plugin, or 'Unknown author' if not found.
224
+ */
162
225
  getAuthor(packageJson) {
163
226
  if (packageJson.author && typeof packageJson.author === 'string')
164
227
  return packageJson.author;
@@ -166,6 +229,12 @@ export class PluginManager extends EventEmitter {
166
229
  return packageJson.author.name;
167
230
  return 'Unknown author';
168
231
  }
232
+ /**
233
+ * Get the homepage of a plugin from its package.json.
234
+ *
235
+ * @param {Record<string, string | number | Record<string, string | number | object>>} packageJson - The package.json object of the plugin.
236
+ * @returns {string | undefined} The homepage of the plugin, or undefined if not found.
237
+ */
169
238
  getHomepage(packageJson) {
170
239
  if (packageJson.homepage && typeof packageJson.homepage === 'string' && packageJson.homepage.includes('http')) {
171
240
  return packageJson.homepage.replace('git+', '').replace('.git', '');
@@ -174,7 +243,14 @@ export class PluginManager extends EventEmitter {
174
243
  return packageJson.repository.url.replace('git+', '').replace('.git', '');
175
244
  }
176
245
  }
246
+ /**
247
+ * Get the help URL of a plugin from its package.json.
248
+ *
249
+ * @param {Record<string, string | number | Record<string, string | number | object>>} packageJson - The package.json object of the plugin.
250
+ * @returns {string | undefined} The URL to the help page or to the README file, or undefined if not found.
251
+ */
177
252
  getHelp(packageJson) {
253
+ // If there's a help field that looks like a URL, return it.
178
254
  if (packageJson.help && typeof packageJson.help === 'string' && packageJson.help.startsWith('http')) {
179
255
  return packageJson.help;
180
256
  }
@@ -185,7 +261,14 @@ export class PluginManager extends EventEmitter {
185
261
  return packageJson.homepage.replace('git+', '').replace('.git', '');
186
262
  }
187
263
  }
264
+ /**
265
+ * Get the changelog URL of a plugin from its package.json.
266
+ *
267
+ * @param {Record<string, string | number | Record<string, string | number | object>>} packageJson - The package.json object of the plugin.
268
+ * @returns {string | undefined} The URL to the CHANGELOG file, or undefined if not found.
269
+ */
188
270
  getChangelog(packageJson) {
271
+ // If there's a changelog field that looks like a URL, return it.
189
272
  if (packageJson.changelog && typeof packageJson.changelog === 'string' && packageJson.changelog.startsWith('http')) {
190
273
  return packageJson.changelog;
191
274
  }
@@ -196,6 +279,13 @@ export class PluginManager extends EventEmitter {
196
279
  return packageJson.homepage.replace('git+', '').replace('.git', '');
197
280
  }
198
281
  }
282
+ /**
283
+ * Get the first funding URL(s) of a plugin from its package.json.
284
+ *
285
+ * @param {Record<string, any>} packageJson - The package.json object of the plugin.
286
+ * @returns {string | undefined} The first funding URLs, or undefined if not found.
287
+ */
288
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
199
289
  getFunding(packageJson) {
200
290
  const funding = packageJson.funding;
201
291
  if (!funding)
@@ -204,16 +294,25 @@ export class PluginManager extends EventEmitter {
204
294
  return;
205
295
  if (typeof funding === 'string' && funding.startsWith('http'))
206
296
  return funding;
297
+ // Normalize funding into an array.
207
298
  const fundingEntries = Array.isArray(funding) ? funding : [funding];
208
299
  for (const entry of fundingEntries) {
209
300
  if (entry && typeof entry === 'string' && entry.startsWith('http')) {
301
+ // If the funding entry is a string, assume it is a URL.
210
302
  return entry;
211
303
  }
212
304
  else if (entry && typeof entry === 'object' && typeof entry.url === 'string' && entry.url.startsWith('http')) {
305
+ // If it's an object with a 'url' property, use that.
213
306
  return entry.url;
214
307
  }
215
308
  }
216
309
  }
310
+ /**
311
+ * Loads and parse the plugin package.json and returns it.
312
+ *
313
+ * @param {RegisteredPlugin} plugin - The plugin to load the package from.
314
+ * @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.
315
+ */
217
316
  async parse(plugin) {
218
317
  const { promises } = await import('node:fs');
219
318
  try {
@@ -245,6 +344,7 @@ export class PluginManager extends EventEmitter {
245
344
  this.log.warn(`Plugin ${plg}${plugin.name}${wr} has no path`);
246
345
  if (!plugin.type)
247
346
  this.log.warn(`Plugin ${plg}${plugin.name}${wr} has no type`);
347
+ // Check for @project-chip and @matter packages in dependencies and devDependencies
248
348
  const checkForProjectChipPackages = (dependencies) => {
249
349
  return Object.keys(dependencies).filter((pkg) => pkg.startsWith('@project-chip') || pkg.startsWith('@matter'));
250
350
  };
@@ -266,6 +366,7 @@ export class PluginManager extends EventEmitter {
266
366
  this.log.error(`Please open an issue on the plugin repository to remove them.`);
267
367
  return null;
268
368
  }
369
+ // Check for matterbridge package in dependencies and devDependencies
269
370
  const checkForMatterbridgePackage = (dependencies) => {
270
371
  return Object.keys(dependencies).filter((pkg) => pkg === 'matterbridge');
271
372
  };
@@ -295,6 +396,16 @@ export class PluginManager extends EventEmitter {
295
396
  return null;
296
397
  }
297
398
  }
399
+ /**
400
+ * Enables a plugin by its name or path.
401
+ *
402
+ * This method enables a plugin by setting its `enabled` property to `true` and saving the updated
403
+ * plugin information to storage. It first checks if the plugin is already registered in the `_plugins` map.
404
+ * If not, it attempts to resolve the plugin's `package.json` file to retrieve its name and enable it.
405
+ *
406
+ * @param {string} nameOrPath - The name or path of the plugin to enable.
407
+ * @returns {Promise<RegisteredPlugin | null>} A promise that resolves to the enabled plugin object, or null if the plugin could not be enabled.
408
+ */
298
409
  async enable(nameOrPath) {
299
410
  const { promises } = await import('node:fs');
300
411
  if (!nameOrPath)
@@ -330,6 +441,16 @@ export class PluginManager extends EventEmitter {
330
441
  return null;
331
442
  }
332
443
  }
444
+ /**
445
+ * Enables a plugin by its name or path.
446
+ *
447
+ * This method enables a plugin by setting its `enabled` property to `true` and saving the updated
448
+ * plugin information to storage. It first checks if the plugin is already registered in the `_plugins` map.
449
+ * If not, it attempts to resolve the plugin's `package.json` file to retrieve its name and enable it.
450
+ *
451
+ * @param {string} nameOrPath - The name or path of the plugin to enable.
452
+ * @returns {Promise<RegisteredPlugin | null>} A promise that resolves to the enabled plugin object, or null if the plugin could not be enabled.
453
+ */
333
454
  async disable(nameOrPath) {
334
455
  const { promises } = await import('node:fs');
335
456
  if (!nameOrPath)
@@ -365,6 +486,16 @@ export class PluginManager extends EventEmitter {
365
486
  return null;
366
487
  }
367
488
  }
489
+ /**
490
+ * Removes a plugin by its name or path.
491
+ *
492
+ * This method removes a plugin from the `_plugins` map and saves the updated plugin information to storage.
493
+ * It first checks if the plugin is already registered in the `_plugins` map. If not, it attempts to resolve
494
+ * the plugin's `package.json` file to retrieve its name and remove it.
495
+ *
496
+ * @param {string} nameOrPath - The name or path of the plugin to remove.
497
+ * @returns {Promise<RegisteredPlugin | null>} A promise that resolves to the removed plugin object, or null if the plugin could not be removed.
498
+ */
368
499
  async remove(nameOrPath) {
369
500
  const { promises } = await import('node:fs');
370
501
  if (!nameOrPath)
@@ -400,6 +531,17 @@ export class PluginManager extends EventEmitter {
400
531
  return null;
401
532
  }
402
533
  }
534
+ /**
535
+ * Adds a plugin by its name or path.
536
+ *
537
+ * This method adds a plugin to the plugins map and saves the updated plugin information to storage.
538
+ * It first resolves the plugin's `package.json` file to retrieve its details. If the plugin is already
539
+ * registered, it logs an info message and returns null. Otherwise, it registers the plugin, enables it,
540
+ * and saves the updated plugin information to storage.
541
+ *
542
+ * @param {string} nameOrPath - The name or path of the plugin to add.
543
+ * @returns {Promise<RegisteredPlugin | null>} A promise that resolves to the added plugin object, or null if the plugin could not be added.
544
+ */
403
545
  async add(nameOrPath) {
404
546
  const { promises } = await import('node:fs');
405
547
  if (!nameOrPath)
@@ -435,6 +577,15 @@ export class PluginManager extends EventEmitter {
435
577
  return null;
436
578
  }
437
579
  }
580
+ /**
581
+ * Installs a plugin by its name.
582
+ *
583
+ * This method first uninstalls any existing version of the plugin, then installs the plugin globally using npm.
584
+ * It logs the installation process and retrieves the installed version of the plugin.
585
+ *
586
+ * @param {string} name - The name of the plugin to install.
587
+ * @returns {Promise<string | undefined>} A promise that resolves to the installed version of the plugin, or undefined if the installation failed.
588
+ */
438
589
  async install(name) {
439
590
  const { exec } = await import('node:child_process');
440
591
  await this.uninstall(name);
@@ -449,11 +600,14 @@ export class PluginManager extends EventEmitter {
449
600
  else {
450
601
  this.log.info(`Installed plugin ${plg}${name}${nf}`);
451
602
  this.log.debug(`Installed plugin ${plg}${name}${db}: ${stdout}`);
603
+ // Get the installed version
604
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
452
605
  exec(`npm list -g ${name} --depth=0`, (listError, listStdout, listStderr) => {
453
606
  if (listError) {
454
607
  this.log.error(`List error: ${listError}`);
455
608
  resolve(undefined);
456
609
  }
610
+ // Clean the output to get only the package name and version
457
611
  const lines = listStdout.split('\n');
458
612
  const versionLine = lines.find((line) => line.includes(`${name}@`));
459
613
  if (versionLine) {
@@ -470,6 +624,15 @@ export class PluginManager extends EventEmitter {
470
624
  });
471
625
  });
472
626
  }
627
+ /**
628
+ * Uninstalls a plugin by its name.
629
+ *
630
+ * This method uninstalls a globally installed plugin using npm. It logs the uninstallation process
631
+ * and returns the name of the uninstalled plugin if successful, or undefined if the uninstallation failed.
632
+ *
633
+ * @param {string} name - The name of the plugin to uninstall.
634
+ * @returns {Promise<string | undefined>} A promise that resolves to the name of the uninstalled plugin, or undefined if the uninstallation failed.
635
+ */
473
636
  async uninstall(name) {
474
637
  const { exec } = await import('node:child_process');
475
638
  this.log.info(`Uninstalling plugin ${plg}${name}${nf}`);
@@ -489,6 +652,15 @@ export class PluginManager extends EventEmitter {
489
652
  });
490
653
  });
491
654
  }
655
+ /**
656
+ * Loads a plugin and returns the corresponding MatterbridgePlatform instance.
657
+ *
658
+ * @param {RegisteredPlugin} plugin - The plugin to load.
659
+ * @param {boolean} start - Optional flag indicating whether to start the plugin after loading. Default is false.
660
+ * @param {string} message - Optional message to pass to the plugin when starting.
661
+ * @param {boolean} configure - Optional flag indicating whether to configure the plugin after loading. Default is false.
662
+ * @returns {Promise<MatterbridgePlatform | undefined>} A Promise that resolves to the loaded MatterbridgePlatform instance or undefined.
663
+ */
492
664
  async load(plugin, start = false, message = '', configure = false) {
493
665
  const { promises } = await import('node:fs');
494
666
  const { default: path } = await import('node:path');
@@ -502,15 +674,20 @@ export class PluginManager extends EventEmitter {
502
674
  }
503
675
  this.log.info(`Loading plugin ${plg}${plugin.name}${nf} type ${typ}${plugin.type}${nf}`);
504
676
  try {
677
+ // Load the package.json of the plugin
505
678
  const packageJson = JSON.parse(await promises.readFile(plugin.path, 'utf8'));
679
+ // Resolve the main module path relative to package.json
506
680
  const pluginEntry = path.resolve(path.dirname(plugin.path), packageJson.main);
681
+ // Dynamically import the plugin
507
682
  const { pathToFileURL } = await import('node:url');
508
683
  const pluginUrl = pathToFileURL(pluginEntry);
509
684
  this.log.debug(`Importing plugin ${plg}${plugin.name}${db} from ${pluginUrl.href}`);
510
685
  const pluginInstance = await import(pluginUrl.href);
511
686
  this.log.debug(`Imported plugin ${plg}${plugin.name}${db} from ${pluginUrl.href}`);
687
+ // Call the default export function of the plugin, passing this MatterBridge instance, the log and the config
512
688
  if (pluginInstance.default) {
513
689
  const config = await this.loadConfig(plugin);
690
+ // 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.
514
691
  plugin.name = packageJson.name;
515
692
  plugin.description = packageJson.description ?? 'No description';
516
693
  plugin.version = packageJson.version;
@@ -519,7 +696,7 @@ export class PluginManager extends EventEmitter {
519
696
  plugin.schemaJson = await this.loadSchema(plugin);
520
697
  config.name = plugin.name;
521
698
  config.version = packageJson.version;
522
- const log = new AnsiLogger({ logName: plugin.description ?? 'No description', logTimestampFormat: 4, logLevel: config.debug ? "debug" : this.matterbridge.log.logLevel });
699
+ const log = new AnsiLogger({ logName: plugin.description ?? 'No description', logTimestampFormat: 4 /* TimestampFormat.TIME_MILLIS */, logLevel: config.debug ? "debug" /* LogLevel.DEBUG */ : this.matterbridge.log.logLevel });
523
700
  const platform = pluginInstance.default(this.matterbridge, log, config);
524
701
  config.type = platform.type;
525
702
  platform.name = packageJson.name;
@@ -538,7 +715,7 @@ export class PluginManager extends EventEmitter {
538
715
  plugin.loaded = true;
539
716
  plugin.registeredDevices = 0;
540
717
  plugin.addedDevices = 0;
541
- await this.saveToStorage();
718
+ await this.saveToStorage(); // Save the plugin to storage
542
719
  this.log.notice(`Loaded plugin ${plg}${plugin.name}${nt} type ${typ}${platform.type}${nt} (entrypoint ${UNDERLINE}${pluginEntry}${UNDERLINEOFF})`);
543
720
  this.emit('loaded', plugin.name);
544
721
  if (start)
@@ -558,6 +735,14 @@ export class PluginManager extends EventEmitter {
558
735
  }
559
736
  return undefined;
560
737
  }
738
+ /**
739
+ * Starts a plugin.
740
+ *
741
+ * @param {RegisteredPlugin} plugin - The plugin to start.
742
+ * @param {string} [message] - Optional message to pass to the plugin's onStart method.
743
+ * @param {boolean} [configure] - Indicates whether to configure the plugin after starting (default false).
744
+ * @returns {Promise<RegisteredPlugin | undefined>} A promise that resolves when the plugin is started successfully, or rejects with an error if starting the plugin fails.
745
+ */
561
746
  async start(plugin, message, configure = false) {
562
747
  if (!plugin.loaded) {
563
748
  this.log.error(`Plugin ${plg}${plugin.name}${er} not loaded`);
@@ -588,6 +773,12 @@ export class PluginManager extends EventEmitter {
588
773
  }
589
774
  return undefined;
590
775
  }
776
+ /**
777
+ * Configures a plugin.
778
+ *
779
+ * @param {RegisteredPlugin} plugin - The plugin to configure.
780
+ * @returns {Promise<RegisteredPlugin | undefined>} A promise that resolves when the plugin is configured successfully, or rejects with an error if configuration fails.
781
+ */
591
782
  async configure(plugin) {
592
783
  if (!plugin.loaded) {
593
784
  this.log.error(`Plugin ${plg}${plugin.name}${er} not loaded`);
@@ -619,6 +810,18 @@ export class PluginManager extends EventEmitter {
619
810
  }
620
811
  return undefined;
621
812
  }
813
+ /**
814
+ * Shuts down a plugin.
815
+ *
816
+ * This method shuts down a plugin by calling its `onShutdown` method and resetting its state.
817
+ * It logs the shutdown process and optionally removes all devices associated with the plugin.
818
+ *
819
+ * @param {RegisteredPlugin} plugin - The plugin to shut down.
820
+ * @param {string} [reason] - The reason for shutting down the plugin.
821
+ * @param {boolean} [removeAllDevices] - Whether to remove all devices associated with the plugin.
822
+ * @param {boolean} [force] - Whether to force the shutdown even if the plugin is not loaded or started.
823
+ * @returns {Promise<RegisteredPlugin | undefined>} A promise that resolves to the shut down plugin object, or undefined if the shutdown failed.
824
+ */
622
825
  async shutdown(plugin, reason, removeAllDevices = false, force = false) {
623
826
  this.log.debug(`Shutting down plugin ${plg}${plugin.name}${db}`);
624
827
  if (!plugin.loaded) {
@@ -662,6 +865,15 @@ export class PluginManager extends EventEmitter {
662
865
  }
663
866
  return undefined;
664
867
  }
868
+ /**
869
+ * Loads the configuration for a plugin.
870
+ * If the configuration file exists, it reads the file and returns the parsed JSON data.
871
+ * If the configuration file does not exist, it creates a new file with default configuration and returns it.
872
+ * If any error occurs during file access or creation, it logs an error and return un empty config.
873
+ *
874
+ * @param {RegisteredPlugin} plugin - The plugin for which to load the configuration.
875
+ * @returns {Promise<PlatformConfig>} A promise that resolves to the loaded or created configuration.
876
+ */
665
877
  async loadConfig(plugin) {
666
878
  const { default: path } = await import('node:path');
667
879
  const { promises } = await import('node:fs');
@@ -672,6 +884,8 @@ export class PluginManager extends EventEmitter {
672
884
  const data = await promises.readFile(configFile, 'utf8');
673
885
  const config = JSON.parse(data);
674
886
  this.log.debug(`Loaded config file ${configFile} for plugin ${plg}${plugin.name}${db}.`);
887
+ // this.log.debug(`Loaded config file ${configFile} for plugin ${plg}${plugin.name}${db}.\nConfig:${rs}\n`, config);
888
+ // The first time a plugin is added to the system, the config file is created with the plugin name and type "AnyPlatform".
675
889
  config.name = plugin.name;
676
890
  config.type = plugin.type;
677
891
  if (config.debug === undefined)
@@ -695,6 +909,7 @@ export class PluginManager extends EventEmitter {
695
909
  try {
696
910
  await promises.writeFile(configFile, JSON.stringify(config, null, 2), 'utf8');
697
911
  this.log.debug(`Created config file ${configFile} for plugin ${plg}${plugin.name}${db}.`);
912
+ // this.log.debug(`Created config file ${configFile} for plugin ${plg}${plugin.name}${db}.\nConfig:${rs}\n`, config);
698
913
  return config;
699
914
  }
700
915
  catch (err) {
@@ -708,6 +923,19 @@ export class PluginManager extends EventEmitter {
708
923
  }
709
924
  }
710
925
  }
926
+ /**
927
+ * Saves the configuration of a plugin to a file.
928
+ *
929
+ * This method saves the configuration of the specified plugin to a JSON file in the matterbridge directory.
930
+ * If the plugin's configuration is not found, it logs an error and rejects the promise. If the configuration
931
+ * is successfully saved, it logs a debug message. If an error occurs during the file write operation, it logs
932
+ * the error and rejects the promise.
933
+ *
934
+ * @param {RegisteredPlugin} plugin - The plugin whose configuration is to be saved.
935
+ * @param {boolean} [restartRequired] - Indicates whether a restart is required after saving the configuration.
936
+ * @returns {Promise<void>} A promise that resolves when the configuration is successfully saved, or rejects if an error occurs.
937
+ * @throws {Error} If the plugin's configuration is not found.
938
+ */
711
939
  async saveConfigFromPlugin(plugin, restartRequired = false) {
712
940
  const { default: path } = await import('node:path');
713
941
  const { promises } = await import('node:fs');
@@ -722,6 +950,7 @@ export class PluginManager extends EventEmitter {
722
950
  if (restartRequired)
723
951
  plugin.restartRequired = true;
724
952
  this.log.debug(`Saved config file ${configFile} for plugin ${plg}${plugin.name}${db}`);
953
+ // this.log.debug(`Saved config file ${configFile} for plugin ${plg}${plugin.name}${db}.\nConfig:${rs}\n`, plugin.platform.config);
725
954
  return Promise.resolve();
726
955
  }
727
956
  catch (err) {
@@ -729,6 +958,20 @@ export class PluginManager extends EventEmitter {
729
958
  return Promise.reject(err);
730
959
  }
731
960
  }
961
+ /**
962
+ * Saves the configuration of a plugin from a JSON object to a file.
963
+ *
964
+ * This method saves the provided configuration of the specified plugin to a JSON file in the matterbridge directory.
965
+ * It first checks if the configuration data is valid by ensuring it contains the correct name and type, and matches
966
+ * the plugin's name. If the configuration data is invalid, it logs an error and returns. If the configuration is
967
+ * successfully saved, it updates the plugin's `configJson` property and logs a debug message. If an error occurs
968
+ * during the file write operation, it logs the error and returns.
969
+ *
970
+ * @param {RegisteredPlugin} plugin - The plugin whose configuration is to be saved.
971
+ * @param {PlatformConfig} config - The configuration data to be saved.
972
+ * @param {boolean} [restartRequired] - Indicates whether a restart is required after saving the configuration.
973
+ * @returns {Promise<void>} A promise that resolves when the configuration is successfully saved, or returns if an error occurs.
974
+ */
732
975
  async saveConfigFromJson(plugin, config, restartRequired = false) {
733
976
  const { default: path } = await import('node:path');
734
977
  const { promises } = await import('node:fs');
@@ -747,12 +990,23 @@ export class PluginManager extends EventEmitter {
747
990
  plugin.platform.onConfigChanged(config).catch((err) => this.log.error(`Error calling onConfigChanged for plugin ${plg}${plugin.name}${er}: ${err}`));
748
991
  }
749
992
  this.log.debug(`Saved config file ${configFile} for plugin ${plg}${plugin.name}${db}`);
993
+ // this.log.debug(`Saved config file ${configFile} for plugin ${plg}${plugin.name}${db}.\nConfig:${rs}\n`, config);
750
994
  }
751
995
  catch (err) {
752
996
  this.log.error(`Error saving config file ${configFile} for plugin ${plg}${plugin.name}${er}: ${err instanceof Error ? err.message + '\n' + err.stack : err}`);
753
997
  return;
754
998
  }
755
999
  }
1000
+ /**
1001
+ * Loads the schema for a plugin.
1002
+ *
1003
+ * This method attempts to load the schema file for the specified plugin. If the schema file is found,
1004
+ * it reads and parses the file, updates the schema's title and description, and logs the process.
1005
+ * If the schema file is not found, it logs the event and loads a default schema for the plugin.
1006
+ *
1007
+ * @param {RegisteredPlugin} plugin - The plugin whose schema is to be loaded.
1008
+ * @returns {Promise<PlatformSchema>} A promise that resolves to the loaded schema object, or the default schema if the schema file is not found.
1009
+ */
756
1010
  async loadSchema(plugin) {
757
1011
  const { promises } = await import('node:fs');
758
1012
  const schemaFile = plugin.path.replace('package.json', `${plugin.name}.schema.json`);
@@ -763,6 +1017,7 @@ export class PluginManager extends EventEmitter {
763
1017
  schema.title = plugin.description;
764
1018
  schema.description = plugin.name + ' v. ' + plugin.version + ' by ' + plugin.author;
765
1019
  this.log.debug(`Loaded schema file ${schemaFile} for plugin ${plg}${plugin.name}${db}.`);
1020
+ // this.log.debug(`Loaded schema file ${schemaFile} for plugin ${plg}${plugin.name}${db}.\nSchema:${rs}\n`, schema);
766
1021
  return schema;
767
1022
  }
768
1023
  catch (_err) {
@@ -770,6 +1025,16 @@ export class PluginManager extends EventEmitter {
770
1025
  return this.getDefaultSchema(plugin);
771
1026
  }
772
1027
  }
1028
+ /**
1029
+ * Returns the default schema for a plugin.
1030
+ *
1031
+ * This method generates a default schema object for the specified plugin. The schema includes
1032
+ * metadata such as the plugin's title, description, version, and author. It also defines the
1033
+ * properties of the schema, including the plugin's name, type, debug flag, and unregisterOnShutdown flag.
1034
+ *
1035
+ * @param {RegisteredPlugin} plugin - The plugin for which the default schema is to be generated.
1036
+ * @returns {PlatformSchema} The default schema object for the plugin.
1037
+ */
773
1038
  getDefaultSchema(plugin) {
774
1039
  return {
775
1040
  title: plugin.description,
@@ -800,3 +1065,4 @@ export class PluginManager extends EventEmitter {
800
1065
  };
801
1066
  }
802
1067
  }
1068
+ //# sourceMappingURL=pluginManager.js.map