matterbridge 3.1.1-dev-20250703-80c685d → 3.1.1

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