Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
msimerson committed Apr 15, 2024
1 parent 9282eaf commit 13dde5d
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 103 deletions.
2 changes: 1 addition & 1 deletion lib/reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions lib/regex.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

module.exports = regex = {
module.exports = {
section: /^\s*\[\s*([^\]]*?)\s*\]\s*$/,
param: /^\s*([\w@:._\-/[\]]+)\s*(?:=\s*(.*?)\s*)?$/,
comment: /^\s*[;#].*$/,
Expand Down
43 changes: 22 additions & 21 deletions lib/watch.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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])
Expand All @@ -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)
})
}
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)
})
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
37 changes: 14 additions & 23 deletions test/readers/ini.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,30 +46,24 @@ 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')
assert.strictEqual(r.sect1.str_false, 'false')
})

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')
Expand All @@ -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)
Expand Down
108 changes: 54 additions & 54 deletions test/regex.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
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)
})
})

0 comments on commit 13dde5d

Please sign in to comment.