forked from webgme/webgme
/
webgme.js
202 lines (177 loc) · 5.94 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*jshint node:true*/
/**
* @author kecso / https://github.com/kecso
* @author nabana / https://github.com/nabana
*/
'use strict';
//This is is the only which defines the baseUrl for requirejs and adds it to the global.requireJS
var requirejs = require('requirejs'),
path = require('path'),
requireJsBase = path.join(__dirname, 'src'),
fs = require('fs'),
webgmeUtils = require('./src/utils'),
_core,
_canon,
_Logger,
_REGEXP,
_PluginCliManager,
exports = {
requirejs: requirejs
};
global.requireJS = requirejs;
requirejs.config({
nodeRequire: require,
baseUrl: requireJsBase,
paths: {
text: 'client/lib/require/require-text/text',
blob: 'common/blob',
executor: 'common/executor'
}
});
function addToRequireJsPaths(gmeConfig) {
function addAssetFromBasePath(basepaths, componentName) {
var componentNames = webgmeUtils.getComponentNames(basepaths);
return addFromBasePath(basepaths, componentName, componentNames);
}
function addDirsFromBasePath(basepaths, componentName) {
var componentNames = [],
files,
filePath;
for (var i = basepaths.length; i--;) {
files = fs.readdirSync(basepaths[i]);
for (var j = files.length; j--;) {
filePath = path.join(basepaths[i], files[j]);
if (fs.statSync(filePath).isDirectory()) {
componentNames.push(files[j]);
}
}
}
return addFromBasePath(basepaths, componentName, componentNames);
}
function addFromBasePath(basepaths, componentName, componentNames) {
//type = 'plugin'
var componentPaths,
componentPath,
found,
items,
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.
componentPaths = {};
for (i in componentNames) {
found = false;
for (j = 0; j < basepaths.length; j++) {
if (found) {
break;
}
try {
items = fs.readdirSync(basepaths[j]);
if (items.indexOf(componentNames[i]) !== -1) {
componentPath = path.relative(requireJsBase, path.resolve(basepaths[j]));
componentPaths[componentName + '/' + componentNames[i]] = componentPath;
found = true;
}
} catch (e) {
//do nothing as we will go on anyway
//console.error(e);
}
}
}
requirejs.config({
paths: componentPaths
});
}
function addFromRequireJsPath(requireJsPaths) {
var configPaths = {},
keys = Object.keys(requireJsPaths),
i;
for (i = 0; i < keys.length; i += 1) {
configPaths[keys[i]] = path.relative(requireJsBase, path.resolve(requireJsPaths[keys[i]]));
}
requirejs.config({
paths: configPaths
});
}
addAssetFromBasePath(gmeConfig.plugin.basePaths, 'plugin');
addAssetFromBasePath(gmeConfig.addOn.basePaths, 'addon');
addAssetFromBasePath(gmeConfig.visualization.layout.basePaths, 'layout');
// Panels are structured a little differently and may not have
// PANEL_NAME/PANEL_NAME.js like plugins, addons or layouts
addDirsFromBasePath(gmeConfig.visualization.panelPaths, 'panel');
addFromRequireJsPath(gmeConfig.requirejsPaths);
}
Object.defineProperties(exports, {
core: {
get: function () {
if (!_core) {
_core = requirejs('common/core/core');
}
return _core;
}
},
canon: {
get: function () {
if (!_canon) {
_canon = requirejs('common/util/canon');
}
return _canon;
}
},
Logger: {
get: function () {
if (!_Logger) {
_Logger = require('./src/server/logger');
}
return _Logger;
}
},
REGEXP: {
get: function () {
if (!_REGEXP) {
_REGEXP = requirejs('common/regexp');
}
return _REGEXP;
}
},
PluginCliManager: {
get: function () {
if (!_PluginCliManager) {
_PluginCliManager = require('./src/plugin/climanager');
}
return _PluginCliManager;
}
}
});
exports.addToRequireJsPaths = addToRequireJsPaths;
exports.standaloneServer = function (gmeConfig) {
var Standalone = require('./src/server/standalone.js');
return new Standalone(gmeConfig);
};
exports.getStorage = function (logger, gmeConfig, gmeAuth) {
var Mongo = require('./src/server/storage/mongo'),
SafeStorage = require('./src/server/storage/safestorage'),
mongo = new Mongo(logger, gmeConfig);
return new SafeStorage(mongo, logger, gmeConfig, gmeAuth);
};
exports.getGmeAuth = function (gmeConfig, callback) {
var Q = require('q'),
GMEAuth = require('./src/server/middleware/auth/gmeauth'),
deferred = Q.defer(),
gmeAuth;
gmeAuth = new GMEAuth(null, gmeConfig);
gmeAuth.connect(function (err) {
if (err) {
deferred.reject(err);
} else {
deferred.resolve(gmeAuth);
}
});
return deferred.promise.nodeify(callback);
};
exports.getGmeConfig = function () {
var gmeConfig = require(path.join(process.cwd(), 'config'));
addToRequireJsPaths(gmeConfig);
return gmeConfig;
};
module.exports = exports;