-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
219 lines (152 loc) · 5.55 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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
var path = require('path');
var globby = require('globby');
var caller = require('caller');
var annotationParser = require('annotation-parser');
module.exports = function(pattern, eachCallback, finalizeCallback){
pattern = path.resolve(path.dirname(caller()), pattern);
globby(pattern, function(err, files){
var fileCompleted = 0;
// loop through the files
for(var i = 0, len = files.length; i < len; i++){
(function(fileIndex){
annotationParser(files[fileIndex], function(err, annotations){
var routes = extractRoutes(annotations);
var controller = pathParse(files[fileIndex]);
controller.annotations = extractControllerAnnotations(annotations.module.annotations);
controller.rawAnnotations = extractControllerAnnotations(annotations.module.rawAnnotations);
setControllerOnRoutes(controller, routes);
for(var i = 0, len = routes.length; i < len; i++){
eachCallback(null, routes[i]);
}
if(++fileCompleted === files.length){
finalizeCallback(null);
}
});
})(i);
}
if(files.length === 0){
finalizeCallback(null);
}
});
};
module.exports.sync = function(pattern){
pattern = path.resolve(path.dirname(caller()), pattern);
var files = globby.sync(pattern);
var routes = [];
for(var i = 0, len = files.length; i < len; i++){
var annotations = annotationParser.sync(files[i]);
var fileRoutes = extractRoutes(annotations);
var controller = pathParse(files[i]);
controller.annotations = extractControllerAnnotations(annotations.module.annotations);
controller.rawAnnotations = extractControllerAnnotations(annotations.module.rawAnnotations);
setControllerOnRoutes(controller, fileRoutes);
routes = routes.concat(fileRoutes);
}
return routes;
};
function extractRoutes(annotations){
var routes = [];
// loop through the functions
for(var functionName in annotations.functions){
var urls = getUrls(annotations.module.annotations, annotations.functions[functionName].annotations);
if(urls.length === 0){
continue;
}
var method = getMethod(annotations.functions[functionName].annotations, functionName);
var actionAnnotations = extractActionAnnotations(annotations.functions[functionName].annotations);
var actionRawAnnotations = extractActionAnnotations(annotations.functions[functionName].rawAnnotations);
// loop through the urls (one function can have multiple route)
for(var i in urls){
routes.push({
url: urls[i],
method: method,
action: annotations.functions[functionName].ref,
actionName: functionName,
annotations: actionAnnotations,
rawAnnotations: actionRawAnnotations,
});
}
}
return routes;
}
function setControllerOnRoutes(controller, routes){
for(var i = 0, len = routes.length; i < len; i++){
routes[i].controller = controller;
}
return routes;
}
function getMethod(functionAnnotations, functionName){
// search for httpGet/Post/... annotations
for(var annotationName in functionAnnotations){
if(annotationName.indexOf('http') === 0){
var method = annotationName.slice(4);
if(method.toLowerCase() in methodEnum){
return method.toUpperCase();
}
}
}
// use the function name to determinate the method
// TODO: extand to more word (ie: create/edit/...)
for(var method in methodEnum){
if(functionName.toLowerCase().indexOf(method.toLowerCase()) === 0){
return method.toUpperCase();
}
}
throw new Error('could not determind the correct method for ' + functionName);
}
function getUrls(moduleAnnotations, functionAnnotations){
var routePrefixs = [];
// set the route prefix and remove the '/' at its end
if(moduleAnnotations.routePrefix){
for(var i in moduleAnnotations.routePrefix){
routePrefixs.push(moduleAnnotations.routePrefix[i][0]);
}
}
if(routePrefixs.length === 0){
routePrefixs.push('');
}
var urls = [];
for(var i in routePrefixs){
for(var j in functionAnnotations.route){
var url = functionAnnotations.route[j][0];
urls.push(routePrefixs[i] + url);
}
}
return urls;
}
var methodEnum = {
get: 0,
post: 1,
put: 2,
delete: 3,
head: 4,
};
// mimics the path.parse function
function pathParse(fullPath){
var parsedPath = {
ext: path.extname(fullPath),
dir: path.dirname(fullPath),
full: fullPath,
base: path.basename(fullPath),
};
parsedPath.name = path.basename(fullPath, parsedPath.ext);
return parsedPath;
}
function copyObject(object, except){
if(!except){
except = [];
}
var copy = {};
for(var i in object){
if(except.indexOf(i) === -1){
copy[i] = object[i];
}
}
return copy;
}
function extractControllerAnnotations(annotations){
return copyObject(annotations, ['routePrefix']);
}
function extractActionAnnotations(annotations){
return copyObject(annotations, ['route', 'httpGet', 'httpPost', 'httpPut', 'httpDelete', 'httpHead']);
}