Skip to content

Commit

Permalink
fix: make config-loader fs operations async (fixes #23)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicokaiser committed Aug 8, 2024
1 parent 1069f36 commit d1e3494
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
21 changes: 13 additions & 8 deletions lib/config-loader.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const fs = require('fs');
const fs = require('node:fs/promises');
const path = require('path');

/**
Expand All @@ -12,16 +12,16 @@ const path = require('path');
* @return {Array}
* @private
*/
function walk(configDirectory, resourceName, resources) {
async function walk(configDirectory, resourceName, resources) {
resourceName = resourceName || '';
resources = resources || {};

fs.readdirSync(path.join(configDirectory, resourceName)).forEach((fileName) => {
for (const fileName of await fs.readdir(path.join(configDirectory, resourceName))) {
const subResourceName = (resourceName !== '' ? resourceName + '/' : '') + fileName;
const absoluteFilePath = path.join(configDirectory, subResourceName);
const stat = fs.statSync(absoluteFilePath);
const stat = await fs.stat(absoluteFilePath);
if (stat && stat.isDirectory()) {
walk(configDirectory, subResourceName, resources);
await walk(configDirectory, subResourceName, resources);
} else if (resourceName !== '') {
if (fileName.startsWith('config.')) {
if (!resources[resourceName]) resources[resourceName] = {};
Expand All @@ -32,7 +32,7 @@ function walk(configDirectory, resourceName, resources) {
resources[resourceName].instanceFile = absoluteFilePath;
}
}
});
}

return resources;
}
Expand All @@ -58,12 +58,17 @@ module.exports = async function configLoader(api, options) {
const configDirectory = path.resolve(cfg.directory);
const configParsers = cfg.parsers;

if (!fs.existsSync(configDirectory)) {
if (
!(await fs
.access(configDirectory)
.then(() => true)
.catch(() => false))
) {
throw new Error(`Config directory "${configDirectory}" does not exist`);
}

try {
resources = walk(configDirectory);
resources = await walk(configDirectory);
} catch (err) {
err.message = 'Error reading resource directory tree: ' + err.message;
throw err;
Expand Down
14 changes: 7 additions & 7 deletions test/config-loader.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('config-loader', () => {
});
});

xit('should read configs from directories', (done) => {
it('should read configs from directories', (done) => {
fsMock({
config: {
resource1: { 'config.xml': '' },
Expand All @@ -56,7 +56,7 @@ describe('config-loader', () => {
.catch(done);
});

xit('should read configs recursively', (done) => {
it('should read configs recursively', (done) => {
fsMock({
config: {
groupfolder1: {
Expand Down Expand Up @@ -90,7 +90,7 @@ describe('config-loader', () => {
.catch(done);
});

xit('should strip path to config directory from resource', (done) => {
it('should strip path to config directory from resource', (done) => {
fsMock({
configs: {
are: {
Expand Down Expand Up @@ -126,7 +126,7 @@ describe('config-loader', () => {
.catch(done);
});

xit('should strip path also for relative paths', (done) => {
it('should strip path also for relative paths', (done) => {
fsMock({
configs: {
'relative-path': {
Expand All @@ -150,7 +150,7 @@ describe('config-loader', () => {
.catch(done);
});

xit('should issue an error if no parser is found for a file extension extension', (done) => {
it('should issue an error if no parser is found for a file extension extension', (done) => {
fsMock({
config: {
resource1: { 'config.xml': '' },
Expand All @@ -169,7 +169,7 @@ describe('config-loader', () => {
});
});

xit('should register additional loaders', (done) => {
it('should register additional loaders', (done) => {
fsMock({
config: {
resource1: { 'config.xml': '' },
Expand All @@ -195,7 +195,7 @@ describe('config-loader', () => {
.catch(done);
});

xit('should ignore all other but config files', (done) => {
it('should ignore all other but config files', (done) => {
fsMock({
config: {
groupfolder1: {
Expand Down

0 comments on commit d1e3494

Please sign in to comment.