-
Notifications
You must be signed in to change notification settings - Fork 0
/
cfgload.js
141 lines (129 loc) · 2.96 KB
/
cfgload.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
135
136
137
138
139
140
141
var fs = require("fs");
var util = require("util");
var path = require("path");
function debug(str){
console.log(str);
}
var log = {
error : function(str){
debug(str);
}
}
const LOAD_DELAY = 5000;//5s
exports.ctor = Cfg;
function Cfg(fname){
this.file_type = "";
this.load_timer = null
this.left_file_count = 0;
//file or directory,
/*the file mapping
* {
* "filenamea" : obj
* "filenameb" : obj
* }
*/
this.cfg_map = {};
var fpath = __dirname + "/" + fname;
var stat = fs.statSync(fpath);
if(stat.isFile()){
this.file_type = "FILE";
}else if(stat.isDirectory()){
this.file_type = "DIRECTORY";
}else{
log.error("no file or directory...");
return;
}
this.loadCfg(fpath);
var self = this;
fs.watchFile(fpath ,function(curr, prev){
if(self.load_timer) return;
if(curr.ctime.toString() != prev.ctime.toString()){
debug("p.ctime : " + prev.ctime + ";c.ctime : " + curr.ctime);
self.load_timer = setTimeout(function(){
self.loadCfg(fpath);
},LOAD_DELAY);
}
});
}
Cfg.prototype.get = function(fname){
if(this.file_type == "FILE"){
return this.cfg_map;
}else{
if(fname){
return this.cfg_map[fname];
}else{
return this.cfg_map;
}
}
}
Cfg.prototype.mapFile = function(fpath){
var self = this;
fs.readFile(fpath , function(err ,data){
if(err){
log.error("error loading config file :" + fpath);
}else{
try{
var obj = JSON.parse(data);
if(self.file_type == "FILE"){
self.cfg_map = obj;
}else{
var fname = path.basename(fpath);
self.cfg_map[fname] = obj;
}
}catch(e){
log.error("error parsing config file :" + fpath);
}
}
//this load procudre has finished ,restart watcher
if(--self.left_file_count <= 0){
self.load_timer = null;
//watchOnce();
}
})
}
//if we remove some cfg files
//the files returned by readdir don't contain these files
Cfg.prototype.eraseOldCfg = function(curFiles){
for(var file in this.cfg_map){
var found = false;
for(var i=0; i<curFiles.length; i++){
if(file == curFiles[i]){
found = true;
break;
}
}
if(found == false){
delete this.cfg_map[file];
}
}
}
function files_filter(files){
var arr = [];
files.forEach(function(f){
if(f[0] != '.'){
arr.push(f);
}
})
return arr;
}
Cfg.prototype.loadCfg = function(fpath){
debug("load config...");
if(this.file_type == "FILE"){
this.left_file_count = 1;
this.mapFile(fpath);
}else{
var self = this;
fs.readdir(fpath, function(err, fnames){
if(err){
log.error("error read config directory : " + fpath);
}else{
fnames = files_filter(fnames);
self.left_file_count = fnames.length;
fnames.forEach(function(file){
self.mapFile(fpath + "/" + file);
});
self.eraseOldCfg(fnames);
}
});
}
}