-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
103 lines (82 loc) · 3.37 KB
/
index.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
const path = require('path');
const fs = require('fs');
async function clientListAsync() {
return clientList();
}
function clientList() {
const clientPath = path.join(__dirname,"/clients/");
const allClients = fs.readdirSync(clientPath, { withFileTypes: true }).filter(dirent => dirent.isDirectory()).map(dirent => dirent.name).filter(clnt => clnt !== 'template');
var clientInfo = [];
allClients.forEach(loadClient => {
try {
var dynModPath = path.join(__dirname,"/clients/",loadClient,"/client.js");
var dynClass = require(dynModPath);
var clientClass = new dynClass({});
clientInfo.push(Object.assign(clientClass.clientInfo,{
id: loadClient,
path: dynModPath,
authSupported: (clientClass.clientInfo.methods.includes("auth")) ? true : false,
signSupported: (clientClass.clientInfo.methods.includes("sign")) ? true : false,
toString: function() { return loadClient; }
}));
} catch(err) {
throw new Error(`getConfigObject Error: ${clientType} instancing error: ${err}`)
}
});
return clientInfo;
}
async function configFactoryAsync(settings) {
return configFactory(settings);
}
function configFactory(settings) {
//Make sure we get some good config data
if (!settings) throw new Error("getConfigObject Error: Settings are missing.");
if (!settings.clientType) throw new Error("getConfigObject Error: clientType property is missing.");
//Get the specified client and then delete tha value as it should not be forwarded
var clientType = settings.clientType;
delete settings.clientType;
// Get the path and if it exists
const dynModPath = path.join(__dirname,"/clients/",clientType,"/settings.js");
if (fs.existsSync(dynModPath)) {
// We get a simple object back so lets just return that
try {
return require(dynModPath)(settings);
} catch(err) {
throw new Error(`getConfigObject Error: ${clientType} instancing error: ${err}`)
}
} else {
throw new Error(`getConfigObject Error: ${clientType} is not found.`)
}
}
async function clientFactoryAsync(settings) {
return clientFactory(settings);
}
function clientFactory(settings) {
//Make sure we get some good input data
if (!settings) throw new Error("clientFactory Error: Client settings are missing.");
if (!settings.clientType) throw new Error("clientFactory Error: clientType property is missing.");
//Get the specified client and then delete tha value as it should not be forwarded
var clientType = settings.clientType;
delete settings.clientType;
// Get the path and if it exists
const dynModPath = path.join(__dirname,"/clients/",clientType,"/client.js");
if (fs.existsSync(dynModPath)) {
//Create a new client class and return it
try {
const clientClass = require(dynModPath);
return new clientClass(settings);
} catch(err) {
throw new Error(`clientFactory Error: ${clientType} instancing error: ${err}`)
}
} else {
throw new Error(`clientFactory Error: ${clientType} is not found.`)
}
}
module.exports = {
configFactory,
configFactoryAsync,
clientFactory,
clientFactoryAsync,
clientList,
clientListAsync
}