Skip to content

A simple utility for iterating over folders and files and importing modules from those files. It's in its early stages, there may be lots of incoming breaking changes.

License

Notifications You must be signed in to change notification settings

xayanide/file-folder-loader

Repository files navigation

file-folder-loader

A simple utility for iterating over folders and files and importing modules from those files. An abstraction over for-loops approach of loading files and folders. Syntactic sugar for iterating and loading folders and files.

Installation

npm install file-folder-loader

Usage & Examples

Example 1: Loading Folders

import * as nodeUrl from "node:url";
import * as nodePath from "node:path";
import { getFolders, loadFolders } from "file-folder-loader";

function getDirname(moduleAbsoluteFileUrl) {
    const fileName = nodeUrl.fileURLToPath(moduleAbsoluteFileUrl);
    return nodePath.dirname(fileName);
}

async function init() {
    const dirPath = nodePath.join(getDirname(import.meta.url), "someDirectory");
    const folderPaths = await getFolders(dirPath);
    if (folderPaths.length > 0) {
        await loadFolders(folderPaths, async (folderPath, folderName) => {
            console.log(`Loaded folder ${folderName} from path: ${folderPath}`);
        });
    }
}

await init();

Example 2: Loading Modules

import * as nodeUrl from "node:url";
import * as nodePath from "node:path";
import { getModules, loadModules } from "file-folder-loader";

function getDirname(moduleAbsoluteFileUrl) {
    const fileName = nodeUrl.fileURLToPath(moduleAbsoluteFileUrl);
    return nodePath.dirname(fileName);
}

async function init() {
    const dirPath = nodePath.join(getDirname(import.meta.url), "someDirectory");
    const modulePaths = await getModules(dirPath);
    if (modulePaths.length > 0) {
        await loadModules(modulePaths, async (moduleExport, moduleFileUrlHref, moduleFileName) => {
            console.log(`Loaded module ${moduleFileName} from path: ${moduleFileUrlHref}`);
            console.log(`Module export:`, moduleExport);
        });
    }
}

await init();

Example 3: Loading Folders Sequentially and Loading Modules Concurrently

import * as nodeUrl from "node:url";
import * as nodePath from "node:path";
import { getFolders, getModules, loadFolders, loadModules } from "file-folder-loader";

function getDirname(moduleAbsoluteFileUrl) {
    const fileName = nodeUrl.fileURLToPath(moduleAbsoluteFileUrl);
    return nodePath.dirname(fileName);
}

async function init() {
    const dirPath = nodePath.join(getDirname(import.meta.url), "someDirectory");
    // Recursive = false by default
    const folderPaths = await getFolders(dirPath);
    if (folderPaths.length > 0) {
        await loadFolders(folderPaths, async (folderPath, folderName) => {
            console.log(`Loaded folder ${folderName} from path: ${folderPath}`);
            // Recursive = false by default
            const modulePaths = await getModules(folderPath, false);
            if (modulePaths.length > 0) {
                await loadModules(modulePaths, async (moduleExport, moduleFileUrlHref, moduleFileName) => {
                    console.log(`Loaded module ${moduleFileName} from path: ${moduleFileUrlHref}`);
                    // moduleExport will be null because isImportEnabled is false (true by default)
                    console.log(`Module export:`, moduleExport);
                }, { isImportEnabled: false });
            }
        }, { processMode: "sequential" });
    }
}

await init();

Use Cases

  • Discord Bots: Load folders and files dynamically,
  • Modular Applications: Load user-defined files and folders dynamically.

The project's core tech stack

The project incorporates the following tools

About

A simple utility for iterating over folders and files and importing modules from those files. It's in its early stages, there may be lots of incoming breaking changes.

Resources

License

Stars

Watchers

Forks