-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
43 lines (33 loc) · 1.07 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
var fs = require( 'fs' );
var shared = require('./shared'),
stores = {},
constructors = {
StringStore: require('./StringStore.js'),
KeyValueStore: require('./KeyValueStore.js')
},
storeDir = shared.storeDir;
function init(){
var t = new Date().getTime();
if(!fs.existsSync( storeDir ) ) {
fs.mkdirSync( storeDir );
}
var storeFiles = fs.readdirSync(storeDir);
storeFiles.forEach( createStoreFromFile );
console.log( Object.keys( stores ).length + ' dumbstores loaded in ' + ( new Date().getTime() - t ) + 'ms' );
}
init();
function createStoreFromFile( filename ){
var split = filename.split('.'),
storeName = split.slice(0, split.length - 1).join('.'),
contents = fs.readFileSync( storeDir + '/' + filename, 'utf8' ),
storeInfo = JSON.parse( contents ),
store = new constructors[storeInfo.type]( storeName, storeInfo.items );
stores[storeName] = store;
}
function getStore( type, name ){
var store = stores[name];
if( !store ) store = new constructors[ type ]( name );
stores[ name ] = store;
return store;
}
module.exports.getStore = getStore;