matterbridge 2.1.1 → 2.1.2-dev.3

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 (111) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/dist/cli.js +0 -26
  3. package/dist/cluster/export.js +0 -2
  4. package/dist/defaultConfigSchema.js +0 -23
  5. package/dist/deviceManager.js +3 -29
  6. package/dist/frontend.js +51 -245
  7. package/dist/index.js +0 -28
  8. package/dist/logger/export.js +0 -1
  9. package/dist/matter/behaviors.js +0 -2
  10. package/dist/matter/clusters.js +0 -2
  11. package/dist/matter/devices.js +0 -2
  12. package/dist/matter/endpoints.js +0 -2
  13. package/dist/matter/export.js +1 -2
  14. package/dist/matter/types.js +0 -2
  15. package/dist/matterbridge.js +38 -752
  16. package/dist/matterbridgeAccessoryPlatform.js +0 -33
  17. package/dist/matterbridgeBehaviors.js +1 -32
  18. package/dist/matterbridgeDeviceTypes.js +11 -112
  19. package/dist/matterbridgeDynamicPlatform.js +0 -33
  20. package/dist/matterbridgeEndpoint.js +6 -690
  21. package/dist/matterbridgeEndpointHelpers.js +0 -96
  22. package/dist/matterbridgePlatform.js +9 -129
  23. package/dist/matterbridgeTypes.js +0 -24
  24. package/dist/pluginManager.js +5 -243
  25. package/dist/storage/export.js +0 -1
  26. package/dist/utils/colorUtils.js +2 -205
  27. package/dist/utils/export.js +0 -1
  28. package/dist/utils/utils.js +7 -251
  29. package/npm-shrinkwrap.json +2 -2
  30. package/package.json +1 -2
  31. package/dist/cli.d.ts +0 -25
  32. package/dist/cli.d.ts.map +0 -1
  33. package/dist/cli.js.map +0 -1
  34. package/dist/cluster/export.d.ts +0 -2
  35. package/dist/cluster/export.d.ts.map +0 -1
  36. package/dist/cluster/export.js.map +0 -1
  37. package/dist/defaultConfigSchema.d.ts +0 -27
  38. package/dist/defaultConfigSchema.d.ts.map +0 -1
  39. package/dist/defaultConfigSchema.js.map +0 -1
  40. package/dist/deviceManager.d.ts +0 -46
  41. package/dist/deviceManager.d.ts.map +0 -1
  42. package/dist/deviceManager.js.map +0 -1
  43. package/dist/frontend.d.ts +0 -109
  44. package/dist/frontend.d.ts.map +0 -1
  45. package/dist/frontend.js.map +0 -1
  46. package/dist/index.d.ts +0 -35
  47. package/dist/index.d.ts.map +0 -1
  48. package/dist/index.js.map +0 -1
  49. package/dist/logger/export.d.ts +0 -2
  50. package/dist/logger/export.d.ts.map +0 -1
  51. package/dist/logger/export.js.map +0 -1
  52. package/dist/matter/behaviors.d.ts +0 -2
  53. package/dist/matter/behaviors.d.ts.map +0 -1
  54. package/dist/matter/behaviors.js.map +0 -1
  55. package/dist/matter/clusters.d.ts +0 -2
  56. package/dist/matter/clusters.d.ts.map +0 -1
  57. package/dist/matter/clusters.js.map +0 -1
  58. package/dist/matter/devices.d.ts +0 -2
  59. package/dist/matter/devices.d.ts.map +0 -1
  60. package/dist/matter/devices.js.map +0 -1
  61. package/dist/matter/endpoints.d.ts +0 -2
  62. package/dist/matter/endpoints.d.ts.map +0 -1
  63. package/dist/matter/endpoints.js.map +0 -1
  64. package/dist/matter/export.d.ts +0 -4
  65. package/dist/matter/export.d.ts.map +0 -1
  66. package/dist/matter/export.js.map +0 -1
  67. package/dist/matter/types.d.ts +0 -3
  68. package/dist/matter/types.d.ts.map +0 -1
  69. package/dist/matter/types.js.map +0 -1
  70. package/dist/matterbridge.d.ts +0 -409
  71. package/dist/matterbridge.d.ts.map +0 -1
  72. package/dist/matterbridge.js.map +0 -1
  73. package/dist/matterbridgeAccessoryPlatform.d.ts +0 -39
  74. package/dist/matterbridgeAccessoryPlatform.d.ts.map +0 -1
  75. package/dist/matterbridgeAccessoryPlatform.js.map +0 -1
  76. package/dist/matterbridgeBehaviors.d.ts +0 -1056
  77. package/dist/matterbridgeBehaviors.d.ts.map +0 -1
  78. package/dist/matterbridgeBehaviors.js.map +0 -1
  79. package/dist/matterbridgeDeviceTypes.d.ts +0 -177
  80. package/dist/matterbridgeDeviceTypes.d.ts.map +0 -1
  81. package/dist/matterbridgeDeviceTypes.js.map +0 -1
  82. package/dist/matterbridgeDynamicPlatform.d.ts +0 -39
  83. package/dist/matterbridgeDynamicPlatform.d.ts.map +0 -1
  84. package/dist/matterbridgeDynamicPlatform.js.map +0 -1
  85. package/dist/matterbridgeEndpoint.d.ts +0 -834
  86. package/dist/matterbridgeEndpoint.d.ts.map +0 -1
  87. package/dist/matterbridgeEndpoint.js.map +0 -1
  88. package/dist/matterbridgeEndpointHelpers.d.ts +0 -2262
  89. package/dist/matterbridgeEndpointHelpers.d.ts.map +0 -1
  90. package/dist/matterbridgeEndpointHelpers.js.map +0 -1
  91. package/dist/matterbridgePlatform.d.ts +0 -164
  92. package/dist/matterbridgePlatform.d.ts.map +0 -1
  93. package/dist/matterbridgePlatform.js.map +0 -1
  94. package/dist/matterbridgeTypes.d.ts +0 -165
  95. package/dist/matterbridgeTypes.d.ts.map +0 -1
  96. package/dist/matterbridgeTypes.js.map +0 -1
  97. package/dist/pluginManager.d.ts +0 -238
  98. package/dist/pluginManager.d.ts.map +0 -1
  99. package/dist/pluginManager.js.map +0 -1
  100. package/dist/storage/export.d.ts +0 -2
  101. package/dist/storage/export.d.ts.map +0 -1
  102. package/dist/storage/export.js.map +0 -1
  103. package/dist/utils/colorUtils.d.ts +0 -61
  104. package/dist/utils/colorUtils.d.ts.map +0 -1
  105. package/dist/utils/colorUtils.js.map +0 -1
  106. package/dist/utils/export.d.ts +0 -3
  107. package/dist/utils/export.d.ts.map +0 -1
  108. package/dist/utils/export.js.map +0 -1
  109. package/dist/utils/utils.d.ts +0 -221
  110. package/dist/utils/utils.d.ts.map +0 -1
  111. package/dist/utils/utils.js.map +0 -1
@@ -1,28 +1,4 @@
1
- /**
2
- * This file contains the Plugins class.
3
- *
4
- * @file plugins.ts
5
- * @author Luca Liguori
6
- * @date 2024-07-14
7
- * @version 1.1.0
8
- *
9
- * Copyright 2024, 2025, 2026 Luca Liguori.
10
- *
11
- * Licensed under the Apache License, Version 2.0 (the "License");
12
- * you may not use this file except in compliance with the License.
13
- * You may obtain a copy of the License at
14
- *
15
- * http://www.apache.org/licenses/LICENSE-2.0
16
- *
17
- * Unless required by applicable law or agreed to in writing, software
18
- * distributed under the License is distributed on an "AS IS" BASIS,
19
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
- * See the License for the specific language governing permissions and
21
- * limitations under the License. *
22
- */
23
- // NodeStorage and AnsiLogger modules
24
1
  import { AnsiLogger, BLUE, db, er, nf, nt, rs, UNDERLINE, UNDERLINEOFF, wr } from './logger/export.js';
25
- // Node.js modules
26
2
  import path from 'path';
27
3
  import { promises as fs } from 'fs';
28
4
  import { pathToFileURL } from 'url';
@@ -36,9 +12,8 @@ export class PluginManager {
36
12
  log;
37
13
  constructor(matterbridge) {
38
14
  this.matterbridge = matterbridge;
39
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
40
15
  this.nodeContext = matterbridge.nodeContext;
41
- this.log = new AnsiLogger({ logName: 'PluginManager', logTimestampFormat: 4 /* TimestampFormat.TIME_MILLIS */, logLevel: matterbridge.log.logLevel });
16
+ this.log = new AnsiLogger({ logName: 'PluginManager', logTimestampFormat: 4, logLevel: matterbridge.log.logLevel });
42
17
  this.log.debug('Matterbridge plugin manager starting...');
43
18
  }
44
19
  get length() {
@@ -67,48 +42,28 @@ export class PluginManager {
67
42
  return this._plugins.values();
68
43
  }
69
44
  async forEach(callback) {
45
+ if (this.size === 0)
46
+ return;
70
47
  const tasks = Array.from(this._plugins.values()).map(async (plugin) => {
71
48
  try {
72
49
  await callback(plugin);
73
50
  }
74
51
  catch (error) {
75
52
  this.log.error(`Error processing forEach plugin ${plg}${plugin.name}${er}:`, error);
76
- // throw error;
77
53
  }
78
54
  });
79
55
  await Promise.all(tasks);
80
56
  }
81
- get logLevel() {
82
- return this.log.logLevel;
83
- }
84
57
  set logLevel(logLevel) {
85
58
  this.log.logLevel = logLevel;
86
59
  }
87
- /**
88
- * Loads registered plugins from storage.
89
- *
90
- * This method retrieves an array of registered plugins from the storage and converts it
91
- * into a map where the plugin names are the keys and the plugin objects are the values.
92
- *
93
- * @returns {Promise<RegisteredPlugin[]>} A promise that resolves to an array of registered plugins.
94
- */
95
60
  async loadFromStorage() {
96
- // Load the array from storage and convert it to a map
97
61
  const pluginsArray = await this.nodeContext.get('plugins', []);
98
62
  for (const plugin of pluginsArray)
99
63
  this._plugins.set(plugin.name, plugin);
100
64
  return pluginsArray;
101
65
  }
102
- /**
103
- * Loads registered plugins from storage.
104
- *
105
- * This method retrieves an array of registered plugins from the storage and converts it
106
- * into a map where the plugin names are the keys and the plugin objects are the values.
107
- *
108
- * @returns {Promise<RegisteredPlugin[]>} A promise that resolves to an array of registered plugins.
109
- */
110
66
  async saveToStorage() {
111
- // Convert the map to an array
112
67
  const plugins = [];
113
68
  const pluginArrayFromMap = Array.from(this._plugins.values());
114
69
  for (const plugin of pluginArrayFromMap) {
@@ -128,18 +83,11 @@ export class PluginManager {
128
83
  this.log.debug(`Saved ${BLUE}${plugins.length}${db} plugins to storage`);
129
84
  return plugins.length;
130
85
  }
131
- /**
132
- * Resolves the name of a plugin by loading and parsing its package.json file.
133
- * @param pluginPath - The path to the plugin or the path to the plugin's package.json file.
134
- * @returns The path to the resolved package.json file, or null if the package.json file is not found or does not contain a name.
135
- */
136
86
  async resolve(pluginPath) {
137
87
  if (!pluginPath.endsWith('package.json'))
138
88
  pluginPath = path.join(pluginPath, 'package.json');
139
- // Resolve the package.json of the plugin
140
89
  let packageJsonPath = path.resolve(pluginPath);
141
90
  this.log.debug(`Resolving plugin path ${plg}${packageJsonPath}${db}`);
142
- // Check if the package.json file exists
143
91
  try {
144
92
  await fs.access(packageJsonPath);
145
93
  }
@@ -149,13 +97,11 @@ export class PluginManager {
149
97
  this.log.debug(`Trying at ${plg}${packageJsonPath}${db}`);
150
98
  }
151
99
  try {
152
- // Load the package.json of the plugin
153
100
  const packageJson = JSON.parse(await fs.readFile(packageJsonPath, 'utf8'));
154
101
  if (!packageJson.name) {
155
102
  this.log.error(`Package.json name not found at ${packageJsonPath}`);
156
103
  return null;
157
104
  }
158
- // Check for main issues
159
105
  if (!packageJson.type || packageJson.type !== 'module') {
160
106
  this.log.error(`Plugin at ${packageJsonPath} is not a module`);
161
107
  return null;
@@ -164,13 +110,6 @@ export class PluginManager {
164
110
  this.log.error(`Plugin at ${packageJsonPath} has no main entrypoint in package.json`);
165
111
  return null;
166
112
  }
167
- /*
168
- if (!packageJson.types) {
169
- this.log.error(`Plugin at ${packageJsonPath} has no types in package.json`);
170
- return null;
171
- }
172
- */
173
- // Check for @project-chip packages in dependencies and devDependencies
174
113
  const checkForProjectChipPackages = (dependencies) => {
175
114
  return Object.keys(dependencies).filter((pkg) => pkg.startsWith('@project-chip') || pkg.startsWith('@matter'));
176
115
  };
@@ -192,7 +131,6 @@ export class PluginManager {
192
131
  this.log.error(`Please open an issue on the plugin repository to remove them.`);
193
132
  return null;
194
133
  }
195
- // Check for matterbridge package in dependencies and devDependencies
196
134
  const checkForMatterbridgePackage = (dependencies) => {
197
135
  return Object.keys(dependencies).filter((pkg) => pkg === 'matterbridge');
198
136
  };
@@ -222,11 +160,6 @@ export class PluginManager {
222
160
  return null;
223
161
  }
224
162
  }
225
- /**
226
- * Loads and parse the plugin package.json and returns it.
227
- * @param plugin - The plugin to load the package from.
228
- * @returns A Promise that resolves to the package.json object or undefined if the package.json could not be loaded.
229
- */
230
163
  async parse(plugin) {
231
164
  this.log.debug(`Parsing package.json of plugin ${plg}${plugin.name}${db}`);
232
165
  try {
@@ -243,7 +176,6 @@ export class PluginManager {
243
176
  this.log.error(`Plugin ${plg}${plugin.name}${er} is not a module`);
244
177
  if (!packageJson.main)
245
178
  this.log.error(`Plugin ${plg}${plugin.name}${er} has no main entrypoint in package.json`);
246
- // if (!packageJson.types) this.log.error(`Plugin ${plg}${plugin.name}${er} has no types in package.json`);
247
179
  plugin.name = packageJson.name || 'Unknown name';
248
180
  plugin.version = packageJson.version || '1.0.0';
249
181
  plugin.description = packageJson.description || 'Unknown description';
@@ -252,7 +184,6 @@ export class PluginManager {
252
184
  this.log.warn(`Plugin ${plg}${plugin.name}${wr} has no path`);
253
185
  if (!plugin.type)
254
186
  this.log.warn(`Plugin ${plg}${plugin.name}${wr} has no type`);
255
- // Check for @project-chip packages in dependencies and devDependencies
256
187
  const checkForProjectChipPackages = (dependencies) => {
257
188
  return Object.keys(dependencies).filter((pkg) => pkg.startsWith('@project-chip') || pkg.startsWith('@matter'));
258
189
  };
@@ -274,7 +205,6 @@ export class PluginManager {
274
205
  this.log.error(`Please open an issue on the plugin repository to remove them.`);
275
206
  return null;
276
207
  }
277
- // Check for matterbridge package in dependencies and devDependencies
278
208
  const checkForMatterbridgePackage = (dependencies) => {
279
209
  return Object.keys(dependencies).filter((pkg) => pkg === 'matterbridge');
280
210
  };
@@ -296,7 +226,6 @@ export class PluginManager {
296
226
  this.log.error(`Please open an issue on the plugin repository to remove them.`);
297
227
  return null;
298
228
  }
299
- // await this.saveToStorage(); // No need to save the plugin to storage
300
229
  return packageJson;
301
230
  }
302
231
  catch (err) {
@@ -305,16 +234,6 @@ export class PluginManager {
305
234
  return null;
306
235
  }
307
236
  }
308
- /**
309
- * Enables a plugin by its name or path.
310
- *
311
- * This method enables a plugin by setting its `enabled` property to `true` and saving the updated
312
- * plugin information to storage. It first checks if the plugin is already registered in the `_plugins` map.
313
- * If not, it attempts to resolve the plugin's `package.json` file to retrieve its name and enable it.
314
- *
315
- * @param {string} nameOrPath - The name or path of the plugin to enable.
316
- * @returns {Promise<RegisteredPlugin | null>} A promise that resolves to the enabled plugin object, or null if the plugin could not be enabled.
317
- */
318
237
  async enable(nameOrPath) {
319
238
  if (!nameOrPath || nameOrPath === '')
320
239
  return null;
@@ -347,16 +266,6 @@ export class PluginManager {
347
266
  return null;
348
267
  }
349
268
  }
350
- /**
351
- * Enables a plugin by its name or path.
352
- *
353
- * This method enables a plugin by setting its `enabled` property to `true` and saving the updated
354
- * plugin information to storage. It first checks if the plugin is already registered in the `_plugins` map.
355
- * If not, it attempts to resolve the plugin's `package.json` file to retrieve its name and enable it.
356
- *
357
- * @param {string} nameOrPath - The name or path of the plugin to enable.
358
- * @returns {Promise<RegisteredPlugin | null>} A promise that resolves to the enabled plugin object, or null if the plugin could not be enabled.
359
- */
360
269
  async disable(nameOrPath) {
361
270
  if (!nameOrPath || nameOrPath === '')
362
271
  return null;
@@ -389,16 +298,6 @@ export class PluginManager {
389
298
  return null;
390
299
  }
391
300
  }
392
- /**
393
- * Removes a plugin by its name or path.
394
- *
395
- * This method removes a plugin from the `_plugins` map and saves the updated plugin information to storage.
396
- * It first checks if the plugin is already registered in the `_plugins` map. If not, it attempts to resolve
397
- * the plugin's `package.json` file to retrieve its name and remove it.
398
- *
399
- * @param {string} nameOrPath - The name or path of the plugin to remove.
400
- * @returns {Promise<RegisteredPlugin | null>} A promise that resolves to the removed plugin object, or null if the plugin could not be removed.
401
- */
402
301
  async remove(nameOrPath) {
403
302
  if (!nameOrPath || nameOrPath === '')
404
303
  return null;
@@ -431,17 +330,6 @@ export class PluginManager {
431
330
  return null;
432
331
  }
433
332
  }
434
- /**
435
- * Adds a plugin by its name or path.
436
- *
437
- * This method adds a plugin to the `_plugins` map and saves the updated plugin information to storage.
438
- * It first resolves the plugin's `package.json` file to retrieve its details. If the plugin is already
439
- * registered, it logs an info message and returns null. Otherwise, it registers the plugin, enables it,
440
- * and saves the updated plugin information to storage.
441
- *
442
- * @param {string} nameOrPath - The name or path of the plugin to add.
443
- * @returns {Promise<RegisteredPlugin | null>} A promise that resolves to the added plugin object, or null if the plugin could not be added.
444
- */
445
333
  async add(nameOrPath) {
446
334
  if (!nameOrPath || nameOrPath === '')
447
335
  return null;
@@ -467,19 +355,9 @@ export class PluginManager {
467
355
  return null;
468
356
  }
469
357
  }
470
- /**
471
- * Installs a plugin by its name.
472
- *
473
- * This method first uninstalls any existing version of the plugin, then installs the plugin globally using npm.
474
- * It logs the installation process and retrieves the installed version of the plugin.
475
- *
476
- * @param {string} name - The name of the plugin to install.
477
- * @returns {Promise<string | undefined>} A promise that resolves to the installed version of the plugin, or undefined if the installation failed.
478
- */
479
358
  async install(name) {
480
359
  await this.uninstall(name);
481
360
  this.log.info(`Installing plugin ${plg}${name}${nf}`);
482
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
483
361
  return new Promise((resolve, reject) => {
484
362
  exec(`npm install -g ${name} --omit=dev --force`, (error, stdout, stderr) => {
485
363
  if (error) {
@@ -490,14 +368,11 @@ export class PluginManager {
490
368
  else {
491
369
  this.log.info(`Installed plugin ${plg}${name}${nf}`);
492
370
  this.log.debug(`Installed plugin ${plg}${name}${db}: ${stdout}`);
493
- // Get the installed version
494
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
495
371
  exec(`npm list -g ${name} --depth=0`, (listError, listStdout, listStderr) => {
496
372
  if (listError) {
497
373
  this.log.error(`List error: ${listError}`);
498
374
  resolve(undefined);
499
375
  }
500
- // Clean the output to get only the package name and version
501
376
  const lines = listStdout.split('\n');
502
377
  const versionLine = lines.find((line) => line.includes(`${name}@`));
503
378
  if (versionLine) {
@@ -513,18 +388,8 @@ export class PluginManager {
513
388
  });
514
389
  });
515
390
  }
516
- /**
517
- * Uninstalls a plugin by its name.
518
- *
519
- * This method uninstalls a globally installed plugin using npm. It logs the uninstallation process
520
- * and returns the name of the uninstalled plugin if successful, or undefined if the uninstallation failed.
521
- *
522
- * @param {string} name - The name of the plugin to uninstall.
523
- * @returns {Promise<string | undefined>} A promise that resolves to the name of the uninstalled plugin, or undefined if the uninstallation failed.
524
- */
525
391
  async uninstall(name) {
526
392
  this.log.info(`Uninstalling plugin ${plg}${name}${nf}`);
527
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
528
393
  return new Promise((resolve, reject) => {
529
394
  exec(`npm uninstall -g ${name} --force`, (error, stdout, stderr) => {
530
395
  if (error) {
@@ -540,14 +405,6 @@ export class PluginManager {
540
405
  });
541
406
  });
542
407
  }
543
- /**
544
- * Loads a plugin and returns the corresponding MatterbridgePlatform instance.
545
- * @param plugin - The plugin to load.
546
- * @param start - Optional flag indicating whether to start the plugin after loading. Default is false.
547
- * @param message - Optional message to pass to the plugin when starting.
548
- * @returns A Promise that resolves to the loaded MatterbridgePlatform instance.
549
- * @throws An error if the plugin is not enabled, already loaded, or fails to load.
550
- */
551
408
  async load(plugin, start = false, message = '', configure = false) {
552
409
  if (!plugin.enabled) {
553
410
  this.log.error(`Plugin ${plg}${plugin.name}${er} not enabled`);
@@ -559,19 +416,14 @@ export class PluginManager {
559
416
  }
560
417
  this.log.info(`Loading plugin ${plg}${plugin.name}${nf} type ${typ}${plugin.type}${nf}`);
561
418
  try {
562
- // Load the package.json of the plugin
563
419
  const packageJson = JSON.parse(await fs.readFile(plugin.path, 'utf8'));
564
- // Resolve the main module path relative to package.json
565
420
  const pluginEntry = path.resolve(path.dirname(plugin.path), packageJson.main);
566
- // Dynamically import the plugin
567
421
  const pluginUrl = pathToFileURL(pluginEntry);
568
422
  this.log.debug(`Importing plugin ${plg}${plugin.name}${db} from ${pluginUrl.href}`);
569
423
  const pluginInstance = await import(pluginUrl.href);
570
424
  this.log.debug(`Imported plugin ${plg}${plugin.name}${db} from ${pluginUrl.href}`);
571
- // Call the default export function of the plugin, passing this MatterBridge instance, the log and the config
572
425
  if (pluginInstance.default) {
573
426
  const config = await this.loadConfig(plugin);
574
- // 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.
575
427
  plugin.name = packageJson.name;
576
428
  plugin.description = packageJson.description ?? 'No description';
577
429
  plugin.version = packageJson.version;
@@ -580,7 +432,7 @@ export class PluginManager {
580
432
  plugin.schemaJson = await this.loadSchema(plugin);
581
433
  config.name = plugin.name;
582
434
  config.version = packageJson.version;
583
- const log = new AnsiLogger({ logName: plugin.description ?? 'No description', logTimestampFormat: 4 /* TimestampFormat.TIME_MILLIS */, logLevel: config.debug ? "debug" /* LogLevel.DEBUG */ : this.matterbridge.log.logLevel });
435
+ const log = new AnsiLogger({ logName: plugin.description ?? 'No description', logTimestampFormat: 4, logLevel: config.debug ? "debug" : this.matterbridge.log.logLevel });
584
436
  const platform = pluginInstance.default(this.matterbridge, log, config);
585
437
  config.type = platform.type;
586
438
  platform.name = packageJson.name;
@@ -597,7 +449,7 @@ export class PluginManager {
597
449
  plugin.addedDevices = 0;
598
450
  plugin.configJson = config;
599
451
  plugin.schemaJson = await this.loadSchema(plugin);
600
- await this.saveToStorage(); // Save the plugin to storage
452
+ await this.saveToStorage();
601
453
  this.log.notice(`Loaded plugin ${plg}${plugin.name}${nf} type ${typ}${platform.type}${db} (entrypoint ${UNDERLINE}${pluginEntry}${UNDERLINEOFF})`);
602
454
  if (start)
603
455
  await this.start(plugin, message, false);
@@ -616,14 +468,6 @@ export class PluginManager {
616
468
  }
617
469
  return undefined;
618
470
  }
619
- /**
620
- * Starts a plugin.
621
- *
622
- * @param {RegisteredPlugin} plugin - The plugin to start.
623
- * @param {string} [message] - Optional message to pass to the plugin's onStart method.
624
- * @param {boolean} [configure] - Indicates whether to configure the plugin after starting (default false).
625
- * @returns {Promise<RegisteredPlugin | undefined>} A promise that resolves when the plugin is started successfully, or rejects with an error if starting the plugin fails.
626
- */
627
471
  async start(plugin, message, configure = false) {
628
472
  if (!plugin.loaded) {
629
473
  this.log.error(`Plugin ${plg}${plugin.name}${er} not loaded`);
@@ -653,12 +497,6 @@ export class PluginManager {
653
497
  }
654
498
  return undefined;
655
499
  }
656
- /**
657
- * Configures a plugin.
658
- *
659
- * @param {RegisteredPlugin} plugin - The plugin to configure.
660
- * @returns {Promise<void>} A promise that resolves when the plugin is configured successfully, or rejects with an error if configuration fails.
661
- */
662
500
  async configure(plugin) {
663
501
  if (!plugin.loaded) {
664
502
  this.log.error(`Plugin ${plg}${plugin.name}${er} not loaded`);
@@ -681,7 +519,6 @@ export class PluginManager {
681
519
  await plugin.platform.onConfigure();
682
520
  this.log.notice(`Configured plugin ${plg}${plugin.name}${nt} type ${typ}${plugin.type}${nt}`);
683
521
  plugin.configured = true;
684
- // await this.saveConfigFromPlugin(plugin);
685
522
  return plugin;
686
523
  }
687
524
  catch (err) {
@@ -690,18 +527,6 @@ export class PluginManager {
690
527
  }
691
528
  return undefined;
692
529
  }
693
- /**
694
- * Shuts down a plugin.
695
- *
696
- * This method shuts down a plugin by calling its `onShutdown` method and resetting its state.
697
- * It logs the shutdown process and optionally removes all devices associated with the plugin.
698
- *
699
- * @param {RegisteredPlugin} plugin - The plugin to shut down.
700
- * @param {string} [reason] - The reason for shutting down the plugin.
701
- * @param {boolean} [removeAllDevices=false] - Whether to remove all devices associated with the plugin.
702
- * @param {boolean} [force=false] - Whether to force the shutdown even if the plugin is not loaded or started.
703
- * @returns {Promise<RegisteredPlugin | undefined>} A promise that resolves to the shut down plugin object, or undefined if the shutdown failed.
704
- */
705
530
  async shutdown(plugin, reason, removeAllDevices = false, force = false) {
706
531
  this.log.debug(`Shutting down plugin ${plg}${plugin.name}${db}`);
707
532
  if (!plugin.loaded) {
@@ -744,15 +569,6 @@ export class PluginManager {
744
569
  }
745
570
  return undefined;
746
571
  }
747
- /**
748
- * Loads the configuration for a plugin.
749
- * If the configuration file exists, it reads the file and returns the parsed JSON data.
750
- * If the configuration file does not exist, it creates a new file with default configuration and returns it.
751
- * If any error occurs during file access or creation, it logs an error and return un empty config.
752
- *
753
- * @param plugin - The plugin for which to load the configuration.
754
- * @returns A promise that resolves to the loaded or created configuration.
755
- */
756
572
  async loadConfig(plugin) {
757
573
  const configFile = path.join(this.matterbridge.matterbridgeDirectory, `${plugin.name}.config.json`);
758
574
  try {
@@ -760,8 +576,6 @@ export class PluginManager {
760
576
  const data = await fs.readFile(configFile, 'utf8');
761
577
  const config = JSON.parse(data);
762
578
  this.log.debug(`Loaded config file ${configFile} for plugin ${plg}${plugin.name}${db}.`);
763
- // this.log.debug(`Loaded config file ${configFile} for plugin ${plg}${plugin.name}${db}.\nConfig:${rs}\n`, config);
764
- // The first time a plugin is added to the system, the config file is created with the plugin name and type "AnyPlatform".
765
579
  config.name = plugin.name;
766
580
  config.type = plugin.type;
767
581
  if (config.debug === undefined)
@@ -786,7 +600,6 @@ export class PluginManager {
786
600
  try {
787
601
  await fs.writeFile(configFile, JSON.stringify(config, null, 2), 'utf8');
788
602
  this.log.debug(`Created config file ${configFile} for plugin ${plg}${plugin.name}${db}.`);
789
- // this.log.debug(`Created config file ${configFile} for plugin ${plg}${plugin.name}${db}.\nConfig:${rs}\n`, config);
790
603
  return config;
791
604
  }
792
605
  catch (err) {
@@ -803,18 +616,6 @@ export class PluginManager {
803
616
  return { name: plugin.name, type: plugin.type, debug: false, unregisterOnShutdown: false };
804
617
  }
805
618
  }
806
- /**
807
- * Saves the configuration of a plugin to a file.
808
- *
809
- * This method saves the configuration of the specified plugin to a JSON file in the matterbridge directory.
810
- * If the plugin's configuration is not found, it logs an error and rejects the promise. If the configuration
811
- * is successfully saved, it logs a debug message. If an error occurs during the file write operation, it logs
812
- * the error and rejects the promise.
813
- *
814
- * @param {RegisteredPlugin} plugin - The plugin whose configuration is to be saved.
815
- * @returns {Promise<void>} A promise that resolves when the configuration is successfully saved, or rejects if an error occurs.
816
- * @throws {Error} If the plugin's configuration is not found.
817
- */
818
619
  async saveConfigFromPlugin(plugin) {
819
620
  if (!plugin.platform?.config) {
820
621
  this.log.error(`Error saving config file for plugin ${plg}${plugin.name}${er}: config not found`);
@@ -824,7 +625,6 @@ export class PluginManager {
824
625
  try {
825
626
  await fs.writeFile(configFile, JSON.stringify(plugin.platform.config, null, 2), 'utf8');
826
627
  this.log.debug(`Saved config file ${configFile} for plugin ${plg}${plugin.name}${db}`);
827
- // this.log.debug(`Saved config file ${configFile} for plugin ${plg}${plugin.name}${db}.\nConfig:${rs}\n`, plugin.platform.config);
828
628
  return Promise.resolve();
829
629
  }
830
630
  catch (err) {
@@ -832,19 +632,6 @@ export class PluginManager {
832
632
  return Promise.reject(err);
833
633
  }
834
634
  }
835
- /**
836
- * Saves the configuration of a plugin from a JSON object to a file.
837
- *
838
- * This method saves the provided configuration of the specified plugin to a JSON file in the matterbridge directory.
839
- * It first checks if the configuration data is valid by ensuring it contains the correct name and type, and matches
840
- * the plugin's name. If the configuration data is invalid, it logs an error and returns. If the configuration is
841
- * successfully saved, it updates the plugin's `configJson` property and logs a debug message. If an error occurs
842
- * during the file write operation, it logs the error and returns.
843
- *
844
- * @param {RegisteredPlugin} plugin - The plugin whose configuration is to be saved.
845
- * @param {PlatformConfig} config - The configuration data to be saved.
846
- * @returns {Promise<void>} A promise that resolves when the configuration is successfully saved, or returns if an error occurs.
847
- */
848
635
  async saveConfigFromJson(plugin, config) {
849
636
  if (!config.name || !config.type || config.name !== plugin.name) {
850
637
  this.log.error(`Error saving config file for plugin ${plg}${plugin.name}${er}. Wrong config data content:${rs}\n`, config);
@@ -855,24 +642,12 @@ export class PluginManager {
855
642
  await fs.writeFile(configFile, JSON.stringify(config, null, 2), 'utf8');
856
643
  plugin.configJson = config;
857
644
  this.log.debug(`Saved config file ${configFile} for plugin ${plg}${plugin.name}${db}`);
858
- // this.log.debug(`Saved config file ${configFile} for plugin ${plg}${plugin.name}${db}.\nConfig:${rs}\n`, config);
859
645
  }
860
646
  catch (err) {
861
647
  this.log.error(`Error saving config file ${configFile} for plugin ${plg}${plugin.name}${er}: ${err}`);
862
648
  return;
863
649
  }
864
650
  }
865
- /**
866
- * Loads the schema for a plugin.
867
- *
868
- * This method attempts to load the schema file for the specified plugin. If the schema file is found,
869
- * it reads and parses the file, updates the schema's title and description, and logs the process.
870
- * It also attempts to delete any old schema file from the matterbridge directory. If the schema file
871
- * is not found, it logs the event and loads a default schema for the plugin.
872
- *
873
- * @param {RegisteredPlugin} plugin - The plugin whose schema is to be loaded.
874
- * @returns {Promise<PlatformSchema>} A promise that resolves to the loaded schema object, or the default schema if the schema file is not found.
875
- */
876
651
  async loadSchema(plugin) {
877
652
  const schemaFile = plugin.path.replace('package.json', `${plugin.name}.schema.json`);
878
653
  try {
@@ -882,8 +657,6 @@ export class PluginManager {
882
657
  schema.title = plugin.description;
883
658
  schema.description = plugin.name + ' v. ' + plugin.version + ' by ' + plugin.author;
884
659
  this.log.debug(`Loaded schema file ${schemaFile} for plugin ${plg}${plugin.name}${db}.`);
885
- // this.log.debug(`Loaded schema file ${schemaFile} for plugin ${plg}${plugin.name}${db}.\nSchema:${rs}\n`, schema);
886
- // Delete the schema file from old position
887
660
  return schema;
888
661
  }
889
662
  catch (error) {
@@ -891,16 +664,6 @@ export class PluginManager {
891
664
  return this.getDefaultSchema(plugin);
892
665
  }
893
666
  }
894
- /**
895
- * Returns the default schema for a plugin.
896
- *
897
- * This method generates a default schema object for the specified plugin. The schema includes
898
- * metadata such as the plugin's title, description, version, and author. It also defines the
899
- * properties of the schema, including the plugin's name, type, debug flag, and unregisterOnShutdown flag.
900
- *
901
- * @param {RegisteredPlugin} plugin - The plugin for which the default schema is to be generated.
902
- * @returns {PlatformSchema} The default schema object for the plugin.
903
- */
904
667
  getDefaultSchema(plugin) {
905
668
  return {
906
669
  title: plugin.description,
@@ -931,4 +694,3 @@ export class PluginManager {
931
694
  };
932
695
  }
933
696
  }
934
- //# sourceMappingURL=pluginManager.js.map
@@ -1,2 +1 @@
1
1
  export * from 'node-persist-manager';
2
- //# sourceMappingURL=export.js.map