From 4971ea3e1a893b6c3b8e1a6f1d8ff69af248e8f7 Mon Sep 17 00:00:00 2001 From: Yevhen Badorov Date: Wed, 1 Mar 2023 22:29:07 +0200 Subject: [PATCH] Make API loader compatible with Metarhia api Implementation is taken from https://github.com/metatech-university/NodeJS-Pure/blob/55dce31954f2f8ac98287705eaaa64486794c5dc/src/load.js#L21-L33 --- src/loader.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/loader.js b/src/loader.js index 2abe23c..175e282 100644 --- a/src/loader.js +++ b/src/loader.js @@ -22,13 +22,15 @@ const load = async (filePath, sandbox) => { }; const loadDir = async (dir, sandbox) => { - const files = await fsp.readdir(dir); + const files = await fsp.readdir(dir, { withFileTypes: true }); const container = {}; - for (const fileName of files) { - if (!fileName.endsWith('.js')) continue; - const filePath = path.join(dir, fileName); - const name = path.basename(fileName, '.js'); - container[name] = await load(filePath, sandbox); + for (const file of files) { + const { name } = file; + if (file.isFile() && !name.endsWith('.js')) continue; + const location = path.join(dir, name); + const key = path.basename(name, '.js'); + const loader = file.isFile() ? load : loadDir; + container[key] = await loader(location, sandbox); } return container; };