-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathActionConfig.js
More file actions
88 lines (73 loc) · 1.88 KB
/
ActionConfig.js
File metadata and controls
88 lines (73 loc) · 1.88 KB
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
const EventEmitter = require('events').EventEmitter;
let ACTION_CONF = {};
let configured = false;
// make this so conf can alert when its configured
const exportItem = new EventEmitter();
const FIELDS_FORMAT = [
'log',
{
name: 'time',
format: [
'isZeroTime',
'timeComp',
'now',
'toNumber',
'epoch'
]
},
{
name: 'messages',
format: [
'getMessage',
'getMessageConstants'
]
},
'ActionServerInterface',
'ActionClientInterface'
];
function reset() {
configured = false;
}
function readConfig(conf, formatFields, writeConf, keyPath = '') {
if (!formatFields) {
throw new Error('Unable to readConfig without format at ' + keyPath);
}
else if (!conf) {
throw new Error('Invalid config - missing entry at ' + keyPath);
}
for (let i = 0; i < formatFields.length; ++i) {
const formatField = formatFields[i];
if (typeof formatField === 'string') {
const confVal = conf[formatField];
if (confVal === undefined) {
const fullPath = keyPath ? keyPath + '.' + formatField : formatField;
throw new Error('Unable to readConfig without field ' + fullPath);
}
writeConf[formatField] = confVal;
}
else if (typeof formatField === 'object') {
const { name, format } = formatField;
if (!writeConf.hasOwnProperty(name)) {
writeConf[name] = {};
}
const fullPath = keyPath ? keyPath + '.' + name : name;
readConfig(conf[name], format, writeConf[name], fullPath);
}
}
}
function init(conf) {
readConfig(conf, FIELDS_FORMAT, ACTION_CONF);
configured = true;
exportItem.emit('configured', ACTION_CONF);
}
function isConfigured() {
return configured;
}
function get() {
return ACTION_CONF;
}
exportItem.init = init;
exportItem.reset = reset;
exportItem.isConfigured = isConfigured;
exportItem.get = get;
module.exports = exportItem;