Skip to content

Commit

Permalink
add watcher to get config dynamically.
Browse files Browse the repository at this point in the history
  • Loading branch information
rockdragon committed Dec 31, 2014
1 parent 82a6daf commit 8916c38
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
16 changes: 15 additions & 1 deletion modules/config/configUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ var path = require('path');

var cfgFileName = 'config.cfg';
var cache = {};
var filePath = path.join(process.env.spider_home, cfgFileName);

function getConfigs() {
if (!cache[cfgFileName]) {
if (!process.env.cloudDriveConfig) {
process.env.cloudDriveConfig = path.join(process.env.spider_home, cfgFileName);
process.env.cloudDriveConfig = filePath;
}
if (fs.existsSync(process.env.cloudDriveConfig)) {
var contents = fs.readFileSync(process.env.cloudDriveConfig, {encoding: 'utf-8'});
Expand All @@ -18,6 +19,19 @@ function getConfigs() {
}
module.exports.getConfigs = getConfigs;

function fileChanged(curr, prev){
if(curr.mtime !== prev.mtime) {
console.log('config has been changed. curr mtime is: ',
curr.mtime, 'prev mtime was: ' + prev.mtime);
if (fs.existsSync(filePath)) {
var contents = fs.readFileSync(filePath, {encoding: 'utf-8'});
cache[cfgFileName] = JSON.parse(contents);
console.log(cache[cfgFileName]);
}
}
}
fs.watchFile(filePath, fileChanged);

module.exports.isDevelopment = function(){
return getConfigs().node_env === 'development';
};
12 changes: 0 additions & 12 deletions test/test1.ls

This file was deleted.

0 comments on commit 8916c38

Please sign in to comment.