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

@beyonk/load-all

Package Overview
Dependencies
Maintainers
3
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@beyonk/load-all

Loads all files from within a directory

  • 0.3.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
22
increased by15.79%
Maintainers
3
Weekly downloads
 
Created
Source

Beyonk

Load All

js-standard-style CircleCI

Loads the contents of sub-directories of a specified directory into an object or an array, suitable for mass loading of small modules from a filesystem.

This library is used extensively at Beyonk for loading route mappings, validations, error objects, and anywhere where a large number of small modules needs to be loaded.

Installation

npm i -D @beyonk/load-all

Usage

The library only has two methods, both of which amalgamate all exported items from a list of files.

exportDir

exportDir which results in a hash of export name -> function (), so that a directory structured thusly:

/my-dir
|-- library1
|   `-- index.js
|-- library2and3
|   `-- index.js
| ...

can be imported with:

const exported = exportDir('/my-dir')

// {
// library1: (exported as `library` from library1.js),
// library2: (exported as `library2` from library2and3.js),
// library3: (exported as `library3` from library2and3.js)
// }

includeDir

includeDir results in a concatenated array of the contents of files (which should export an array themselves), so that a directory structured thusly:

/routes
|-- routes1
|   `-- index.js
|-- routes2and3
|   `-- index.js
| ...

can be imported with:

const routes = includeDir('/my-dir')

// [ route1, route2, route3 ]

Advanced usage

Logging

The library will let you know every file it is loading, if you specify the second parameter to any method:

includeDir('/some-dir', 'route')

will result in log messages similar to the following:

// Adding route from ./some-dir/my-route

Modifying file content

You can modify the content of the files you load before it is put into the final hash or array, if you, for instance, would like to capitalise the key names, or add metadata or similar.

const { capitalize } = require('lodash')

includeDir('/some-dir', 'some-label', exported => {
  return Object.keys(exported).reduce((acc, exportName) {
    acc[capitalize(exportName)] = exported[exportName]
    return acc
  }, {})
})

Developing

You can run the suite of unit tests with

npm run test

code is linted according to @beyonk/eslint-config

Keywords

FAQs

Package last updated on 01 Jul 2021

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