-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.js
More file actions
111 lines (94 loc) · 3.3 KB
/
functions.js
File metadata and controls
111 lines (94 loc) · 3.3 KB
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
var fs = require('fs');
var wrench = require('wrench');
var path = require('path');
var async = require('async');
var replace = require("replaceall");
var val = require('validator');
module.exports = function (config) {
var module = {};
//Loop files
wrench.readdirSyncRecursive(config.path.functions).forEach(function (file){
file = path.join(config.path.functions,file);
//Check extension
if (!val.equals(path.extname(file),'.js')){
return;
}
//Construct
var call = require(file);
call.path = file;
//Construct name
call.name = replace(config.path.functions,'',file);
call.name = replace(path.extname(call.name),'',call.name);
call.name = replace('/','_',call.name);
if (call.name.charAt(0) == '_'){
call.name = call.name.substring(1);
}
//Blanks
if (!call.hasOwnProperty('brief')){ call.brief = 'No Brief'; }
if (!call.hasOwnProperty('description')){ call.description = 'No Description'; }
if (!call.hasOwnProperty('methods')){ call.methods = [] }
if (!call.hasOwnProperty('params')){ call.params = {} }
if (!call.hasOwnProperty('execute')){
call.execute = [];
call.execute[0] = function(response, params, info, next){ next('Could not find execution function', response, params, info); }
}
//Param Blanks
for (param in call.params){
if (!call.params[param].hasOwnProperty('description')){ call.params[param].description = 'No Description'; }
if (!call.params[param].hasOwnProperty('required')){ call.params[param].required = true; }
if (!call.params[param].hasOwnProperty('validator')){ call.params[param].validator = function(object){ return object.toString(); } }
if (!call.params[param].hasOwnProperty('description')){ call.params[param].description = 'No Description'; }
}
//Run Function
call.run = function(params, callback, add){
try{
//Check required & validation
var valid_params = {};
for (param in call.params){
//Required
if (!params.hasOwnProperty(param)){
if (call.params[param].required){
throw "Please enter a " + replace('_',' ',param);
}else{
valid_params[param] = null;
continue;
}
}
//Validate
valid_params[param] = call.params[param].validator(params[param], 'Please enter a valid ' + replace('_',' ',param));
}
//Construct info
var add = add || {};
var info = {};
info.temp = {};
//Populate info
info.functions = module;
if (add.hasOwnProperty('remoteAddress')){
info.address = add.remoteAddress;
info.tcp = add;
}else if (add.hasOwnProperty('address')){
info.address = add.address;
info.udp = add;
}else if (add.hasOwnProperty('connection')){
info.address = add.connection.remoteAddress;
info.express = add;
}else if (add != {}){
info.other = add;
}
//Execute
var execution = [function(next){ next(null, {message: 'Successful', output: {}}, valid_params, info); }]
async.waterfall(execution.concat(call.execute), function (error, response) {
if (error){
callback(false, error, response.output, response.download);
}else{
callback(true, response.message, response.output, response.download);
}
});
}catch(error){
callback(false, error, {});
}
}
module[call.name] = call;
});
return module;
};