-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
177 lines (151 loc) · 4.33 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
var _require = require;
var fs = _require('fs');
var path = require('path');
var global = {};
var Module = function(id, parent){
this.id = id;
this.parent = parent;
this._require = _require;
this.filename = undefined;
this.__dir = undefined;
this.exports = {} // otherwise the exports is undefined ...
}
Module.cache = {};
Module.prototype._resolvePath = function(moduleName){
var parent = this.parent;
//var modulePath = moduleName.split("/");
if (moduleName.indexOf('cloud') === 0) {
moduleName = moduleName.replace('cloud/', '');
}
var currentPath = ''
if (moduleName[0] == ".") {
if (parent && parent.filename) {
currentPath = path.dirname(parent.filename);
//var start = currentPath.split("/");
//start = start.slice(0, start.length-1);
//modulePath = modulePath.slice(1);
//moduleName = path.join(currentPath, moduleName);//start.concat(modulePath).join("/");
}else{
//modulePath[0] = "cloud";
//moduleName = path.join(currentPath, moduleName);//modulePath.join("/");
}
}
var resolvedName = path.join(currentPath, moduleName);
return resolvedName;
}
Module.prototype._getRealPath = function(moduleName){
var parent = this.parent;
var isJS = this.checkJSFile(moduleName);
if (isJS) {
return isJS;
};
var isModule = this.checkModuleFolder(moduleName);
if (isModule) {
return isModule;
};
var isParseModule = this.checkParseModule(moduleName);
if (isParseModule) {
return isParseModule;
}
return moduleName;
}
Module.prototype.load = function(){
var resolvedPath = this._resolvePath(this.id);
if (resolvedPath.split(".")[1] === "json") {
this.exports = JSON.parse(fs.readFileSync(path.join('cloud', resolvedPath)));
}else{
this.filename = this._getRealPath(resolvedPath);
if (Module.cache[this.filename]) {
return Module.cache[this.filename];
};
/*if (!this.filename || this.filename.indexOf('cloud') !== 0) {
throw 'Module not found '+this.id;
};*/
var self = this;
var f = fs.readFileSync(path.join('cloud', this.filename));
module.children = module.children || [];
module.children.push(self);
//self.parent = module;
var wrap = '(function(Parse, module, require, exports, console, global, _require){\n'+f+'\n;})(Parse, self, reqFunc(self), self.exports, console, global, self._require);';
eval(wrap);
Module.cache[this.filename] = self;
}
return this;
}
Module.prototype.checkJSFile = function(moduleName){
var parts = moduleName.split(".");
if (parts.length == 1) {
moduleName += ".js"
};
if(fs.existsSync(path.join('cloud', moduleName))){
return moduleName;
}
return;
}
Module.prototype.checkModuleFolder = function(moduleName){
var exist = false;
var parts = moduleName.split(".");
var resolvedName = moduleName;
var resolvedNameJSON = moduleName;
if (parts.length == 1) {
var lastChar = resolvedName[resolvedName.length-1];
if (lastChar !== "/") {
resolvedName+="/";
}
resolvedNameJSON = resolvedName + "package.json";
resolvedName += "index.js"
}
if(fs.existsSync(path.join('cloud', resolvedName))){
return resolvedName;
}
if (fs.existsSync(path.join('cloud', resolvedNameJSON))) {
var tpath = path.join('cloud', resolvedNameJSON);
var jsonFile = fs.readFileSync(tpath);
var json = JSON.parse(jsonFile);
if (json.main) {
var fullpath = path.join(moduleName, json.main);
return fullpath;
}
}
resolvedName = moduleName;
return;
}
Module.prototype.checkParseModule = function(moduleName){
var parent = this.parent;
var parentPath = ['cloud'];
if (parent && parent.filename) {
parentPath = path.dirname(parent.filename).split("/");
}
var resolvedName;
do{
var mpath = path.join(parentPath,'parse_modules', moduleName);
parentPath = mpath.split("/");
resolvedName = this.checkModuleFolder(mpath);
parentPath = parentPath.slice(0, parentPath.length-1);
}while(parentPath.length > 1 && !resolvedName);
return resolvedName;
}
var reqFunc = function(parent){
return function(moduleName){
return require2(moduleName, parent);
}
}
var require2 = function(moduleName, parent){
// Load modules only at runtime!
if (!Parse.applicationId || !Parse.masterKey) {
return;
};
try{
if (moduleName.indexOf("/") < 0) {
// Native Modules...
var m = _require(moduleName);
return m;
};
}catch(e){
console.error(e);
}
//return;
var m = new Module(moduleName, parent);
return m.load().exports;
}
module.exports = require2;