-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (51 loc) · 1.29 KB
/
index.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
const dashButton = require("node-dash-button");
const { readFileSync } = require("fs");
function malformedConfig() {
console.log("Configuration file was malformed");
process.exit(1);
}
let config;
const configFile = process.argv[2];
if (!configFile) {
console.log("Usage: yarn start <config-file>");
process.exit(1);
}
try {
config = JSON.parse(readFileSync(configFile));
} catch (e) {
malformedConfig();
}
if (!Array.isArray(config)) {
malformedConfig();
}
config.forEach(({ button, mac, integrations, options }) => {
button = button || mac;
if (!mac) {
console.log(
"You need to provide a MAC address for the button for it to work..."
);
return;
}
if (!integrations || !Array.isArray(integrations)) {
console.log(`No integrations found for ${button}... skipping`);
return;
}
const integrationFunctions = integrations
.map(integration => {
try {
return require(`./lib/${integration}`);
} catch (e) {
console.log(
`Could not load integration ${integration} for button ${
button
}... skipping.`
);
return;
}
})
.filter(Boolean);
const dash = dashButton(mac);
dash.on("detected", () => {
integrationFunctions.forEach(fn => fn(button, options));
});
});