I'm working on a node.js application (discord bot).
I have several different files like events, models, services, etc. Folder Structure.
I'm facing the following problem: I need to require all files first thing, they need to be easily accessible from any file, and I also need to be able to use the Code Preview and Go To Definition Visual Studio Code tools.
This is my current approach:
const models = {};
const services = {};
const modelsFolder = 'models/folder/path';
const servicesFolder = 'services/folder/path';
const getFolderFiles = (folder, previousFolder) => {
const tempMap = new Map();
// Recursive search on all files inside the folder.
// Returns a Map, where the filename will be the key,
// and the require is the value.
tempMap.set('fileName', require('found/file/path'));
return tempMap;
};
// Method called from the index.js starting script.
const fetchAllFiles = () => {
const modelsMap = getFolderFiles(modelsFolder);
const servicesMap = getFolderFiles(servicesFolder);
// Assign found Maps to the exportable Object.
};
module.exports = {
fetchAllFiles,
models,
services,
};
After that, I can access anything that I want, be It a service or a specific model.
const { models, services } = require('holder/file/path');
const { ModelA, ModelB } = models;
const { MethodX1, MethodX2 } = services.ServiceX;
const { MethodY1, MethodY2 } = services.ServiceY;
// Do stuff...
The Problem: No type definition found, I really need to be able to peek at other files, since there are dozens of them.
I don't think this workflow is bad, but if there's a better one, I'd appreciate It!