Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release v1.3.0 #76

Merged
merged 5 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ jobs:
secrets: inherit

ubuntu:
needs: [lint]
uses: haraka/.github/.github/workflows/ubuntu.yml@master

windows:
needs: [lint]
uses: haraka/.github/.github/workflows/windows.yml@master

macos:
uses: haraka/.github/.github/workflows/macos.yml@master
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/).

### Unreleased

### [1.3.0] - 2024-05-02

- feat: getDir is now recursive

### [1.2.4] - 2024-04-26

- fix(watch): callback was losing context. Use explicit obj
Expand Down Expand Up @@ -134,3 +138,4 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/).
[1.2.1]: https://github.com/haraka/haraka-config/releases/tag/v1.2.1
[1.2.2]: https://github.com/haraka/haraka-config/releases/tag/v1.2.2
[1.2.4]: https://github.com/haraka/haraka-config/releases/tag/v1.2.4
[1.3.0]: https://github.com/haraka/haraka-config/releases/tag/v1.3.0
2 changes: 1 addition & 1 deletion CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

This handcrafted artisinal software is brought to you by:

| <img height="80" src="https://avatars.githubusercontent.com/u/261635?v=4"><br><a href="https://github.com/msimerson">msimerson</a> (<a href="https://github.com/haraka/haraka-config/commits?author=msimerson">56</a>) | <img height="80" src="https://avatars.githubusercontent.com/u/42121756?v=4"><br><a href="https://github.com/PSSGCSim">PSSGCSim</a> (<a href="https://github.com/haraka/haraka-config/commits?author=PSSGCSim">7</a>) | <img height="80" src="https://avatars.githubusercontent.com/u/662371?v=4"><br><a href="https://github.com/baudehlo">baudehlo</a> (<a href="https://github.com/haraka/haraka-config/commits?author=baudehlo">1</a>) | <img height="80" src="https://avatars.githubusercontent.com/u/651048?v=4"><br><a href="https://github.com/Wesitos">Wesitos</a> (<a href="https://github.com/haraka/haraka-config/commits?author=Wesitos">1</a>) | <img height="80" src="https://avatars.githubusercontent.com/u/2270015?v=4"><br><a href="https://github.com/oreoluwa">oreoluwa</a> (<a href="https://github.com/haraka/haraka-config/commits?author=oreoluwa">1</a>) |
| <img height="80" src="https://avatars.githubusercontent.com/u/261635?v=4"><br><a href="https://github.com/msimerson">msimerson</a> (<a href="https://github.com/haraka/haraka-config/commits?author=msimerson">57</a>) | <img height="80" src="https://avatars.githubusercontent.com/u/42121756?v=4"><br><a href="https://github.com/PSSGCSim">PSSGCSim</a> (<a href="https://github.com/haraka/haraka-config/commits?author=PSSGCSim">7</a>) | <img height="80" src="https://avatars.githubusercontent.com/u/662371?v=4"><br><a href="https://github.com/baudehlo">baudehlo</a> (<a href="https://github.com/haraka/haraka-config/commits?author=baudehlo">1</a>) | <img height="80" src="https://avatars.githubusercontent.com/u/651048?v=4"><br><a href="https://github.com/Wesitos">Wesitos</a> (<a href="https://github.com/haraka/haraka-config/commits?author=Wesitos">1</a>) | <img height="80" src="https://avatars.githubusercontent.com/u/2270015?v=4"><br><a href="https://github.com/oreoluwa">oreoluwa</a> (<a href="https://github.com/haraka/haraka-config/commits?author=oreoluwa">1</a>) |
| :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: |

<sub>this file is maintained by [.release](https://github.com/msimerson/.release)</sub>
7 changes: 6 additions & 1 deletion config.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,13 @@ class Config {
}

getDir(name, opts, done) {
const dir = path.resolve(this.root_path, name)

// no callback, return promise
if (arguments.length < 3) return reader.read_dir(dir, opts)

reader
.read_dir(path.resolve(this.root_path, name), opts)
.read_dir(dir, opts)
.then((files) => {
done(null, files) // keep the API consistent
})
Expand Down
50 changes: 26 additions & 24 deletions lib/reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,30 +137,32 @@ class Reader {
return result
}

read_dir(name, opts = {}) {
return new Promise((resolve, reject) => {
this._read_args[name] = { opts }

fsp
.stat(name)
.then((stat) => stat.isDirectory())
.then(() => fsp.readdir(name))
.then(async (fileList) => {
const contents = []
for (const file of fileList) {
const type = opts.type ?? this.getType(file)
contents.push({
path: file,
data: this.load_config(path.resolve(name, file), type, opts),
})
}
return contents
})
.then(resolve)
.catch(reject)

if (opts.watchCb) watch.dir2(this, name)
})
async read_dir(name, opts = {}) {
this._read_args[name] = { opts }

const contents = []
const dirs = []

const stat = await fsp.stat(name)
if (stat.isDirectory()) dirs.push(name)

for (const dir of dirs) {
for (const entry of await fsp.readdir(dir)) {
const entryPath = path.join(dir, entry)
const stat = await fsp.stat(entryPath)
if (stat.isDirectory()) dirs.push(entryPath) // recursion
if (stat.isFile()) {
const type = opts.type ?? this.getType(entry)
contents.push({
path: entryPath,
data: this.load_config(entryPath, type, opts),
})
}
}
}

if (opts.watchCb) watch.dir2(this, name)
return contents
}

get_filetype_reader(type) {
Expand Down
3 changes: 2 additions & 1 deletion lib/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ Watch.dir2 = (reader, dirPath) => {
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
if (!/win|darwin/.test(process.platform)) watchOpts.recursive = false

watchers[dirPath] = fs.watch(dirPath, watchOpts, (fse, filename) => {
// console.log(`event: ${fse}, ${filename}`);
Expand All @@ -105,6 +105,7 @@ Watch.dir2 = (reader, dirPath) => {
args.opts.watchCb()
}, 2 * 1000)
})
watchers[dirPath].unref()
}

Watch.onEvent = (reader, name, args) => {
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.2.4",
"version": "1.3.0",
"homepage": "http://haraka.github.io",
"repository": {
"type": "git",
Expand Down
12 changes: 5 additions & 7 deletions test/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ describe('getDir', function () {
if (err) console.error(err)
assert.ifError(err)
assert.equal(err, null)
assert.equal(files.length, 3)
assert.equal(files.length, 4)
assert.equal(files[0].data, `contents1${os.EOL}`)
assert.equal(files[2].data, `contents3${os.EOL}`)
done()
Expand All @@ -484,8 +484,8 @@ describe('getDir', function () {
it('reloads when file in dir is touched', function (done) {
this.timeout(3500)

// due to differences in fs.watch, this test is not reliable on Mac OS X
if (/darwin/.test(process.platform)) return done()
// due to differences in fs.watch, this test is unreliable on Mac OS X
// if (/darwin/.test(process.platform)) return done()

let callCount = 0

Expand All @@ -496,17 +496,15 @@ describe('getDir', function () {
if (err) console.error(err)
callCount++
if (callCount === 1) {
// console.log(files);
assert.equal(err, null)
assert.equal(files.length, 3)
assert.equal(files.length, 4)
assert.equal(files[0].data, `contents1${os.EOL}`)
assert.equal(files[2].data, `contents3${os.EOL}`)
fs.writeFile(tmpFile, 'contents4\n', (err2) => {
assert.equal(err2, null)
// console.log('file touched, waiting for callback');
})
}
if (callCount === 2) {
} else if (callCount === 2) {
assert.equal(files[3].data, 'contents4\n')
fs.unlink(tmpFile, () => {})
done()
Expand Down
1 change: 1 addition & 0 deletions test/config/dir/subdir/4.flat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
contents4
25 changes: 13 additions & 12 deletions test/reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,22 +170,23 @@ describe('reader', function () {

describe('read_dir', function () {
it('returns dir contents', async function () {
// may have race collission with config.getDir test
const result = await this.cfreader.read_dir(
path.resolve('test/config/dir'),
)
assert.deepEqual(result, [
{ data: 'contents1', path: '1.ext' },
{ data: 'contents2', path: '2.ext' },
{ data: 'contents3', path: '3.ext' },
const dir = path.resolve('test/config/dir')
assert.deepEqual(await this.cfreader.read_dir(dir), [
{ data: 'contents1', path: path.join(dir, '1.ext') },
{ data: 'contents2', path: path.join(dir, '2.ext') },
{ data: 'contents3', path: path.join(dir, '3.ext') },
{ data: 'contents4', path: path.join(dir, 'subdir', '4.flat') },
])
})

it('returns dir with mixed types', async function () {
const result = await this.cfreader.read_dir('test/config/mixed')
assert.deepEqual(result, [
{ data: { main: {}, sect: { one: 'true' } }, path: '1.ini' },
{ data: { main: { two: false } }, path: '2.yml' },
const dir = path.join('test', 'config', 'mixed')
assert.deepEqual(await this.cfreader.read_dir(dir), [
{
data: { main: {}, sect: { one: 'true' } },
path: path.join(dir, '1.ini'),
},
{ data: { main: { two: false } }, path: path.join(dir, '2.yml') },
])
})
})
Expand Down
Loading