-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathadapter.js
60 lines (52 loc) · 1.51 KB
/
adapter.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
const fs = require('fs');
const path = require('path');
const util = require('./util');
// Maps from typeName to class function
var subClasses = {}; // private static field
var normNames = {}; // normalize the type name
module.exports = (function(){
// constructor
function cls(app, acct)
{
// public instance method
this.send = function(probNum, filePath, callback){
// suf includes the dot
var suf = path.extname(filePath);
if (suf.length <= 1)
{
callback({message: "Cannot auto-detect language"});
return;
}
suf = suf.substring(1).toLowerCase();
this._send(probNum, filePath, suf, callback);
};
this.account = function(){
return acct;
};
}
cls.normalizeType = function(s){
return normNames[s.toLowerCase()];
};
// public static method
cls.create = function(app, acct){
var clsFn = subClasses[acct.type().toLowerCase()];
if (clsFn) return new clsFn(app, acct);
return null;
};
return cls;
})();
/*
* Auto load the subclasses
*/
(function(){
var files = fs.readdirSync(__dirname);
for (var i=0; i < files.length; i++)
{
var match = /^adapter(\w+)/i.exec(files[i]);
if (!match) continue;
var modName = match[1];
var lower = modName.toLowerCase();
normNames[lower] = modName;
subClasses[lower] = require('./'+files[i]);
}
})();