Skip to content

zoodogood/import-directory

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

55 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NPM, GITHUB

Dynamically imports all files from a directory.

Warning:
Supports only ES6 import*!

Basic usage

import { ImportDirectory } from '@zoodogood/import-directory';

const path = "./folder";

const modules = await new ImportDirectory()
  .import(path);

modules.forEach(moduleExport => /*...*/)

With details

import { ImportDirectory } from '@zoodogood/import-directory';

// the example shows the default values
const options = {
  // Import one by one
  sync: true,

  // Import files from subfolders
  subfolders: false,

  // Ignore files not matching regular expression. Default all files that do not start with a dot and ending with `.js` or `ts`
  regex: /^[^.].*?\.(?:js|ts)$/i,

  // Call a function on every import
  callback: null,

  // Check if files are folders
  skipValidation: false,

  // Required for relative paths to work properly
  rootDirectory: process.cwd()
}

// Relative path from project root folder or absolute path.
const path = "./folder";


const importDirectory = new ImportDirectory(options);
const modules = await importDirectory.import(path);

for (const module of modules){
  // You will see the data exported from the file, equal to what you get with a normal import
  console.info(module);
}

About

No description or website provided.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published