-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathnode_helper.js
157 lines (130 loc) · 3.76 KB
/
node_helper.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* Magic Mirror
* Module: Api
*
* By Julian Zimmermann
* MIT Licensed.
*/
const NodeHelper = require("node_helper");
const path = require("path");
const url = require("url");
const fs = require("fs");
const exec = require("child_process").exec;
const os = require("os");
const bodyParser = require("body-parser");
Module = {
configDefaults: {},
register: function (name, moduleDefinition) {
// console.log("Module config loaded: " + name);
Module.configDefaults[name] = moduleDefinition.defaults;
}
};
module.exports = NodeHelper.create({
// Subclass start method.
start: function() {
var self = this;
this.moduleData = {}
self.loadModules(
path.resolve(__dirname + "/../default"),
function(f) {
return f !== "node_modules"
&& f !== "README.md"
&& f !== "defaultmodules.js"
}
);
self.loadModules(
path.resolve(__dirname + "/.."),
function(f) {
return f !== "node_modules"
&& f !== "default"
&& f !== "README.md"
}
);
console.log("Starting node helper for: " + self.name);
this.expressApp.get("/api/v1/modules", (req, res) => {
res.send({'success': 'true', 'modules': this.moduleData});
});
this.expressApp.get("/api/v1/modules/:modulename", (req, res) => {
var moduleName = self.formatName(req.params.modulename)
console.log(moduleName)
if (this.moduleData[moduleName] == undefined) {
res.status(404).send({'success': "false", "error": "Module not found"})
return
}
res.send({'success': 'true', 'actions': this.moduleData[moduleName]});
});
this.expressApp.post("/api/notify/:action", (req, res) => {
var query = url.parse(req.url, true).query
this.sendSocketNotification(req.params.action.toUpperCase(), query);
res.send({'success': 'true'});
});
this.expressApp.post("/api/v1/modules/:modulename/:action", (req, res) => {
var moduleName = self.formatName(req.params.modulename)
if (this.moduleData[moduleName] == undefined) {
res.status(404).send({'success': "false", "error": "Module not found"})
return
}
var actionName =req.params.action.toUpperCase();
if (this.moduleData[moduleName].indexOf(actionName) < 0) {
res.status(404).send({'success': "false", "error": "Action not found"})
return
}
var query = url.parse(req.url, true).query
this.sendSocketNotification(actionName, query);
res.send({'success': 'true'});
});
},
loadModules: function(modulePath, filter) {
var self = this;
var files = fs.readdirSync(modulePath).filter(
filter
)
for (index in files) {
var file = files[index];
var installedModules = []
for (var i = 0; i < files.length; i++) {
var module = files[i];
installedModules.push(module);
}
for (index in installedModules) {
var moduleName = installedModules[index]
var file = fs.readFileSync(
path.resolve(modulePath + '/' + moduleName + '/' + moduleName + '.js'),
'utf8')
var moduleActions = self.getActions(file)
if (moduleActions.length > 0) {
self.moduleData[moduleName] = moduleActions
}
}
}
},
getActions: (content) => {
var self = this;
var re = /notification \=\=\=? "([A-Z_]+)"|case '([A-Z_]+)'/g;
var m;
var availabeActions = [];
do {
if (m = re.exec(content)) {
m = m.filter(function(rm) {
return rm != undefined
})
if (m.indexOf('DOM_OBJECTS_CREATED') >= 0) {
continue;
}
availabeActions.push(m[1]);
}
} while (m);
return availabeActions;
},
formatName: function(string) {
var parts = string.split('-');
if (parts.length == 1) {
return string
}
if (parts[0].toLowerCase() == 'mmm') {
parts[0] = parts[0].toUpperCase();
}
return parts.map(function(p) {
return p.charAt(0).toUpperCase() + p.substr(1);
}).join('-')
},
});