MCPcopy
hub / github.com/homebridge/homebridge / load

Method load

src/plugin.ts:179–227  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

177 }
178
179 public async load(): Promise<void> {
180 const context = this.loadContext!
181 assert(context, 'Reached illegal state. Plugin state is undefined!')
182 this.loadContext = undefined // free up memory
183
184 // pluck out the Homebridge version requirement
185 if (!context.engines || !context.engines.homebridge) {
186 throw new Error(`Plugin ${this.pluginPath} does not contain the 'homebridge' package in 'engines'.`)
187 }
188
189 const versionRequired = context.engines.homebridge
190 const nodeVersionRequired = context.engines.node
191
192 // make sure the version is satisfied by the currently running version of Homebridge
193 if (!satisfies(getVersion(), versionRequired, { includePrerelease: true })) {
194 // TODO - change this back to an error
195 log.warn(`The plugin "${this.pluginName}" requires a Homebridge version of ${versionRequired} which does \
196not satisfy the current Homebridge version of v${getVersion()}. You may need to update this plugin (or Homebridge) to a newer version. \
197You may face unexpected issues or stability problems running this plugin.`)
198 }
199
200 // make sure the version is satisfied by the currently running version of Node
201 if (nodeVersionRequired && !satisfies(process.version, nodeVersionRequired)) {
202 log.warn(`The plugin "${this.pluginName}" requires a Node.js version of ${nodeVersionRequired} which does \
203not satisfy the current Node.js version of ${process.version}. You may need to upgrade your installation of Node.js - see https://homebridge.io/w/JTKEF`)
204 }
205
206 const dependencies = context.dependencies || {}
207 if (dependencies.homebridge || dependencies['hap-nodejs'] || dependencies['@homebridge/hap-nodejs']) {
208 log.error(`The plugin "${this.pluginName}" defines 'homebridge' and/or 'hap-nodejs' in their 'dependencies' section, \
209meaning they carry an additional copy of homebridge and hap-nodejs. This not only wastes disk space, but also can cause \
210major incompatibility issues and thus is considered bad practice. Please inform the developer to update their plugin!`)
211 }
212
213 const mainPath = join(this.pluginPath, this.main)
214
215 // try to import it and grab the exported initialization hook
216 // pathToFileURL(specifier).href to turn a path into a "file url"
217 // see https://github.com/nodejs/node/issues/31710
218 const pluginModules = (await import(pathToFileURL(mainPath).href)).default
219
220 if (typeof pluginModules === 'function') {
221 this.pluginInitializer = pluginModules
222 } else if (pluginModules && typeof pluginModules.default === 'function') {
223 this.pluginInitializer = pluginModules.default
224 } else {
225 throw new Error(`Plugin ${this.pluginPath} does not export a initializer function from main.`)
226 }
227 }
228
229 public initialize(api: API): void | Promise<void> {
230 if (!this.pluginInitializer) {

Callers 4

loadPluginMethod · 0.45
main.jsFile · 0.45
SeFunction · 0.45

Calls 3

getVersionFunction · 0.85
warnMethod · 0.80
errorMethod · 0.80

Tested by

no test coverage detected