-
Notifications
You must be signed in to change notification settings - Fork 388
/
Copy pathextract-plugin-alias.js
132 lines (122 loc) · 3.16 KB
/
extract-plugin-alias.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* eslint-disable unused-imports/no-unused-vars */
/**
* This script "mocks" homebridge and is used to extract the plugin alias and type.
*/
const process = require('node:process')
const EventEmitter = require('node:events').EventEmitter
const path = require('node:path')
let pluginAlias
let pluginType
const HomebridgeApiMock = {
registerPlatform(pluginIdentifier, platformName, constructor) {
pluginType = 'platform'
if (typeof platformName === 'function') {
constructor = platformName
platformName = pluginIdentifier
pluginAlias = platformName
} else {
pluginAlias = platformName
}
},
registerAccessory(pluginIdentifier, accessoryName, constructor) {
pluginType = 'accessory'
if (typeof accessoryName === 'function') {
constructor = accessoryName
accessoryName = pluginIdentifier
pluginAlias = accessoryName
} else {
pluginAlias = accessoryName
}
},
version: 2.5,
serverVersion: '1.2.3',
on: () => { /** mock */ },
emit: () => { /** mock */ },
hap: {
Characteristic: new class Characteristic extends EventEmitter {
constructor() {
super()
return new Proxy(this, {
get() {
return {
UUID: '0000003E-0000-1000-8000-0026BB765291',
}
},
})
}
}(),
Service: new class Service extends EventEmitter {
constructor() {
super()
return new Proxy(this, {
get() {
return {
UUID: '0000003E-0000-1000-8000-0026BB765291',
}
},
})
}
}(),
AccessoryLoader: {},
Accessory: {},
Bridge: {},
Categories: {},
Units: {},
uuid: {
generate: () => { /** mock */ },
},
},
platformAccessory() {
return {
addService() { /** mock */ },
getService() { /** mock */ },
removeService() { /** mock */ },
context() { /** mock */ },
services() { /** mock */ },
}
},
registerPlatformAccessories() { /** mock */ },
unregisterPlatformAccessories() { /** mock */ },
publishExternalAccessories() { /** mock */ },
updatePlatformAccessories() { /** mock */ },
user: {
configPath() {
return path.join(process.cwd(), 'config.json')
},
storagePath() {
return process.cwd()
},
cachedAccessoryPath() {
return path.join(process.cwd(), 'accessories')
},
persistPath() {
return path.join(process.cwd(), 'persist')
},
},
}
function main() {
try {
let pluginInitializer
const pluginPath = process.env.UIX_EXTRACT_PLUGIN_PATH
const pluginModules = require(pluginPath)
if (typeof pluginModules === 'function') {
pluginInitializer = pluginModules
} else if (pluginModules && typeof pluginModules.default === 'function') {
pluginInitializer = pluginModules.default
} else {
throw new Error(`Plugin ${pluginPath} does not export a initializer function from main.`)
}
pluginInitializer(HomebridgeApiMock)
process.send({
pluginAlias,
pluginType,
})
process.exit()
} catch (e) {
process.exit(1)
}
}
main()
setTimeout(() => {
process.exit(1)
}, 2500)