1.4.0 • Published 4 years ago

call-dir v1.4.0

Weekly downloads
6
License
MIT
Repository
github
Last release
4 years ago

Default CI/CD Known Vulnerabilities npm package size npm version npm dependency Status npm downloads

call-dir searches for files in a directory and executes a callback for each. All symlinks, dotfiles and files without extension are ignored. Supports deep-loading. It can be used as an autoloader for JavaScript.

Installation

$ npm install call-dir

Usage

import load, { loadAll } from "call-dir";

load(directory, callback);
loadAll(directory, callback);

Examples

Basic loading

import path from "path";
import load, { loadAll } from "call-dir";

const models = path.resolve(__dirname, "./path/to/models");
const routes = path.resolve(__dirname, "./path/to/routes");

load(models, (fpath, fname) => {
  console.log(`Found file: ${fname} (absolute path: ${fpath})`);
});

// You can initialize modules from a directory easily:
load(models, (fpath) => require(fpath)(some, variables, ...here));
load(routes, (fpath) => require(fpath)(some, variables, ...here));

Deep loading

import path from "path";
import load, { loadAll } from "call-dir";

const modules = path.resolve(__dirname, "../node_modules");

// Those two calls are equivalents:
loadAll(modules, (fpath, fname) => /* … */);
load(modules, (fpath, fname) => /* … */, true);

Tests

$ npm test
1.4.0

4 years ago

1.3.0

6 years ago

1.2.1

6 years ago

1.1.0

6 years ago

1.0.1

7 years ago

1.0.0

7 years ago

0.0.2

7 years ago

0.0.1

8 years ago