require a directory asynchronously.
yarn add require-directory-async
npm i require-directory-async
- offered async operation
- support typescript
- offered filter
// router/index.js
const requireDirectoryAsync = require('require-directory-async');
module.exports = requireDirectoryAsync(module);
- use in other file
const routerPromise = require('./router');
routerPromise.then((module) => {
console.log(module) // directory module
})
- add another directory
// router/index.js
const requireDirectoryAsync = require('require-directory-async');
module.exports = requireDirectoryAsync(module, {
addPath: ['../routerAddition']
});
- filter
// router/index.js
const requireDirectoryAsync = require('require-directory-async');
module.exports = requireDirectoryAsync(module, {
filter (module) {
// filter operation here
return module;
}
});
- set recurse
// router/index.js
const requireDirectoryAsync = require('require-directory-async');
module.exports = requireDirectoryAsync(module, {
recurse: false
});
npm run test
yarn test
- for more detail, such as ts usage, please view
test/
.