Skip to content

Commit

Permalink
Merge pull request #1 from haraka/npm-packaged-plugins
Browse files Browse the repository at this point in the history
added path resolution for npm packaged plugins
  • Loading branch information
msimerson committed Feb 29, 2016
2 parents 7b37e12 + f56b6a9 commit cdff5bd
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 62 deletions.
7 changes: 4 additions & 3 deletions config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
'use strict';

var path = require('path');
var path = require('path');

var cfreader = require('./configfile');
var cfreader = require('./configfile');

module.exports = new Config();

function Config (root_path) {
this.root_path = root_path || cfreader.config_path;
// console.log('root_path: ' + this.root_path);
this.module_config = function (defaults_path, overrides_path) {
var cfg = new Config(path.join(defaults_path, 'config'));
if (overrides_path) {
Expand All @@ -17,7 +18,7 @@ function Config (root_path) {
};
}

Config.prototype.get = function(name, type, cb, options) {
Config.prototype.get = function (name, type, cb, options) {
var a = this.arrange_args([name, type, cb, options]);
if (!a[1]) a[1] = 'value';

Expand Down
58 changes: 43 additions & 15 deletions configfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,38 @@ var regex = exports.regex = {

var cfreader = exports;

cfreader.config_path = process.env.HARAKA ?
path.join(process.env.HARAKA, 'config')
: path.join(__dirname, './config');
var config_dir_candidates = [
path.join(__dirname, 'config'), // Haraka ./config dir
__dirname, // npm packaged plugins
];

function get_path_to_config_dir () {
if (process.env.HARAKA) {
// console.log('process.env.HARAKA: ' + process.env.HARAKA);
cfreader.config_path = path.join(process.env.HARAKA, 'config');
return;
}

if (process.env.NODE_ENV === 'test') {
cfreader.config_path = path.join(__dirname, 'test', 'config');
return;
}

for (var i=0; i < config_dir_candidates.length; i++) {
try {
var stat = fs.statSync(config_dir_candidates[i]);
if (stat && stat.isDirectory()) {
cfreader.config_path = config_dir_candidates[i];
return;
}
}
catch (ignore) {
console.error(ignore.message);
}
}
}
get_path_to_config_dir();
// console.log('cfreader.config_path: ' + cfreader.config_path);

cfreader.watch_files = true;
cfreader._config_cache = {};
Expand Down Expand Up @@ -157,14 +186,10 @@ cfreader.read_config = function(name, type, cb, options) {
// Check cache first
if (!process.env.WITHOUT_CONFIG_CACHE) {
var cache_key = cfreader.get_cache_key(name, options);
// console.log('\tcache_key: ' + cache_key);
if (cfreader._config_cache[cache_key]) {
var cached = cfreader._config_cache[cache_key];
// Make sure that any .ini file booleans are applied
if (type === 'ini' && (options && options.booleans &&
Array.isArray(options.booleans)))
{
cfreader.init_booleans(options, cached);
}
// console.log('\t' + name + ' is cached');
return cached;
}
}
Expand Down Expand Up @@ -244,6 +269,7 @@ cfreader.load_config = function (name, type, options) {
cfrType = cfreader.get_filetype_reader(type);
}

var cache_key = cfreader.get_cache_key(name, options);
try {
switch (type) {
case 'ini':
Expand All @@ -252,29 +278,31 @@ cfreader.load_config = function (name, type, options) {
case 'json':
case 'yaml':
result = cfrType.load(name);
cfreader.process_file_overrides(name, result);
cfreader.process_file_overrides(name, options, result);
break;
// case 'binary':
default:
result = cfrType.load(name, type, options, regex);
}
cfreader._config_cache[cache_key] = result;
}
catch (err) {
if (err.code !== 'EBADF') throw err;
if (cfreader._config_cache[name]) {
result = cfreader._config_cache[name];
if (cfreader._config_cache[cache_key]) {
result = cfreader._config_cache[cache_key];
}
}
return result;
};

cfreader.process_file_overrides = function (name, result) {
cfreader.process_file_overrides = function (name, options, result) {
// We might be re-loading this file:
// * build a list of cached overrides
// * remove them and add them back
var cp = cfreader.config_path;
if (cfreader._config_cache[name]) {
var ck_keys = Object.keys(cfreader._config_cache[name]);
var cache_key = cfreader.get_cache_key(name, options);
if (cfreader._config_cache[cache_key]) {
var ck_keys = Object.keys(cfreader._config_cache[cache_key]);
for (var i=0; i<ck_keys.length; i++) {
if (ck_keys[i].substr(0,1) !== '!') continue;
delete cfreader._config_cache[path.join(cp, ck_keys[i].substr(1))];
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "haraka-config",
"license": "MIT",
"description": "Haraka's config file loader",
"version": "1.0.1",
"version": "1.0.2",
"homepage": "http://haraka.github.io",
"repository": {
"type": "git",
Expand Down
Loading

0 comments on commit cdff5bd

Please sign in to comment.