MCPcopy
hub / github.com/homebridge/homebridge / loadInstalledPlugins

Method loadInstalledPlugins

src/pluginManager.ts:362–425  ·  view source on GitHub ↗

* Gets all plugins installed on the local system

()

Source from the content-addressed store, hash-verified

360 * Gets all plugins installed on the local system
361 */
362 private loadInstalledPlugins(): void {
363 this.loadDefaultPaths()
364
365 this.searchPaths.forEach((searchPath) => { // search for plugins among all known paths
366 if (!existsSync(searchPath)) { // just because this path is in require.main.paths doesn't mean it necessarily exists!
367 return
368 }
369
370 if (existsSync(join(searchPath, 'package.json'))) { // does this path point inside a single plugin and not a directory containing plugins?
371 try {
372 this.loadPlugin(searchPath)
373 } catch (error: any) {
374 log.warn(error.message)
375 }
376 } else { // read through each directory in this node_modules folder
377 let relativePluginPaths = readdirSync(searchPath) // search for directories only
378 .filter((relativePath) => {
379 try {
380 return statSync(resolve(searchPath, relativePath)).isDirectory()
381 } catch (error: any) {
382 log.debug(`Ignoring path ${resolve(searchPath, relativePath)} - ${error.message}`)
383 return false
384 }
385 })
386
387 // expand out @scoped plugins
388 const scopeDirectories = relativePluginPaths.filter(path => path.startsWith('@'))
389 relativePluginPaths = relativePluginPaths.filter(path => !path.startsWith('@'))
390
391 for (const scopeDirectory of scopeDirectories) {
392 const absolutePath = join(searchPath, scopeDirectory)
393 readdirSync(absolutePath)
394 .filter(name => PluginManager.isQualifiedPluginIdentifier(name))
395 .filter((name) => {
396 try {
397 return statSync(resolve(absolutePath, name)).isDirectory()
398 } catch (error: any) {
399 log.debug(`Ignoring path ${resolve(absolutePath, name)} - ${error.message}`)
400 return false
401 }
402 })
403 .forEach(name => relativePluginPaths.push(`${scopeDirectory}/${name}`))
404 }
405
406 relativePluginPaths
407 .filter((pluginIdentifier) => {
408 return PluginManager.isQualifiedPluginIdentifier(pluginIdentifier) // needs to be a valid homebridge plugin name
409 && (!this.activePlugins || this.activePlugins.includes(pluginIdentifier)) // check if activePlugins is restricted and if so is the plugin is contained
410 })
411 .forEach((pluginIdentifier) => {
412 try {
413 const absolutePath = resolve(searchPath, pluginIdentifier)
414 this.loadPlugin(absolutePath)
415 } catch (error: any) {
416 log.warn(error.message)
417 }
418 })
419 }

Callers 1

Calls 5

loadDefaultPathsMethod · 0.95
loadPluginMethod · 0.95
warnMethod · 0.80
debugMethod · 0.80

Tested by

no test coverage detected