diff --git a/lib/reader.js b/lib/reader.js index 59bb2a1..0aaa46c 100644 --- a/lib/reader.js +++ b/lib/reader.js @@ -8,7 +8,7 @@ const watch = require('./watch') let config_dir_candidates = [ path.join(__dirname, '..', 'config'), // Haraka ./config dir - path.join(__dirname, '..') // npm packaged plugins + path.join(__dirname, '..'), // npm packaged plugins ] class cfreader { diff --git a/lib/regex.js b/lib/regex.js index 1e90c0d..4c61fac 100644 --- a/lib/regex.js +++ b/lib/regex.js @@ -1,5 +1,4 @@ - -module.exports = regex = { +module.exports = { section: /^\s*\[\s*([^\]]*?)\s*\]\s*$/, param: /^\s*([\w@:._\-/[\]]+)\s*(?:=\s*(.*?)\s*)?$/, comment: /^\s*[;#].*$/, diff --git a/lib/watch.js b/lib/watch.js index fb8ca4f..748a8fb 100644 --- a/lib/watch.js +++ b/lib/watch.js @@ -1,3 +1,5 @@ +const fs = require('node:fs') +const path = require('node:path') module.exports.file = (reader, name, type, cb, options) => { // This works on all OS's, but watch_dir() above is preferred for Linux and @@ -24,7 +26,6 @@ module.exports.file = (reader, name, type, cb, options) => { } } - module.exports.dir = (reader) => { // NOTE: Has OS platform limitations: // https://nodejs.org/api/fs.html#fs_fs_watch_filename_options_listener @@ -39,7 +40,7 @@ module.exports.dir = (reader) => { if (!filename) return const full_path = path.join(cp, filename) const args = reader._read_args[full_path] - if (!args) return + if (!args) return if (args.options?.no_watch) return if (reader._sedation_timers[filename]) { clearTimeout(reader._sedation_timers[filename]) @@ -58,24 +59,24 @@ module.exports.dir = (reader) => { } module.exports.dir2 = (reader, dirPath) => { - if (reader._watchers[dirPath]) return - const watchOpts = { persistent: false, recursive: true } + if (reader._watchers[dirPath]) return + const watchOpts = { persistent: false, recursive: true } - // recursive is only supported on Windows (win32, win64) and macOS (darwin) - if (!/win/.test(process.platform)) watchOpts.recursive = false + // recursive is only supported on Windows (win32, win64) and macOS (darwin) + if (!/win/.test(process.platform)) watchOpts.recursive = false - reader._watchers[dirPath] = fs.watch(dirPath, watchOpts, (fse, filename) => { - // console.log(`event: ${fse}, ${filename}`); - if (!filename) return - const full_path = path.join(dirPath, filename) - const args = reader._read_args[dirPath] - // console.log(args); - if (reader._sedation_timers[full_path]) { - clearTimeout(reader._sedation_timers[full_path]) - } - reader._sedation_timers[full_path] = setTimeout(() => { - delete reader._sedation_timers[full_path] - args.opts.watchCb() - }, 2 * 1000) - }) -} \ No newline at end of file + reader._watchers[dirPath] = fs.watch(dirPath, watchOpts, (fse, filename) => { + // console.log(`event: ${fse}, ${filename}`); + if (!filename) return + const full_path = path.join(dirPath, filename) + const args = reader._read_args[dirPath] + // console.log(args); + if (reader._sedation_timers[full_path]) { + clearTimeout(reader._sedation_timers[full_path]) + } + reader._sedation_timers[full_path] = setTimeout(() => { + delete reader._sedation_timers[full_path] + args.opts.watchCb() + }, 2 * 1000) + }) +} diff --git a/package.json b/package.json index 5c6733d..a957203 100644 --- a/package.json +++ b/package.json @@ -32,8 +32,8 @@ }, "scripts": { "format": "npm run prettier:fix && npm run lint:fix", - "lint": "npx eslint@^8 *.js readers test test/*/*.js", - "lint:fix": "npx eslint@^8 *.js readers test test/*/*.js --fix", + "lint": "npx eslint@^8 *.js lib test test/*/*.js", + "lint:fix": "npx eslint@^8 *.js lib test test/*/*.js --fix", "prettier": "npx prettier . --check", "prettier:fix": "npx prettier . --write --log-level=warn", "test": "npx mocha@10 test test/readers", diff --git a/test/readers/ini.js b/test/readers/ini.js index 99bbdfc..e31e057 100644 --- a/test/readers/ini.js +++ b/test/readers/ini.js @@ -46,12 +46,9 @@ describe('ini', function () { }) it('sect1, opts', function () { - const r = this.ini.load( - 'test/config/test.ini', - { - booleans: ['sect1.bool_true', 'sect1.bool_false'], - }, - ) + const r = this.ini.load('test/config/test.ini', { + booleans: ['sect1.bool_true', 'sect1.bool_false'], + }) assert.strictEqual(r.sect1.bool_true, true) assert.strictEqual(r.sect1.bool_false, false) assert.strictEqual(r.sect1.str_true, 'true') @@ -59,17 +56,14 @@ describe('ini', function () { }) it('sect1, opts, w/defaults', function () { - const r = this.ini.load( - 'test/config/test.ini', - { - booleans: [ - '+sect1.bool_true', - '-sect1.bool_false', - '+sect1.bool_true_default', - 'sect1.-bool_false_default', - ], - }, - ) + const r = this.ini.load('test/config/test.ini', { + booleans: [ + '+sect1.bool_true', + '-sect1.bool_false', + '+sect1.bool_true_default', + 'sect1.-bool_false_default', + ], + }) assert.strictEqual(r.sect1.bool_true, true) assert.strictEqual(r.sect1.bool_false, false) assert.strictEqual(r.sect1.str_true, 'true') @@ -79,12 +73,9 @@ describe('ini', function () { }) it('wildcard boolean', function () { - const r = this.ini.load( - 'test/config/test.ini', - { - booleans: ['+main.bool_true', '*.is_bool'], - }, - ) + const r = this.ini.load('test/config/test.ini', { + booleans: ['+main.bool_true', '*.is_bool'], + }) assert.strictEqual(r['*'], undefined) assert.strictEqual(r.main.bool_true, true) assert.strictEqual(r.main.is_bool, undefined) diff --git a/test/regex.js b/test/regex.js index d50d283..f095af0 100644 --- a/test/regex.js +++ b/test/regex.js @@ -3,57 +3,57 @@ const assert = require('node:assert') const regex = require('../lib/regex') describe('regex', function () { - it('section', function () { - assert.equal(regex.section.test('[foo]'), true) - assert.equal(regex.section.test('bar'), false) - assert.equal(regex.section.test('[bar'), false) - assert.equal(regex.section.test('bar]'), false) - }) - - it('param', function () { - assert.equal(regex.param.exec('foo=true')[1], 'foo') - assert.equal(regex.param.exec(';foo=true'), undefined) - }) - - it('comment', function () { - assert.equal(regex.comment.test('; true'), true) - assert.equal(regex.comment.test('false'), false) - }) - - it('line', function () { - assert.equal(regex.line.test(' boo '), true) - assert.equal(regex.line.test('foo'), true) - }) - - it('blank', function () { - assert.equal(regex.blank.test('foo'), false) - assert.equal(regex.blank.test(' '), true) - }) - - it('is_integer', function () { - assert.equal(regex.is_integer.test(1), true) - assert.equal(regex.is_integer.test(''), false) - assert.equal(regex.is_integer.test('a'), false) - }) - - it('is_float', function () { - assert.equal(regex.is_float.test('1.0'), true) - assert.equal(regex.is_float.test(''), false) - assert.equal(regex.is_float.test('45'), false) - }) - - it('is_truth', function () { - assert.equal(regex.is_truth.test('no'), false) - assert.equal(regex.is_truth.test('nope'), false) - assert.equal(regex.is_truth.test('nuh uh'), false) - assert.equal(regex.is_truth.test('yes'), true) - assert.equal(regex.is_truth.test('true'), true) - assert.equal(regex.is_truth.test(true), true) - }) - - it('is_array', function () { - assert.equal(regex.is_array.test('foo=bar'), false) - assert.equal(regex.is_array.test('foo'), false) - assert.equal(regex.is_array.test('foo[]'), true) - }) -}) \ No newline at end of file + it('section', function () { + assert.equal(regex.section.test('[foo]'), true) + assert.equal(regex.section.test('bar'), false) + assert.equal(regex.section.test('[bar'), false) + assert.equal(regex.section.test('bar]'), false) + }) + + it('param', function () { + assert.equal(regex.param.exec('foo=true')[1], 'foo') + assert.equal(regex.param.exec(';foo=true'), undefined) + }) + + it('comment', function () { + assert.equal(regex.comment.test('; true'), true) + assert.equal(regex.comment.test('false'), false) + }) + + it('line', function () { + assert.equal(regex.line.test(' boo '), true) + assert.equal(regex.line.test('foo'), true) + }) + + it('blank', function () { + assert.equal(regex.blank.test('foo'), false) + assert.equal(regex.blank.test(' '), true) + }) + + it('is_integer', function () { + assert.equal(regex.is_integer.test(1), true) + assert.equal(regex.is_integer.test(''), false) + assert.equal(regex.is_integer.test('a'), false) + }) + + it('is_float', function () { + assert.equal(regex.is_float.test('1.0'), true) + assert.equal(regex.is_float.test(''), false) + assert.equal(regex.is_float.test('45'), false) + }) + + it('is_truth', function () { + assert.equal(regex.is_truth.test('no'), false) + assert.equal(regex.is_truth.test('nope'), false) + assert.equal(regex.is_truth.test('nuh uh'), false) + assert.equal(regex.is_truth.test('yes'), true) + assert.equal(regex.is_truth.test('true'), true) + assert.equal(regex.is_truth.test(true), true) + }) + + it('is_array', function () { + assert.equal(regex.is_array.test('foo=bar'), false) + assert.equal(regex.is_array.test('foo'), false) + assert.equal(regex.is_array.test('foo[]'), true) + }) +})