forked from LinkedDataFragments/Server.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule.js
29 lines (26 loc) · 992 Bytes
/
module.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
/*! @license MIT ©2015-2016 Ruben Verborgh, Ghent University - imec */
/* Exports of the ldf-server package for use as a submodule (as opposed to standalone) */
var fs = require('fs'),
path = require('path');
// Export all modules in the `lib` folder
module.exports = readModules(path.join(__dirname, 'lib'));
// Reads modules in the given folder and subfolders
function readModules(folder) {
var modules = {};
fs.readdirSync(folder).forEach(function (name) {
var location = path.join(folder, name), item = fs.statSync(location);
// Add script files by including them
if (item.isFile()) {
var scriptMatch = name.match(/(\w+)\.js$/);
if (scriptMatch) {
try { modules[scriptMatch[1]] = require(location); }
catch (error) { /* ignore modules that cannot be instantiated */ }
}
}
// Add folders by recursing over them
else if (item.isDirectory()) {
modules[name] = readModules(location);
}
});
return modules;
}