Update conf and add walk utility

Signed-off-by: Alexandre Bodin <bodin.alex@gmail.com>
This commit is contained in:
Alexandre Bodin 2020-04-08 00:45:08 +02:00
parent 960315c066
commit 096f4b0df5
6 changed files with 47 additions and 31 deletions

View File

@ -1,7 +1,6 @@
'use strict';
const assert = require('assert');
const util = require('util');
const _ = require('lodash');
module.exports = (initialConfig = {}) => {
@ -26,9 +25,5 @@ module.exports = (initialConfig = {}) => {
_.merge(_config, ...args);
return this;
},
_dump() {
console.log(util.inspect(_config, false, null, true));
},
});
};

View File

@ -1,22 +1,13 @@
'use strict';
const path = require('path');
const fs = require('fs');
const fse = require('fs-extra');
const walk = require('./walk');
const loadFunctions = dir => {
if (!fs.existsSync(dir)) return {};
if (!fse.existsSync(dir)) return {};
return fs.readdirSync(dir, { withFileTypes: true }).reduce((acc, file) => {
const key = path.basename(file.name, path.extname(file.name));
if (file.isFile()) {
acc[key] = loadFunction(path.resolve(dir, file.name));
} else if (file.isDirectory()) {
acc[key] = loadFunctions(path.resolve(dir, file.name));
}
return acc;
}, {});
return walk(dir, { loader: loadFunction });
};
const loadFunction = file => {

View File

@ -2,21 +2,24 @@
const assert = require('assert');
const path = require('path');
const fs = require('fs');
const fse = require('fs-extra');
module.exports = dir => {
if (!fs.existsSync(dir)) return {};
if (!fse.existsSync(dir)) return {};
return fs
.readdirSync(dir, { withFileTypes: true })
.filter(file => file.isFile())
.reduce((acc, file) => {
const key = path.basename(file.name, path.extname(file.name));
const root = {};
const paths = fse.readdirSync(dir, { withFileTypes: true }).filter(fd => fd.isFile());
acc[key] = loadPolicy(path.resolve(dir, file.name));
for (let fd of paths) {
const { name } = fd;
const fullPath = dir + path.sep + name;
return acc;
}, {});
const ext = path.extname(name);
const key = path.basename(name, ext);
root[key] = loadPolicy(fullPath);
}
return root;
};
const loadPolicy = file => {

View File

@ -0,0 +1,27 @@
'use strict';
const assert = require('assert');
const path = require('path');
const fse = require('fs-extra');
module.exports = function walk(dir, { loader } = {}) {
assert(typeof loader === 'function', 'opts.loader must be a function');
const root = {};
const paths = fse.readdirSync(dir, { withFileTypes: true });
for (let fd of paths) {
const { name } = fd;
const fullPath = dir + path.sep + name;
if (fd.isDirectory()) {
root[name] = walk(fullPath, { loader });
} else {
const ext = path.extname(name);
const key = path.basename(name, ext);
root[key] = loader(fullPath);
}
}
return root;
};

View File

@ -1,7 +1,7 @@
'use strict';
const coreStoreModel = config => ({
connection: config.get('currentEnvironment.database.defaultConnection'),
connection: config.get('database.defaultConnection'),
uid: 'strapi::core-store',
internal: true,
info: {

View File

@ -4,7 +4,7 @@
'use strict';
const webhookModel = config => ({
connection: config.get('currentEnvironment.database.defaultConnection'),
connection: config.get('database.defaultConnection'),
uid: 'strapi::webhooks',
internal: true,
globalId: 'StrapiWebhooks',