-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
190 lines (159 loc) · 4.49 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
"use strict";
var rc = function rc () {
var that = this;
that.status = {dirname: __dirname, filename: __filename, };
that.verbose = arguments[0].verbose || false;
that.config = require('./config.json');
that.config.package = require('./package.json');
that.node = {
events: require('events'),
fs: require('fs'),
path: require('path'),
os: require('os'),
};
that.npm = {
//requirejs: require('requirejs'),
//eventemitter2: require('eventemitter2').EventEmitter2,
};
//that.npm.requirejs.config({baseUrl: __dirname , nodeRequire: require});
//that.shared_utility = require('./html/js/shared_utility.js');
var log = that.log = require('./static/log.js');
//that.log("that.log"); //The log function can be used now.
that.api = {}; // important: Never call any api in this function rc () {}.
// done: injecting api from ./api/*.js
// todo: recursively function
that.node.fs.readdir(that.node.path.resolve(__dirname, 'api'), function(){
if (arguments[0]) {
throw arguments[0];
}
for (var i in arguments[1]) {
var filename = arguments[1][i];
if (!filename.match(/\.js$/i)) {
log("invalid filename: " + filename + ' ');
continue;
}
var api_file = require(that.node.path.resolve(__dirname, 'api', filename));
if (typeof(api_file) === 'function') {
var api_name = filename.replace(/\.js$/i,'');
if (typeof(that.api[api_name]) != 'undefined') {
log("ignoring duplicated api_name: " + api_name);
continue;
}
log("injecting api: " + api_name + ", from: " + filename);
that.api[api_name] = api_file;
} else if (typeof(api_file) === 'object') {
for (var j in api_file) {
if (typeof(that.api[j]) != 'undefined') {
log("duplicated api_name: " + api_name);
log("ignoring duplicated api_name: " + api_name);
}
log("injecting api: " + j + ", from: " + filename);
that.api[j] = api_file[j];
}
} else {
log('ERROR: please debug');
process.exit(99);
}
}
});
// to require "classes" and then new them
/*
that.module = {};
that.instance = {};
var module_require = function module_require () {
console.log("module_require: ");
var module_name = that.node.path.parse(arguments[0]);
that.module[module_name.name] = require(arguments[0]);
if(typeof(that.module[module_name.name]) === 'function'){
console.log("module_new: ");
that.instance[module_name.name] = new that.module[module_name.name];
}
}
var module_list = function module_list () {
//console.log("In module_list");
//console.log(arguments[0]);
var dir = arguments[0];
that.node.fs.readdir(arguments[0], function () {
if(arguments[0]) {
console.log("cannot load module list");
process.exit(99);
}
//console.log(arguments[1]);
for (var i in arguments[1]) {
if(arguments[1][i] && arguments[1][i].match(/\.js$/i)) {
module_require(that.node.path.resolve( dir, arguments[1][i]));
}
}
});
}
module_list(that.node.path.resolve(__dirname, './object/'));
*/
}
rc.prototype.list_api = function () {
var api_list = [];
for (var i in this.api) api_list.push(i);
return api_list;
}
rc.prototype.call_api = function (api_call) {
var that = this;
if (typeof(api_call) === 'undefined') {
that.log("ERROR: undefined API call");
return;
}
var api_name = api_call.api_name;
var api_args = api_call.api_args;
if (typeof(api_name) != 'string') {
that.log("ERROR: The given api_name is not a string.");
return;
}
if (typeof(that.api[api_name]) === 'function') {
var error_stack = (new Error().stack);
var es_array = error_stack.split('\n');
var caller = (es_array[2].match(/\/.*\.[jJ][sS]:[0-9]*:[0-9]*/)[0]);
that.log('' + caller + " is calling api: " + api_name + " with args: " + JSON.stringify(api_args));
process.nextTick(function(){
that.api[api_name]({_rc: that, args: api_args});
});
} else if (typeof(that.api[api_name]) === 'undefined') {
that.log("WARN: undefined api: " + api_name);
} else {
that.log("invalid api: " + api_name);
that.log("ERROR: please debug" );
process.nextTick(function(){
process.exit(99);
});
}
}
rc.prototype.on = function () {
switch (arguments[0]) {
case 'disposed':
break;
default:
break;
}
}
rc.prototype.get = function (args) {
var that = this;
switch (args) {
case 'status':
that.log(this.module);
break;
case '':
break;
default:
break;
}
}
rc.prototype.set = function (args) {
var that = this;
switch (args) {
case 'status':
that.log(this.module);
break;
case '':
break;
default:
break;
}
}
module.exports = rc;