Skip to content
Searches for files in a directory and executes a callback for each. All insecure symlinks, dotfiles and files without extension are ignored.
JavaScript
Branch: development
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
dist
src
test
.babelrc
.editorconfig
.eslintignore
.eslintrc.json
.flowconfig
.gitignore
.npmignore
.travis.yml
LICENSE
README.md
package-lock.json
package.json

README.md

call-dir

Greenkeeper badge Build Status License npm version 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. 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);

Example: 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));

Example: 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
You can’t perform that action at this time.