2014-10-30 06:25:35 +08:00
|
|
|
"use strict";
|
|
|
|
|
2015-03-12 03:31:45 +08:00
|
|
|
var Logger = require("./logger"),
|
|
|
|
_ = require("./utils/helpers");
|
2014-11-06 06:24:07 +08:00
|
|
|
|
2014-11-06 07:24:50 +08:00
|
|
|
// Explicitly these modules here, so Browserify can grab them later
|
2014-12-16 03:15:29 +08:00
|
|
|
require("./test/loopback");
|
|
|
|
require("./test/test-adaptor");
|
|
|
|
require("./test/test-driver");
|
|
|
|
require("./test/ping");
|
2014-11-06 07:24:50 +08:00
|
|
|
|
2014-10-31 04:15:40 +08:00
|
|
|
var missingModuleError = function(module) {
|
2014-12-16 03:15:29 +08:00
|
|
|
var str = "Cannot find the '" + module + "' module.\n";
|
|
|
|
str += "This problem might be fixed by installing it with ";
|
2015-04-15 12:49:12 +08:00
|
|
|
str += "'npm install " + module + "' and trying again.";
|
2014-10-30 06:25:35 +08:00
|
|
|
|
2014-12-16 03:15:29 +08:00
|
|
|
console.log(str);
|
2014-10-30 06:25:35 +08:00
|
|
|
|
2014-12-16 03:15:29 +08:00
|
|
|
process.emit("SIGINT");
|
2014-10-30 06:25:35 +08:00
|
|
|
};
|
|
|
|
|
2014-11-01 05:14:02 +08:00
|
|
|
var Registry = module.exports = {
|
2014-10-31 04:15:40 +08:00
|
|
|
data: {},
|
|
|
|
|
2014-10-30 06:25:35 +08:00
|
|
|
register: function(module) {
|
2014-10-31 04:15:40 +08:00
|
|
|
if (this.data[module]) {
|
2015-02-21 03:25:31 +08:00
|
|
|
return this.data[module].module;
|
2014-10-31 04:15:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var pkg;
|
|
|
|
|
|
|
|
try {
|
|
|
|
pkg = require(module);
|
|
|
|
} catch (e) {
|
|
|
|
if (e.code === "MODULE_NOT_FOUND") {
|
|
|
|
missingModuleError(module);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw e;
|
|
|
|
}
|
2014-10-30 06:25:35 +08:00
|
|
|
|
2014-10-31 04:15:40 +08:00
|
|
|
this.data[module] = {
|
2014-10-30 06:25:35 +08:00
|
|
|
module: pkg,
|
2014-10-31 04:15:40 +08:00
|
|
|
adaptors: pkg.adaptors || [],
|
2014-11-06 06:24:07 +08:00
|
|
|
drivers: pkg.drivers || [],
|
|
|
|
dependencies: pkg.dependencies || []
|
2014-10-30 06:25:35 +08:00
|
|
|
};
|
2014-10-31 04:15:40 +08:00
|
|
|
|
2014-11-06 06:24:07 +08:00
|
|
|
this.logRegistration(module, this.data[module]);
|
|
|
|
|
|
|
|
this.data[module].dependencies.forEach(function(dep) {
|
|
|
|
Registry.register(dep);
|
|
|
|
});
|
2014-11-06 02:04:45 +08:00
|
|
|
|
|
|
|
return this.data[module].module;
|
2014-10-30 06:25:35 +08:00
|
|
|
},
|
|
|
|
|
2015-01-23 06:40:09 +08:00
|
|
|
findBy: function(prop, name) {
|
|
|
|
// pluralize, if necessary
|
2015-03-10 12:26:45 +08:00
|
|
|
if (prop.slice(-1) !== "s") {
|
2015-01-23 06:40:09 +08:00
|
|
|
prop += "s";
|
|
|
|
}
|
2014-10-30 06:25:35 +08:00
|
|
|
|
2015-01-23 06:40:09 +08:00
|
|
|
return this.search(prop, name);
|
2014-10-30 06:25:35 +08:00
|
|
|
},
|
|
|
|
|
2014-11-06 02:04:45 +08:00
|
|
|
findByModule: function(module) {
|
|
|
|
if (!this.data[module]) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.data[module].module;
|
|
|
|
},
|
|
|
|
|
2014-11-06 06:24:07 +08:00
|
|
|
logRegistration: function(name) {
|
|
|
|
var module = this.data[name];
|
|
|
|
|
|
|
|
Logger.debug("Registering module " + name);
|
|
|
|
|
2014-12-16 03:15:29 +08:00
|
|
|
["adaptors", "drivers", "dependencies"].forEach(function(field) {
|
2014-11-06 06:24:07 +08:00
|
|
|
if (module[field].length) {
|
|
|
|
Logger.debug(" " + field + ":");
|
|
|
|
module[field].forEach(function(item) {
|
|
|
|
Logger.debug(" - " + item);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-10-31 04:15:40 +08:00
|
|
|
search: function(entry, value) {
|
|
|
|
for (var name in this.data) {
|
2015-04-15 12:49:12 +08:00
|
|
|
if (this.data.hasOwnProperty(name)) {
|
|
|
|
var repo = this.data[name];
|
2014-10-31 04:15:40 +08:00
|
|
|
|
2015-04-15 12:49:12 +08:00
|
|
|
if (repo[entry] && _.includes(repo[entry], value)) {
|
|
|
|
return repo.module;
|
|
|
|
}
|
2014-10-31 04:15:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2014-10-30 06:25:35 +08:00
|
|
|
};
|
2014-10-31 04:15:40 +08:00
|
|
|
|
|
|
|
// Default drivers/adaptors:
|
2014-12-16 03:15:29 +08:00
|
|
|
["loopback", "ping", "test-adaptor", "test-driver"].forEach(function(module) {
|
|
|
|
Registry.register("./test/" + module);
|
2014-10-31 04:15:40 +08:00
|
|
|
});
|