forked from webgme/webgme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebgme.js
184 lines (159 loc) · 5.88 KB
/
webgme.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
/*
* Copyright (C) 2013-2014 Vanderbilt University, All rights reserved.
*
* Author: Tamas Kecskes
*/
//This is the only module which doesn't checks for requirejs, and this is the only which defines the baseUrl!!!
var PATH = require('path'),
FS = require('fs'),
requirejs = require('requirejs'),
baseDir = __dirname,
paths = {
"logManager": "common/LogManager",
"storage": "storage",
"core": "core",
"server": "server",
"auth": "auth",
"util": "util",
"baseConfig" : "bin/getconfig",
"webgme": "webgme",
"plugin": "plugin"
};
//All other modules should only configure new path in respect with this base URL!!!
requirejs.config({
nodeRequire: require,
baseUrl: baseDir,
paths:paths
});
var __CONFIG = requirejs('baseConfig');
var getConfig = function(){
return JSON.parse(JSON.stringify(__CONFIG));
};
var setConfig = function(configObject){
var keys, i,j;
var isGoodExtraAsset = function(name,filePath){
try{
var file = FS.readFileSync(filePath+'/'+name+'.js','utf-8');
if(file === undefined || file === null){
return false;
} else {
return true;
}
} catch(e){
return false;
}
};
var getPluginNames = function(basePaths){
var names = []; //we add only the "*.js" files from the directories
basePaths = basePaths || [];
for(var i=0;i<basePaths.length;i++){
var additional = FS.readdirSync(basePaths[i]);
for(var j=0;j<additional.length;j++){
if(names.indexOf(additional[j]) === -1){
if(isGoodExtraAsset(additional[j],PATH.join(basePaths[i],additional[j]))){
names.push(additional[j]);
}
}
}
}
return names;
};
var addPluginPathsToRequirejs = function(basepaths){
var requirejsBase = webGMEGlobal.baseDir,
pluginNames = getPluginNames(basepaths),
i,j;
//we go through every plugin and we check where we are able to find the main part of it so we can set the plugin/pluginName path according that in requirejs
var pluginPaths = {};
for(i in pluginNames) {
var found = false;
for (j = 0; j < basepaths.length; j++) {
if (!found) {
try {
var items = FS.readdirSync(basepaths[j]);
if(items.indexOf(pluginNames[i]) !== -1){
pluginPaths['plugin/' + pluginNames[i]] = PATH.relative(requirejsBase,PATH.resolve(basepaths[j]));
found = true;
}
} catch (e) {
//do nothing as we will go on anyway
//console.error(e);
}
} else {
break;
}
}
}
requirejs.config({
paths: pluginPaths
});
};
var addToRequireJSPath = function (requireJSPaths) {
if (!requireJSPaths) {
return;
}
var requirejsBase = webGMEGlobal.baseDir,
configPaths = {};
var keys = Object.keys(requireJSPaths);
for (var i = 0; i < keys.length; i += 1) {
configPaths[keys[i]] = PATH.relative(requirejsBase,PATH.resolve(requireJSPaths[keys[i]]));
}
requirejs.config({
paths: configPaths
});
};
//setting simple values
keys = Object.keys(configObject);
for(i=0;i<keys.length;i++){
if (typeof configObject[keys[i]] === "number" ||
typeof configObject[keys[i]] === "boolean" ||
typeof configObject[keys[i]] === "string") {
__CONFIG[keys[i]] = configObject[keys[i]];
}
}
//adding the paths
if(configObject.paths){
for(i in configObject.paths){
__CONFIG.paths[i] = configObject.paths[i];
}
}
//merge plugin base paths
if(configObject.pluginBasePaths && configObject.pluginBasePaths.length){
__CONFIG.pluginBasePaths = configObject.pluginBasePaths.concat(__CONFIG.pluginBasePaths);
}
if (__CONFIG.pluginBasePaths) {
addPluginPathsToRequirejs(__CONFIG.pluginBasePaths);
}
//merge decorator base paths
if(configObject.decoratorpaths && configObject.decoratorpaths.length){
__CONFIG.decoratorpaths = configObject.decoratorpaths.concat(__CONFIG.decoratorpaths);
}
//merge visualizer descriptor paths
if(configObject.visualizerDescriptors && configObject.visualizerDescriptors.length){
__CONFIG.visualizerDescriptors = __CONFIG.visualizerDescriptors.concat(configObject.visualizerDescriptors);
}
if (__CONFIG.paths) {
addToRequireJSPath(__CONFIG.paths);
}
};
//creating a global variable
webGMEGlobal = {
baseDir : PATH.resolve(baseDir),
getConfig : getConfig,
setConfig : setConfig
};
//setting the default array elements
//TODO this should be done already in getconfig !!!
webGMEGlobal.setConfig({
decoratorpaths : [PATH.join(PATH.join(baseDir,'/client'),"/decorators")],
pluginBasePaths : [PATH.join(baseDir,"/coreplugins")],
visualizerDescriptors : [PATH.join(baseDir,"/client/js/Visualizers.json")]
});
module.exports = {
clientStorage: requirejs('storage/clientstorage'),
serverStorage: requirejs('storage/serverstorage'),
serverUserStorage: requirejs('storage/serveruserstorage'),
core: requirejs('core/core'),
standaloneServer: requirejs('server/standalone'),
logManager: requirejs('logManager'),
runPlugin: requirejs('server/runplugin')
};