-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.js
134 lines (114 loc) · 3.35 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const _ = require('lodash');
const goblin = require('./lib/goblin');
const modes = require('./lib/logger/mode');
const Ambush = require('./lib/ambush');
const Database = require('./lib/database');
/**
* Initialize Goblin DB with the data (if any) stored in the files
*
* @param {object} config Optional. Overwrite initial configuration.
* Ex. {@link @see './config.js'}
* @param {function} cb Optional callback called when the database has finished restoring
* data from files and it's ready to work. This callback takes only
* one parameter which is the error message (if any) that happen in
* the db initialization.
* @returns {object} Goblin instance.
*/
function GoblinExports(config, cb) {
if (!cb) {
cb = function() {};
}
// Set configuration
goblin.config = initialConfiguration(goblin.config, config);
// Initialize current database
Database.init(function(dbError) {
if (dbError) {
return cb(dbError);
}
// Initialize current Ambush Database
Ambush.init(function(ambError) {
cb(ambError);
});
});
return {
ambush: {
add: Ambush.add,
remove: Ambush.remove,
update: Ambush.update,
details: Ambush.details,
list: Ambush.list,
run: Ambush.run
},
on: goblin.hooks.add,
off: goblin.hooks.remove,
getConfig: function() {
return Object.assign({}, goblin.config);
},
updateConfig: function(newConfig) {
goblin.config = updateConfiguration(goblin.config, newConfig);
},
stopStorage: function() {
goblin.config.recordChanges = false;
},
startStorage: function() {
goblin.config.recordChanges = true;
},
get: Database.get,
push: Database.push,
set: Database.set,
update: Database.update,
delete: Database.delete,
truncate: Database.truncate
};
}
function isNotEmptyString(element) {
return element && typeof(element) === 'string';
}
function isBoolean(element) {
return typeof(element) === 'boolean';
}
function isValidMode(mode) {
return modes[mode] !== undefined;
}
function initialConfiguration(defaultConfiguration, externalConfiguration) {
const updated = updateConfiguration(defaultConfiguration, externalConfiguration);
if (!updated.files) {
updated.files = {
ambush: updated.fileName + '.goblin',
db: updated.fileName + '.json'
};
}
return updated;
}
function updateConfiguration(currentConfiguration, newConfiguration) {
if (
typeof(newConfiguration) !== 'object' ||
newConfiguration === null ||
Object.keys(newConfiguration).length === 0
) {
return currentConfiguration;
}
const updatedConfig = Object.assign({}, currentConfiguration);
if (isNotEmptyString(newConfiguration.fileName)) {
updatedConfig.fileName = newConfiguration.fileName;
updatedConfig.files = {
ambush: newConfiguration.fileName + '.goblin',
db: newConfiguration.fileName + '.json'
};
}
if (isNotEmptyString(newConfiguration.pointerSymbol)) {
updatedConfig.pointerSymbol = newConfiguration.pointerSymbol;
}
if (isBoolean(newConfiguration.recordChanges)) {
updatedConfig.recordChanges = newConfiguration.recordChanges;
}
if (isValidMode(newConfiguration.mode)) {
updatedConfig.mode = newConfiguration.mode;
}
// console.log(updateConfiguration, newConfiguration);
if (isNotEmptyString(newConfiguration.logPrefix)) {
updatedConfig.logPrefix = '[' + newConfiguration.logPrefix + ']'
}
return updatedConfig;
}
module.exports = GoblinExports;