https://github.com/bartozzz/scan-dir
Searches for files in a directory and executes a callback for each. All insecure symlinks, dotfiles and files without extension are ignored.
https://github.com/bartozzz/scan-dir
autoloader directory files fs import require
Last synced: 3 months ago
JSON representation
Searches for files in a directory and executes a callback for each. All insecure symlinks, dotfiles and files without extension are ignored.
- Host: GitHub
- URL: https://github.com/bartozzz/scan-dir
- Owner: Bartozzz
- License: mit
- Created: 2016-08-05T16:04:53.000Z (about 9 years ago)
- Default Branch: development
- Last Pushed: 2023-01-07T03:55:50.000Z (almost 3 years ago)
- Last Synced: 2025-02-24T17:07:04.243Z (8 months ago)
- Topics: autoloader, directory, files, fs, import, require
- Language: JavaScript
- Homepage: https://npmjs.com/package/call-dir
- Size: 1.73 MB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
scan-dir

[](https://snyk.io/test/github/Bartozzz/scan-dir?targetFile=package.json)
[](https://www.npmjs.com/package/scan-dir)
[](https://www.npmjs.com/package/@bartozzz/scan-dir)
[](https://www.npmjs.com/package/@bartozzz/scan-dir)
[](https://www.npmjs.com/package/@bartozzz/scan-dir)
**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.
## Installation
```bash
$ npm install @bartozzz/scan-dir
```## Usage
```javascript
import scan, { scanRecursively } from "scan-dir";scan(directory, callback);
scanRecursively(directory, callback);
```### Examples
#### Basic loading
```javascript
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));
```#### Deep loading
```javascript
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) => /* … */);
```## Tests
```bash
$ npm test
```