require-children.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2015 rain1017.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  12. // implied. See the License for the specific language governing
  13. // permissions and limitations under the License. See the AUTHORS file
  14. // for names of contributors.
  15. 'use strict';
  16. // Modified from require-directory
  17. // Require all modulues in one folder
  18. // each direct child (may be folder or file) is a module.
  19. var _ = require('underscore');
  20. var fs = require('fs');
  21. var join = require('path').join;
  22. var resolve = require('path').resolve;
  23. var dirname = require('path').dirname;
  24. var defaultOptions = {
  25. extensions: ['js', 'json', 'coffee'],
  26. rename: function (name) {
  27. return name;
  28. },
  29. visit: function (obj) {
  30. return obj;
  31. }
  32. };
  33. module.exports = function(m, path, options) {
  34. var retval = {}, includeFile = null;
  35. // path is optional
  36. if (path && !options && !_.isString(path)) {
  37. options = path;
  38. path = null;
  39. }
  40. // default options
  41. options = _.defaults(options || {}, defaultOptions);
  42. // if no path was passed in, assume the equivelant of __dirname from caller
  43. // otherwise, resolve path relative to the equivalent of __dirname
  44. if (!path) {
  45. path = dirname(m.filename);
  46. } else {
  47. path = resolve(dirname(m.filename), path);
  48. }
  49. includeFile = function (path, filename) {
  50. // verify file has valid extension
  51. if (!new RegExp('\\.(' + options.extensions.join('|') + ')$', 'i').test(filename)) {
  52. return false;
  53. }
  54. // if options.include is a RegExp, evaluate it and make sure the path passes
  55. if (options.include && options.include instanceof RegExp && !options.include.test(path)) {
  56. return false;
  57. }
  58. // if options.include is a function, evaluate it and make sure the path passes
  59. if (options.include && _.isFunction(options.include) && !options.include(path, filename)) {
  60. return false;
  61. }
  62. // if options.exclude is a RegExp, evaluate it and make sure the path doesn't pass
  63. if (options.exclude && options.exclude instanceof RegExp && options.exclude.test(path)) {
  64. return false;
  65. }
  66. // if options.exclude is a function, evaluate it and make sure the path doesn't pass
  67. if (options.exclude && _.isFunction(options.exclude) && options.exclude(path, filename)) {
  68. return false;
  69. }
  70. return true;
  71. };
  72. // get the path of each file in specified directory, append to current tree node, recurse
  73. path = resolve(path);
  74. var obj = null;
  75. fs.readdirSync(path).forEach(function (filename) {
  76. var joined = join(path, filename), files, key, obj;
  77. if (fs.statSync(joined).isDirectory()) {
  78. try{
  79. obj = m.require(joined);
  80. retval[options.rename(filename, joined, filename)] = options.visit(obj, joined, filename) || obj;
  81. }
  82. catch(e){
  83. }
  84. }
  85. else {
  86. if (joined !== m.filename && includeFile(joined, filename)) {
  87. key = filename.substring(0, filename.lastIndexOf('.')); // hash node key shouldn't include file extension
  88. if(key !== 'index'){ // Ignore index
  89. obj = m.require(joined);
  90. retval[options.rename(key, joined, filename)] = options.visit(obj, joined, filename) || obj;
  91. }
  92. }
  93. }
  94. });
  95. return retval;
  96. };