-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
97 lines (87 loc) · 2.85 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
var fs = require('fs');
var path = require('path');
var mkdirp = require('mkdirp');
var val = require('validator');
var replace = require("replaceall");
//Generate
exports.generate = function(dingle, directory){
directory = path.join(process.cwd(), directory);
//Hostname
var hostnames = '';
if (dingle.config.https.listen != '' && dingle.config.https.hostname != ''){
hostnames += 'exports.https = "' + dingle.config.https.hostname + ':' + dingle.config.https.port + '";\n';
}
if (dingle.config.http.listen != '' && dingle.config.http.hostname != '' ){
hostnames += 'exports.http = "' + dingle.config.http.hostname + ':' + dingle.config.http.port + '";\n';
}
if (dingle.config.tcp.listen != '' && dingle.config.tcp.hostname != '' ){
hostnames += 'exports.tcp = "' + dingle.config.tcp.hostname + ':' + dingle.config.tcp.port + '";\n';
}
if (dingle.config.udp.listen != '' && dingle.config.udp.hostname != '' ){
hostnames += 'exports.udp = "' + dingle.config.udp.hostname + ':' + dingle.config.udp.port + '";\n';
}
//Generate
var functions = '';
for (func in dingle.functions){
functions += generate(dingle.functions[func]) + '';
}
//Make
var filename = path.join(directory, dingle.config.app.prefix + '.js');
var contents = fs.readFileSync(path.join(__dirname + '/request.js')).toString();
contents = replace("<hostnames>", hostnames, contents);
contents = replace("<functions>", functions, contents);
//Write file
mkdirp(path.dirname(filename), function (error) {
if (error){
throw error;
}else{
fs.writeFile(filename, contents, function(error){
if (error){
throw error;
}else{
return;
}
});
}
});
}
//Each Call
function generate(func){
//Parameters
var param_keys = [];
for(var key in func.params) { param_keys.push(key); }
//Parameters
var method_values = [];
for(var key in func.methods) { method_values.push(func.methods[key]); }
//Join
if (param_keys.length != 0){
param_keys = param_keys.join(', ') + ',';
}else{
param_keys = '';
}
//Generate
var str = '//Request\n';
str += 'exports.' + func.name + ' = function(' + param_keys + ' callback, methods, uploading, stream, downloading){\n';
str += '\n';
str += ' //Parameters\n';
str += ' var params = {};\n';
for (param in func.params){
str += ' if (' + param + ' != null){ \n';
str += ' if (typeof file == "object"){\n';
str += ' params["' + param + '"] = ' + param + ';\n';
str += ' }else{\n';
str += ' params["' + param + '"] = ' + param + '.toString();\n';
str += ' }\n';
str += ' }\n';
}
str += ' \n';
str += ' //Methods\n';
str += ' if (methods == null){\n';
str += ' methods = [ "' + method_values.join('", "') + '" ]\n';
str += ' }\n';
str += ' \n';
str += ' //Execute\n';
str += ' exports.sendRequest("' + func.name + '", methods, params, callback, uploading, stream, downloading);\n';
str += '}\n';
return str;
}