UNPKG

simple-walker

Version:

simple-walker is a nodejs synchronous walker to search files inside dirs. It's specially designed to load dynamic files, like routes, modules, etc.

71 lines (57 loc) 2.05 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = function (location, dirFilter, fileFilter) { /** An array to store coincidences. */ var data = []; /** * @namespace * @property {object} listeners - An object composed by listeners ready to find coincidences. * @property {function} listeners.directories - A Function to operate over directories when a coincidence is found. * @property {function} listeners.file - A Function to operate over files when a coincidence is found. * @property {function} listeners.errors - A Function to catch errors. */ var options = { listeners: { directories: function directories(root, dirStatsArray, next) { dirStatsArray.forEach(function (element, index, array) { if (dirFilter && element.name === dirFilter || !dirFilter) { data.push({ dir: _path2.default.join(root, element.name), name: element.name, path: null }); } }); next(); }, file: function file(root, fileStats, next) { data.forEach(function (element, index, array) { element.path = !element.path ? function () { if (root === element.dir && fileStats.name === fileFilter) { return _path2.default.join(root, fileStats.name); } else if (!element.path) { return null; } }() : element.path; }); next(); }, errors: function errors(root, nodeStatsArray, next) { next(); } } }; /** * IMPORTANT: THIS METHOD IS SYNCHRONOUS * Search coincidences using the options defined. */ var walker = _walk2.default.walkSync(location, options); return data; }; var _path = require('path'); var _path2 = _interopRequireDefault(_path); var _walk = require('walk'); var _walk2 = _interopRequireDefault(_walk); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }