forked from homebridge/homebridge-config-ui-x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract-plugin-alias.js
119 lines (108 loc) · 2.88 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
/* eslint-disable @typescript-eslint/no-var-requires */
/**
* This script "mocks" homebridge and is used to extract the plugin alias and type.
*/
const path = require('path');
const EventEmitter = require('events').EventEmitter;
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: {},
AccessoryLoader: {},
Accessory: {},
Bridge: {},
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: pluginAlias,
pluginType: pluginType,
});
process.exit();
} catch (e) {
process.exit(1);
}
}
main();
setTimeout(() => {
process.exit(1);
}, 2500);