1
1
var fs = require ( 'fs' ) ;
2
2
var xtend = require ( '../lib/extend.js' ) ;
3
+ var winston = require ( 'winston' ) ;
4
+ var pwd = require ( 'path' ) ;
5
+
3
6
// ------------------------------------------
4
7
// REQUIRE - HELPER
5
8
// ------------------------------------------
@@ -66,11 +69,11 @@ var loadPlugins = function(folder, conf){
66
69
67
70
// Ends with .prop
68
71
if ( file . endsWith ( '.prop' ) ) {
69
- console . log ( 'Loading plugin properties %s ...' , path ) ;
72
+ winston . log ( 'info' , 'Loading plugin properties %s ...' , path ) ;
70
73
try {
71
74
var json = fs . readFileSync ( path , 'utf8' ) ;
72
75
var plugin = JSON . parse ( json ) ;
73
- } catch ( ex ) { console . log ( ex ) ; }
76
+ } catch ( ex ) { winston . warn ( ex ) ; }
74
77
xtend . extend ( true , conf , plugin ) ;
75
78
}
76
79
} ) ;
@@ -81,19 +84,19 @@ var getJSON = function(name){
81
84
var path = 'plugins/' + name + '/' + name + '.prop' ;
82
85
if ( ! fs . existsSync ( path ) ) { return { } ; }
83
86
84
- console . log ( 'Loading plugin properties %s ...' , path ) ;
87
+ //winston .log('info', 'Loading plugin properties %s ...', path);
85
88
try {
86
89
var json = fs . readFileSync ( path , 'utf8' ) ;
87
90
return JSON . parse ( json ) ;
88
- } catch ( ex ) { console . log ( ex ) ; }
91
+ } catch ( ex ) { winston . warn ( ex ) ; }
89
92
}
90
93
91
94
/**
92
95
* Load default properties
93
96
*/
94
97
var loadProperties = function ( ) {
95
98
if ( ! fs . existsSync ( 'script/wsrnode.prop' ) ) { return { } ; }
96
- console . log ( 'Loading core properties...' ) ;
99
+ winston . info ( 'Loading core properties...' ) ;
97
100
var json = fs . readFileSync ( 'script/wsrnode.prop' , 'utf8' ) ;
98
101
return JSON . parse ( json ) ;
99
102
}
@@ -103,11 +106,32 @@ var loadProperties = function(){
103
106
*/
104
107
var loadCustoms = function ( ) {
105
108
if ( ! fs . existsSync ( 'custom.prop' ) ) { return { } ; }
106
- console . log ( 'Loading custom properties...' ) ;
109
+ winston . info ( 'Loading custom properties...' ) ;
107
110
var json = fs . readFileSync ( 'custom.prop' , 'utf8' ) ;
108
111
var parse = { } ;
109
- try { parse = JSON . parse ( json ) ; } catch ( ex ) { console . log ( ex . message ) ; }
110
- return parse
112
+ try { parse = JSON . parse ( json ) ; } catch ( ex ) { winston . error ( ex . message ) ; }
113
+
114
+ parse [ 'modules' ] = retains ( parse [ 'modules' ] , config [ 'modules' ] ) ;
115
+ parse [ 'phantoms' ] = retains ( parse [ 'phantoms' ] , config [ 'phantoms' ] ) ;
116
+ parse [ 'cron' ] = retains ( parse [ 'cron' ] , config [ 'cron' ] ) ;
117
+
118
+ return parse ;
119
+ }
120
+
121
+ var retains = function ( source , target ) {
122
+ if ( typeof source != 'object' ) return source ;
123
+
124
+ var clean = { } ;
125
+ Object . keys ( source ) . forEach ( function ( attr ) {
126
+ if ( attr == 'description' || attr == 'version' ) { return false ; }
127
+ if ( target [ attr ] === undefined
128
+ && attr != 'x' && attr != 'y'
129
+ && attr != 'w' && attr != 'h'
130
+ && attr != 'c' ) { return winston . log ( 'warn' , 'Bypass config: ' , attr ) ; }
131
+ clean [ attr ] = retains ( source [ attr ] , target [ attr ] ) ;
132
+ } ) ;
133
+
134
+ return clean ;
111
135
}
112
136
113
137
// ------------------------------------------
@@ -121,9 +145,9 @@ var saveProperties = function(cfg) {
121
145
122
146
//json = json.replace(/\{/g,"{\n ").replace(/\}/g,"\n }").replace(/,/g,",\n ");
123
147
fs . writeFileSync ( 'custom.prop' , json , 'utf8' ) ;
124
- console . log ( 'Properties saved successfully' ) ;
148
+ winston . info ( 'Properties saved successfully' ) ;
125
149
} catch ( ex ) {
126
- console . log ( 'Error while saving properties:' , ex . message ) ;
150
+ winston . log ( 'error' , 'Error while saving properties:' , ex . message ) ;
127
151
}
128
152
}
129
153
@@ -133,6 +157,15 @@ var saveProperties = function(cfg) {
133
157
134
158
var routes = function ( req , res , next ) {
135
159
160
+ var json = req . body . json ;
161
+ if ( json ) {
162
+ json = JSON . parse ( json ) ;
163
+ xtend . extend ( true , config , json ) ;
164
+ ConfigManager . save ( config ) ;
165
+ res . redirect ( '/home' ) ;
166
+ return ;
167
+ }
168
+
136
169
var key = req . body . key ;
137
170
if ( ! key ) { res . redirect ( '/home' ) ; return ; }
138
171
@@ -141,7 +174,7 @@ var routes = function(req, res, next){
141
174
142
175
Object . keys ( req . body ) . forEach ( function ( attr ) {
143
176
if ( attr == 'key' ) { return ; }
144
- console . log ( key + '.' + attr + ' => ' + req . body [ attr ] ) ;
177
+ winston . info ( key + '.' + attr + ' => ' + req . body [ attr ] ) ;
145
178
cnf [ attr ] = req . body [ attr ] ;
146
179
} ) ;
147
180
@@ -156,21 +189,52 @@ var routes = function(req, res, next){
156
189
157
190
var getModule = function ( name , uncache ) {
158
191
var module = false ;
192
+ var path = false ;
193
+
159
194
try {
160
- var path = ' ../../plugins/'+ name + '/' + name + '.js' ;
195
+ path = pwd . normalize ( __dirname + '/ ../../plugins/'+ name + '/' + name + '.js' ) ;
161
196
if ( config . debug || uncache ) { require . uncache ( path ) ; }
162
- module = require ( path ) ;
197
+ module = require ( path ) ;
163
198
}
164
199
catch ( ex ) {
165
200
try {
166
- var path = ' ../'+ name + '.js' ;
201
+ path = pwd . normalize ( __dirname + '/ ../'+ name + '.js' ) ;
167
202
if ( config . debug || uncache ) { require . uncache ( path ) ; }
168
203
module = require ( path ) ;
169
204
} catch ( ex ) { }
170
205
}
206
+
207
+ initModule ( module , name ) ;
208
+ if ( ! module ) { return false ; }
209
+ if ( uncache ) { return module ; }
210
+
211
+ // Force reload if file change
212
+
213
+ var modified = fs . statSync ( path ) . mtime . getTime ( ) ;
214
+ if ( ! module . lastModified ) {
215
+ module . lastModified = modified ;
216
+ }
217
+ else if ( module . lastModified < modified ) {
218
+ winston . log ( 'info' , 'Reloading ' + name ) ;
219
+ return getModule ( name , true ) ;
220
+ }
221
+
171
222
return module ;
172
223
}
173
224
225
+ var initModule = function ( module , name ) {
226
+ try {
227
+ if ( ! module ) { return ; }
228
+
229
+ if ( module . initialized ) { return ; }
230
+ module . initialized = true ;
231
+
232
+ winston . log ( 'info' , 'initModule: ' , name ) ;
233
+ if ( ! module . init ) { return ; }
234
+ module . init ( SARAH ) ;
235
+ } catch ( ex ) { winston . log ( 'warn' , 'initModule: ' + ex . message ) ; }
236
+ }
237
+
174
238
// ------------------------------------------
175
239
// GET TICKER
176
240
// ------------------------------------------
@@ -180,7 +244,7 @@ var getTicker = function(){
180
244
var url = 'https://dl.dropbox.com/u/255810/Encausse.net/Sarah/plugins/ticker.json' ;
181
245
var request = require ( 'request' ) ;
182
246
request ( { 'uri' : url , json : true } , function ( err , response , json ) {
183
- if ( err || response . statusCode != 200 ) { console . log ( "Can't retrieve remote ticker" ) ; return ; }
247
+ if ( err || response . statusCode != 200 ) { winston . info ( "Can't retrieve remote ticker" ) ; return ; }
184
248
ticker = json . message ; // delayed by 1 request
185
249
} ) ;
186
250
return ticker ;
@@ -208,7 +272,7 @@ var ConfigManager = {
208
272
xtend . extend ( true , config , loadPlugins ( ) ) ;
209
273
xtend . extend ( true , config , loadCustoms ( ) ) ;
210
274
}
211
- catch ( ex ) { console . log ( 'Error while loding properties:' , ex . message ) ; }
275
+ catch ( ex ) { winston . log ( 'error' , 'Error while loding properties:' , ex . message ) ; }
212
276
return ConfigManager ;
213
277
} ,
214
278
0 commit comments