Skip to content

Commit

Permalink
eslint updates (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
msimerson committed Sep 14, 2017
1 parent f373846 commit aec1fe8
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 70 deletions.
28 changes: 14 additions & 14 deletions config.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
'use strict';

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

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

module.exports = new Config();

function Config (root_path, no_overrides) {
this.root_path = root_path || cfreader.config_path;
this.module_config = function (defaults_path, overrides_path) {
var cfg = new Config(path.join(defaults_path, 'config'), true);
const cfg = new Config(path.join(defaults_path, 'config'), true);
if (overrides_path) {
cfg.overrides_path = path.join(overrides_path, 'config');
}
Expand All @@ -22,17 +22,17 @@ function Config (root_path, no_overrides) {
}

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

var full_path = path.resolve(this.root_path, a[0]);
const full_path = path.resolve(this.root_path, a[0]);

var results = cfreader.read_config(full_path, a[1], a[2], a[3]);
let results = cfreader.read_config(full_path, a[1], a[2], a[3]);

if (this.overrides_path) {
var overrides_path = path.resolve(this.overrides_path, a[0]);
const overrides_path = path.resolve(this.overrides_path, a[0]);

var overrides = cfreader.read_config(overrides_path, a[1], a[2], a[3]);
const overrides = cfreader.read_config(overrides_path, a[1], a[2], a[3]);

results = merge_config(results, overrides, a[1]);
}
Expand Down Expand Up @@ -67,7 +67,7 @@ function merge_config (defaults, overrides, type) {
}

function merge_struct (defaults, overrides) {
for (var k in overrides) {
for (const k in overrides) {
if (k in defaults) {
if (typeof overrides[k] === 'object' &&
typeof defaults[k] === 'object') {
Expand Down Expand Up @@ -96,12 +96,12 @@ config.get('thing', type, cb, options);
*/

Config.prototype.arrange_args = function (args) {
var fs_name = args.shift();
var fs_type = null;
var cb;
var options;
const fs_name = args.shift();
let fs_type = null;
let cb;
let options;

for (var i=0; i < args.length; i++) {
for (let i=0; i < args.length; i++) {
if (args[i] === undefined) continue;
switch (typeof args[i]) { // what is it?
case 'function':
Expand Down
68 changes: 34 additions & 34 deletions configfile.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict';

// Config file loader
var fs = require('fs');
var path = require('path');
const fs = require('fs');
const path = require('path');

// for "ini" type files
var regex = exports.regex = {
const regex = exports.regex = {
section: /^\s*\[\s*([^\]]*?)\s*\]\s*$/,
param: /^\s*([\w@:\._\-\/\[\]]+)\s*(?:=\s*(.*?)\s*)?$/,
comment: /^\s*[;#].*$/,
Expand All @@ -18,7 +18,7 @@ var regex = exports.regex = {
is_array: /(.+)\[\]$/,
};

var cfreader = exports;
const cfreader = exports;

cfreader.watch_files = true;
cfreader._config_cache = {};
Expand All @@ -29,7 +29,7 @@ cfreader._enoent_files = {};
cfreader._sedation_timers = {};
cfreader._overrides = {};

var config_dir_candidates = [
const config_dir_candidates = [
path.join(__dirname, 'config'), // Haraka ./config dir
__dirname, // npm packaged plugins
];
Expand All @@ -54,9 +54,9 @@ function get_path_to_config_dir () {
}

for (let i=0; i < config_dir_candidates.length; i++) {
let candidate = config_dir_candidates[i];
const candidate = config_dir_candidates[i];
try {
var stat = fs.statSync(candidate);
const stat = fs.statSync(candidate);
if (stat && stat.isDirectory()) {
cfreader.config_path = candidate;
return;
Expand Down Expand Up @@ -107,14 +107,14 @@ cfreader.on_watch_event = function (name, type, options, cb) {
cfreader.watch_dir = function () {
// NOTE: Has OS platform limitations:
// https://nodejs.org/api/fs.html#fs_fs_watch_filename_options_listener
var cp = cfreader.config_path;
const cp = cfreader.config_path;
if (cfreader._watchers[cp]) return;

var watcher = function (fse, filename) {
const watcher = function (fse, filename) {
if (!filename) return;
var full_path = path.join(cp, filename);
const full_path = path.join(cp, filename);
if (!cfreader._read_args[full_path]) return;
var args = cfreader._read_args[full_path];
const args = cfreader._read_args[full_path];
if (args.options && args.options.no_watch) return;
if (cfreader._sedation_timers[filename]) {
clearTimeout(cfreader._sedation_timers[filename]);
Expand Down Expand Up @@ -161,7 +161,7 @@ cfreader.watch_file = function (name, type, cb, options) {
};

cfreader.get_cache_key = function (name, options) {
var result;
let result;

// Ignore options etc. if this is an overriden value
if (cfreader._overrides[name]) {
Expand Down Expand Up @@ -196,7 +196,7 @@ 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);
const cache_key = cfreader.get_cache_key(name, options);
// console.log('\tcache_key: ' + cache_key);
if (cfreader._config_cache[cache_key] !== undefined) {
// console.log('\t' + name + ' is cached');
Expand All @@ -205,7 +205,7 @@ cfreader.read_config = function (name, type, cb, options) {
}

// load config file
var result = cfreader.load_config(name, type, options);
const result = cfreader.load_config(name, type, options);
if (!cfreader.watch_files) return result;

// We can watch the directory on these platforms which
Expand Down Expand Up @@ -249,8 +249,8 @@ function fsWatchDir (dirPath) {
cfreader._watchers[dirPath] = fs.watch(dirPath, { persistent: false }, function (fse, filename) {
// console.log('event: ' + fse + ', ' + filename);
if (!filename) return;
var full_path = path.join(dirPath, filename);
var args = cfreader._read_args[dirPath];
const full_path = path.join(dirPath, filename);
const args = cfreader._read_args[dirPath];
// console.log(args);
if (cfreader._sedation_timers[full_path]) {
clearTimeout(cfreader._sedation_timers[full_path]);
Expand All @@ -265,15 +265,15 @@ function fsWatchDir (dirPath) {
cfreader.read_dir = function (name, opts, done) {

cfreader._read_args[name] = { opts: opts }
var type = opts.type || 'binary';
const type = opts.type || 'binary';

isDirectory(name)
.then((result) => {
return fsReadDir(name);
})
.then((result2) => {
var reader = require('./readers/' + type);
var promises = [];
const reader = require('./readers/' + type);
const promises = [];
result2.forEach(function (file) {
promises.push(reader.loadPromise(path.resolve(name, file)))
});
Expand All @@ -294,16 +294,16 @@ cfreader.ensure_enoent_timer = function () {
if (cfreader._enoent_timer) return;
// Create timer
cfreader._enoent_timer = setInterval(function () {
var files = Object.keys(cfreader._enoent_files);
for (var i=0; i<files.length; i++) {
var fileOuter = files[i];
const files = Object.keys(cfreader._enoent_files);
for (let i=0; i<files.length; i++) {
const fileOuter = files[i];
/* BLOCK SCOPE */
(function (file) {
fs.stat(file, function (err) {
if (err) return;
// File now exists
delete(cfreader._enoent_files[file]);
var args = cfreader._read_args[file];
const args = cfreader._read_args[file];
cfreader.load_config(
file, args.type, args.options, args.cb);
cfreader._watchers[file] = fs.watch(
Expand All @@ -325,21 +325,21 @@ cfreader.get_filetype_reader = function (type) {
};

cfreader.load_config = function (name, type, options) {
var result;
let result;

if (!type) {
type = path.extname(name).toLowerCase().substring(1);
}

var cfrType = cfreader.get_filetype_reader(type);
let cfrType = cfreader.get_filetype_reader(type);

if (!fs.existsSync(name)) {

if (!/\.json$/.test(name)) {
return cfrType.empty(options, type);
}

var yaml_name = name.replace(/\.json$/, '.yaml');
const yaml_name = name.replace(/\.json$/, '.yaml');
if (!fs.existsSync(yaml_name)) {
return cfrType.empty(options, type);
}
Expand All @@ -349,7 +349,7 @@ cfreader.load_config = function (name, type, options) {
cfrType = cfreader.get_filetype_reader(type);
}

var cache_key = cfreader.get_cache_key(name, options);
const cache_key = cfreader.get_cache_key(name, options);
try {
switch (type) {
case 'ini':
Expand Down Expand Up @@ -380,11 +380,11 @@ 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;
var cache_key = cfreader.get_cache_key(name, options);
const cp = cfreader.config_path;
const 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++) {
const ck_keys = Object.keys(cfreader._config_cache[cache_key]);
for (let 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 All @@ -393,10 +393,10 @@ cfreader.process_file_overrides = function (name, options, result) {
// Allow JSON files to create or overwrite other
// configuration file data by prefixing the
// outer variable name with ! e.g. !smtp.ini
var keys = Object.keys(result);
for (var j=0; j<keys.length; j++) {
const keys = Object.keys(result);
for (let j=0; j<keys.length; j++) {
if (keys[j].substr(0,1) !== '!') continue;
var fn = keys[j].substr(1);
const fn = keys[j].substr(1);
// Overwrite the config cache for this filename
console.log('Overriding file ' + fn + ' with config from ' + name);
cfreader._config_cache[path.join(cp, fn)] = result[keys[j]];
Expand Down
8 changes: 4 additions & 4 deletions test/readers/binary.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

var fs = require('fs');
const fs = require('fs');

var _set_up = function (done) {
const _set_up = function (done) {
this.bin = require('../../readers/binary');
done();
};
Expand All @@ -21,8 +21,8 @@ exports.load = {
},
'loads the test binary file': function (test) {
test.expect(3);
var testBin = 'test/config/test.binary';
var result = this.bin.load(testBin);
const testBin = 'test/config/test.binary';
const result = this.bin.load(testBin);
test.ok(Buffer.isBuffer(result));
test.equal(result.length, 120);
test.deepEqual(result, fs.readFileSync(testBin));
Expand Down
10 changes: 5 additions & 5 deletions test/readers/flat.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

var regex = require('../../configfile').regex;
const regex = require('../../configfile').regex;

var _set_up = function (done) {
const _set_up = function (done) {
this.flat = require('../../readers/flat');
done();
};
Expand All @@ -21,21 +21,21 @@ exports.load = {
},
'loads the test flat file, as list': function (test) {
test.expect(1);
var result = this.flat.load(
const result = this.flat.load(
'test/config/test.flat', 'list', null, regex);
test.deepEqual(result, [ 'line1', 'line2', 'line3', 'line5' ]);
test.done();
},
'loads the test flat file, unspecified type': function (test) {
test.expect(1);
var result = this.flat.load(
const result = this.flat.load(
'test/config/test.flat', null, null, regex);
test.deepEqual(result, 'line1');
test.done();
},
'returns hostname for empty "me"': function (test) {
test.expect(1);
var result = this.flat.load( 'test/config/me', null, null, regex);
const result = this.flat.load( 'test/config/me', null, null, regex);
console.log(result);
test.ok(result);
test.done();
Expand Down
Loading

0 comments on commit aec1fe8

Please sign in to comment.