Skip to content

Commit

Permalink
added path resolution for npm packaged plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
msimerson committed Feb 28, 2016
1 parent 7b37e12 commit 9a39b10
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 23 deletions.
6 changes: 3 additions & 3 deletions config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

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

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

module.exports = new Config();

Expand All @@ -17,7 +17,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
34 changes: 31 additions & 3 deletions configfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,37 @@ 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_dirs = [
path.join(__dirname, 'config'), // Haraka ./config dir
__dirname, // npm packaged plugins
];

function get_path_to_config_dir () {
if (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_dirs.length; i++) {
try {
var stat = fs.statSync(config_dirs[i]);
if (stat && stat.isDirectory()) {
cfreader.config_path = config_dirs[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
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
31 changes: 15 additions & 16 deletions test/config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

process.env.NODE_ENV = 'test'

var path = require('path');

var config = require('../config');
Expand Down Expand Up @@ -161,16 +163,13 @@ function _test_get(test, name, type, callback, options, expected) {

exports.get = {
// config.get('name');
'name=test (non-existing)' : function (test) {
'test (non-existing)' : function (test) {
_test_get(test, 'test', null, null, null, null);
},

// config.get('name.ini');
'name.ini' : function (test) {
_test_get(test, 'test.ini', null, null, null, { "main": {} });
},
// config.get('test.ini');
'test.ini, no opts' : function (test) {
_test_get(test, '../test/config/test.ini', null, null, null, {
_test_get(test, 'test.ini', null, null, null, {
main: { bool_true: 'true', bool_false: 'false', str_true: 'true', str_false: 'false' },
sect1: { bool_true: 'true', bool_false: 'false', str_true: 'true', str_false: 'false' },
whitespace: { str_no_trail: 'true', str_trail: 'true' },
Expand All @@ -191,53 +190,53 @@ exports.get = {

// config.get('test.flat');
'test.flat, type=' : function (test) {
_test_get(test, '../test/config/test.flat', null, null, null, 'line1');
_test_get(test, 'test.flat', null, null, null, 'line1');
},

// NOTE: the test.flat file had to be duplicated for these tests, to avoid
// the config cache from returning invalid results.

// config.get('test.flat', 'value');
'test.flat, type=value' : function (test) {
_test_get(test, '../test/config/test.value', 'value', null, null, 'line1');
_test_get(test, 'test.value', 'value', null, null, 'line1');
},
// config.get('test.flat', 'list');
'test.flat, type=list' : function (test) {
_test_get(test, '../test/config/test.list', 'list', null, null,
_test_get(test, 'test.list', 'list', null, null,
['line1', 'line2','line3', 'line5'] );
},
// config.get('test.flat', 'data');
'test.flat, type=data' : function (test) {
_test_get(test, '../test/config/test.data', 'data', null, null,
_test_get(test, 'test.data', 'data', null, null,
['line1', 'line2','line3', '', 'line5'] );
},

// config.get('test.json');
'test.json, type=' : function (test) {
_test_get(test, '../test/config/test.json', null, null, null, jsonRes);
_test_get(test, 'test.json', null, null, null, jsonRes);
},
// config.get('test.json', 'json');
'test.json, type=json' : function (test) {
_test_get(test, '../test/config/test.json', 'json', null, null, jsonRes);
_test_get(test, 'test.json', 'json', null, null, jsonRes);
},

// config.get('test.yaml');
'test.yaml, type=' : function (test) {
_test_get(test, '../test/config/test.yaml', null, null, null, yamlRes);
_test_get(test, 'test.yaml', null, null, null, yamlRes);
},
// config.get('test.yaml', 'yaml');
'test.yaml, type=yaml' : function (test) {
_test_get(test, '../test/config/test.yaml', 'yaml', null, null, yamlRes);
_test_get(test, 'test.yaml', 'yaml', null, null, yamlRes);
},
// config.get('missing.json');
'missing.yaml, asked for json' : function (test) {
_test_get(test, '../test/config/missing.json', 'json', null, null, {"matt": "waz here"});
_test_get(test, 'missing.json', 'json', null, null, {"matt": "waz here"});
},

// config.get('test.bin', 'binary');
'test.bin, type=binary' : function (test) {
test.expect(2);
var res = config.get('../test/config/test.binary', 'binary');
var res = config.get('test.binary', 'binary');
test.equal(res.length, 120);
test.ok(Buffer.isBuffer(res));
test.done();
Expand Down
2 changes: 2 additions & 0 deletions test/configfile.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

process.env.NODE_ENV === 'test';

var _set_up = function (done) {
this.cfreader = require('../configfile');
this.opts = { booleans: ['main.bool_true','main.bool_false'] };
Expand Down

0 comments on commit 9a39b10

Please sign in to comment.