|
6 | 6 |
|
7 | 7 | const process = require('node:process') |
8 | 8 | const EventEmitter = require('node:events').EventEmitter |
| 9 | +const { readFileSync } = require('node:fs') |
9 | 10 | const path = require('node:path') |
10 | 11 |
|
11 | 12 | let pluginAlias |
@@ -36,6 +37,26 @@ const HomebridgeApiMock = { |
36 | 37 | serverVersion: '1.2.3', |
37 | 38 | on: () => { /** mock */ }, |
38 | 39 | emit: () => { /** mock */ }, |
| 40 | + // Mock Matter API |
| 41 | + isMatterAvailable() { |
| 42 | + return true |
| 43 | + }, |
| 44 | + isMatterEnabled() { |
| 45 | + return true |
| 46 | + }, |
| 47 | + matterDeviceTypes: new Proxy({}, { |
| 48 | + get() { |
| 49 | + return {} // Return empty object for any device type |
| 50 | + }, |
| 51 | + }), |
| 52 | + matterClusters: new Proxy({}, { |
| 53 | + get() { |
| 54 | + return {} // Return empty object for any cluster |
| 55 | + }, |
| 56 | + }), |
| 57 | + registerMatterAccessory: () => { /** mock */ }, |
| 58 | + unregisterMatterAccessory: () => { /** mock */ }, |
| 59 | + updateMatterAccessoryState: () => { /** mock */ }, |
39 | 60 | hap: { |
40 | 61 | Characteristic: new class Characteristic extends EventEmitter { |
41 | 62 | constructor() { |
@@ -99,11 +120,41 @@ const HomebridgeApiMock = { |
99 | 120 | }, |
100 | 121 | } |
101 | 122 |
|
102 | | -function main() { |
| 123 | +async function main() { |
103 | 124 | try { |
104 | 125 | let pluginInitializer |
105 | 126 | const pluginPath = process.env.UIX_EXTRACT_PLUGIN_PATH |
106 | | - const pluginModules = require(pluginPath) |
| 127 | + |
| 128 | + // Read package.json to get the proper entry point |
| 129 | + let actualEntryPoint = pluginPath |
| 130 | + try { |
| 131 | + const packageJsonPath = path.join(pluginPath, 'package.json') |
| 132 | + const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8')) |
| 133 | + |
| 134 | + if (packageJson.main) { |
| 135 | + actualEntryPoint = path.join(pluginPath, packageJson.main) |
| 136 | + } |
| 137 | + } catch (err) { |
| 138 | + console.error('[extract-plugin-alias] Could not read package.json, using directory path') |
| 139 | + } |
| 140 | + |
| 141 | + let pluginModules |
| 142 | + |
| 143 | + // Try to load as CommonJS first |
| 144 | + try { |
| 145 | + pluginModules = require(actualEntryPoint) |
| 146 | + } catch (requireError) { |
| 147 | + // If require fails, try dynamic import for ESM modules |
| 148 | + try { |
| 149 | + // For ESM, we need to use file:// URL on some platforms |
| 150 | + const importPath = actualEntryPoint.startsWith('/') || actualEntryPoint.startsWith('file://') |
| 151 | + ? actualEntryPoint |
| 152 | + : path.resolve(actualEntryPoint) |
| 153 | + pluginModules = await import(importPath) |
| 154 | + } catch (importError) { |
| 155 | + throw requireError // Throw the original error |
| 156 | + } |
| 157 | + } |
107 | 158 |
|
108 | 159 | if (typeof pluginModules === 'function') { |
109 | 160 | pluginInitializer = pluginModules |
|
0 commit comments