forked from webgme/webgme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webgme.js
149 lines (129 loc) · 4.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
/*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');
global.requireJS = requirejs;
requirejs.config({
nodeRequire: require,
baseUrl: requireJsBase,
paths: {
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);
}
module.exports = {
standaloneServer: function (gmeConfig) {
var Standalone = require('./src/server/standalone.js');
return new Standalone(gmeConfig);
},
requirejs: requirejs,
addToRequireJsPaths: addToRequireJsPaths,
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);
},
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);
},
core: requirejs('common/core/core'),
serializer: requirejs('common/core/users/serialization'),
canon: requirejs('common/util/canon'),
Logger: require('./src/server/logger'),
REGEXP: requirejs('common/regexp'),
PluginCliManager: require('./src/plugin/climanager')
};