1.0.0 • Published 3 years ago

import-as-module v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

About

Import a directory as module.

Installation

Node.js v12.0.0 or newer is recommended.

npm

npm i import-as-module

yarn

yarn add import-as-module

pnpm

pnpm add import-as-module

Syntax

importAsModule(path[, callback]);
  • path \ | \ | \

  • callback \

    • err \ | \ null when no error

    • path \ Path to directory or file

    • stats \<fs.Stats>

Converts a directory into object with its subdirectories & files as properties.

fs.readdirSync() is used to read the directory synchronously. While fs.statSync() is used to synchronously gather \<fs.Stats> for each & every directory or file. A file is imported as property using CJS (commonJS) require().

directory
├── subdirectory
│   └── file.js
└── file.js
const importAsModule = require('import-as-module');
console.log(importAsModule('./directory'));
/* Logs:
{
  file: {},
  subdirectory: { file: {} }
}
*/

When subdirectory & file have similar name, parent object (object representing the above directory) is assigned a property with file's name containing two properties, i.e dir & file where dir represents subdirectory & file represents file.

directory
├── file
│   └── file.js
└── file.js
const importAsModule = require('import-as-module');
console.log(importAsModule('./directory'));
/* Logs:
{
  file: {
    dir: { file: {} },
    file: {}
  }
}
*/

callback has three arguments, i.e err, path & stats. err argument returns Error on error, else null. path returns path to current directory or file. callback's main purpose is to act as a filter other than detecting errors. path can be used to filter files by their extensions. stats returns \<fs.Stats> which helps distinguishing directories & files. By default the callback is:

function defaultCallback(err, path, stats) {
  if (err) throw err;
  return stats.isDirectory() || /\.(js(on)?|node)$/.test(path);
}

Basic Example

util
├── time
│   ├── isUTC.js
│   └── ms.js
└── requestHander.js
const importAsModule = require('import-as-module');
console.log(importAsModule('./util'));
/* Logs:
{
  requestHandler: [Function: requestHandler],
  time: {
    isUTC: [Function: isUTC],
    ms: [Function: ms]
  }
}
*/

Callback Example

server
├── router
│   ├── config.json
│   └── routes.js
├── config.json
└── index.js
const importAsModule = require('import-as-module');
const mod = importAsModule('./server', (err, path, stats) => {
  if (err) throw err;
  return stats.isDirectory() || path.endsWith('.json');
});
console.log(mod);
/* Logs:
{
  config: {
    retryConnection: 60000,
    logger: true
  },
  router: { config: { redirect: '/' } }
}
*/

Sometimes, directory name may end with an extension & can cause confusion when trying to import files explicitly. callback can be used to overcome this problem effectively.

libs
├── core.js
│   ├── src
│   │   └── ...
│   ├── package.json
│   └── index.js
├── package.json
└── index.js
const importAsModule = require('import-as-module');
const mod = importAsModule('./libs', (err, path, stats) => {
  if (err) throw err;
  return stats.isFile() && path.endsWith('.js');
});
console.log(mod);
/* Logs:
{
  index: {
    'core.js': {
      modifiers: [Object],
      prototypes: [Object],
      objects: [Object],
      utils: [Object]
    }
  }
}
*/

License

Refer to LICENSE file.

Links