@tiwarivikram/meanio
Version:
Extracted functionality for MEAN.io
37 lines (31 loc) • 1.22 kB
JavaScript
var fs = require('fs')
var _ = require('lodash')
var glob = require('glob')
var path = require('path')
var baseRgx = /(.*).(js|coffee)$/
// recursively walk modules path and callback for each file
function walk (wpath, type, excludeDir, callback) {
// regex - any chars, then dash type, 's' is optional, with .js or .coffee extension, case-insensitive
// e.g. articles-MODEL.js or mypackage-routes.coffee
var rgx = new RegExp('(.*)-' + type + '(s?).(js|coffee)$', 'i')
if (!fs.existsSync(wpath)) return
fs.readdirSync(wpath).forEach(function (file) {
var newPath = path.join(wpath, file)
var stat = fs.statSync(newPath)
if (stat.isFile() && (rgx.test(file) || (baseRgx.test(file)) && ~newPath.indexOf(type))) {
// if (!rgx.test(file)) console.log(' Consider updating filename:', newPath);
callback(newPath)
} else if (stat.isDirectory() && file !== excludeDir && ~newPath.indexOf(type)) {
walk(newPath, type, excludeDir, callback)
}
})
}
// ability to preload requirements for tests
function preload (gpath, type) {
glob.sync(gpath).forEach(function (file) {
walk(file, type, null, require)
})
}
exports.walk = walk
exports.preload = preload