scan-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.
$ npm install @bartozzz/scan-dir
import scan, { scanRecursively } from "scan-dir";
scan(directory, callback);
scanRecursively(directory, callback);
import path from "path";
import scan, from "scan-dir";
const models = path.resolve(__dirname, "./path/to/models");
const routes = path.resolve(__dirname, "./path/to/routes");
scan(models, (fpath, fname) => {
console.log(`Found file: ${fname} (absolute path: ${fpath})`);
});
// You can initialize modules from a directory easily:
scan(models, (fpath) => require(fpath)(some, variables, ...here));
scan(routes, (fpath) => require(fpath)(some, variables, ...here));
import path from "path";
import scan, { scanRecursively } from "scan-dir";
const modules = path.resolve(__dirname, "../node_modules");
// Those two calls are equivalents:
scan(modules, (fpath, fname) => /* … */, true);
scanRecursively(modules, (fpath, fname) => /* … */);
$ npm test