Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

directory-import

Package Overview
Dependencies
Maintainers
1
Versions
45
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

directory-import

Module sync or async import(requires) all modules from the folder you specify.

  • 1.0.5
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
29K
increased by22.73%
Maintainers
1
Weekly downloads
 
Created
Source

Node version required GitHub code size Downloads Discord server

About

The module will allow you to sync or async import(requires) all modules from the folder you specify.

It's possible either to use modules from the returned object, or to execute a callback at each module

Installation

npm i directory-import

After install, you can require module:

const importDir = require('directory-import');

// Code

Usage

For example, we have the following directory structure:

alt text

In the code below, we gave several examples of how to import all modules into "someDir" directory and all its subdirectories.

const importDir = require('directory-import');

// EX 1
// Simple loading of all modules inside the directory and in all its subdirectories
importDir(`./someDir`, 'sync');

// EX 2
// Loading and working with all modules inside the directory and in all its subdirectories
importDir(`./someDir`, 'sync', (name, path, func) => {
  console.info(
    `name: ${name} \n` +
    `path: ${path} \n` +
    `func: ${func} \n`
  );
  
  // name: someFile1
  // path: ./someDir/someFile1.js
  // func: () => console.info('this is some file 1')
  //
  // name: someFile2
  // path: ./someDir/someFile2.js
  // func: () => console.info('this is some file 2')
  //
  // name: config
  // path: ./someDir/config.json
  // func: [object Object]
  //
  // name: someModule1
  // path: ./someDir/someSubDir/someModule1.js
  // func: () => console.info('this is some module 1')
  //
  // name: someModule2
  // path: ./someDir/someSubDir/someModule2.js
  // func: () => console.info('this is some module 2')
});

// EX 3
// The same as with the sync method above. 
// However, modules load in order from fastest loaded to slowest loaded
importDir(`./someDir`, 'async', (name, path, func) => {
  console.info(
    `name: ${name} \n` +
    `path: ${path} \n` +
    `func: ${func} \n`
  );
});

// EX 4
// Loading and storing modules in the object
const modulesSync = importDir(`./someDir`, 'sync');

console.info(modulesSync);
// { 
//   someFile1: [Function],
//   someFile2: [Function],
//   config: { some: 'text', author: 'kiidii' },
//   someModule1: [Function],
//   someModule2: [Function]
// }

// EX 5
// The same as with the sync method above. 
// However, modules load in order from fastest loaded to slowest loaded
const modulesAsync = importDir(`./someDir`, 'async');


setTimeout(() => {
  console.info(modulesAsync);
}, 1000);

You can easily combine this methods.

const modules = importDir(`./someDir`, 'sync', (name, path, func) => {
  console.info(
    `name: ${name} \n` +
    `path: ${path} \n` +
    `func: ${func} \n`
  );
  
  // name: someFile1
  // path: ./someDir/someFile1.js
  // func: () => console.info('this is some file 1')
  //
  // name: someFile2
  // path: ./someDir/someFile2.js
  // func: () => console.info('this is some file 2')
  //
  // name: config
  // path: ./someDir/config.json
  // func: [object Object]
  //
  // name: someModule1
  // path: ./someDir/someSubDir/someModule1.js
  // func: () => console.info('this is some module 1')
  //
  // name: someModule2
  // path: ./someDir/someSubDir/someModule2.js
  // func: () => console.info('this is some module 2')
});

console.info(modules);
// { 
//   someFile1: [Function],
//   someFile2: [Function],
//   config: { some: 'text', author: 'kiidii' },
//   someModule1: [Function],
//   someModule2: [Function]
// }

Help

If you don't understand something in the documentation, you are experiencing problems, or you just need a gentle nudge in the right direction, please don't hesitate to join our official Discord server.

Although the server was created for Russian speakers, you can also write in English! We will understand you!

Keywords

FAQs

Package last updated on 31 Jul 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc