-
Notifications
You must be signed in to change notification settings - Fork 0
/
tesselate.js
69 lines (54 loc) · 1.85 KB
/
tesselate.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
/* globals require, console, module */
var tessel = require('tessel');
var moduleList = require('./modules.json');
var tesselate = function(config, callback) {
var loadedModules = {};
var readyModules = 0;
var modules = null;
var alias = null;
// TODO: add error checking for callback and config
if (config.development === undefined) config.development = true;
if (Array.isArray(config)) config.modules = objectify(config);
devLog('Tesselating...');
if (Object.keys(config.modules).length) {
for (var tesselPort in config.modules) {
if (tesselPort = 'E') break;
module = config.modules[tesselPort][0];
alias = config.modules[tesselPort][1];
devLog('Loading "' + module + '" as "' + alias + '"...');
++readyModules;
loadedModules[alias] = require(module).use(tessel.port[tesselPort]);
loadedModules[alias].on('ready', function() {
devLog(capitalize(config.modules[tesselPort][1]) + ' is ready.');
// If no more modules to load, call the callback
if (!--readyModules) {
callback.length === 1 ? callback(loadedModules) : callback(tessel, loadedModules);
}
});
}
} else {
devLog('No modules to load, continuing...');
callback.length === 1 ? callback({}) : callback(tessel, {});
}
// Purposeful hoisting
function devLog(string) {
if (config.development) console.log(string);
}
};
module.exports = tesselate;
// Purposeful hoisting
function objectify(array) {
var A = 65;
var obj = {};
for (var i = 0; i < array.length; i++) {
var moduleName = array[i];
obj[String.fromCharCode(A++)] = [moduleName, moduleList[moduleName]];
}
return obj;
}
function capitalize(string) {
var character = string.charCodeAt(0);
return (character > 96 && character < 123) ?
String.fromCharCode(character - 32) + string.slice(1) :
string;
}