walk.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. var path = require('path');
  2. var fs = require('fs');
  3. var minimatch = require('minimatch');
  4. var fileMatcher = require('./filematcher.js');
  5. var Matchers = {};
  6. function isFolderExcluded(resource, opts) {
  7. if (!opts.exclude) {
  8. return false;
  9. }
  10. if (Matchers.exclude) {
  11. return Matchers.exclude.some(function(item) {
  12. if (item.indexOf(resource) === -1) {
  13. return false;
  14. }
  15. try {
  16. return fs.statSync(item).isDirectory();
  17. } catch (err) {
  18. return false;
  19. }
  20. });
  21. }
  22. return false;
  23. }
  24. function addMatcher(type, filePath, opts) {
  25. Matchers[type] = Matchers[type] || [];
  26. var matchers = fileMatcher[type].adaptFilePath(filePath, opts[type]);
  27. if (Array.isArray(matchers)) {
  28. Matchers[type].push.apply(Matchers[type], matchers);
  29. } else {
  30. Matchers[type].push(matchers);
  31. }
  32. }
  33. function walk(dir, done, opts) {
  34. var results = [];
  35. fs.readdir(dir, function(err, list) {
  36. if (err) {
  37. return done(err);
  38. }
  39. var pending = list.length;
  40. if (pending === 0) {
  41. return done(null, results);
  42. }
  43. list.forEach(function(resource) {
  44. resource = path.join(dir, resource);
  45. fs.stat(resource, function(err, stat) {
  46. if (stat && stat.isDirectory()) {
  47. var dirName = resource.split(path.sep).slice(-1)[0];
  48. var isExcluded = isFolderExcluded(resource, opts); // prevent loading of files from an excluded folder
  49. var isSkipped = opts.skipgroup && opts.skipgroup.indexOf(dirName) > -1;
  50. if (isExcluded || isSkipped) {
  51. pending = pending-1;
  52. } else {
  53. walk(resource, function(err, res) {
  54. results = results.concat(res);
  55. pending = pending-1;
  56. if (!pending) {
  57. done(null, results);
  58. }
  59. }, opts);
  60. }
  61. } else {
  62. results.push(resource);
  63. pending = pending-1;
  64. if (!pending) {
  65. done(null, results);
  66. }
  67. }
  68. });
  69. });
  70. });
  71. }
  72. module.exports = {
  73. readPaths : function (paths, cb, opts) {
  74. Matchers.exclude = [];
  75. Matchers.filter = [];
  76. var allmodules = [];
  77. var extensionPattern = /\.js$/;
  78. var modulePaths = paths.slice(0);
  79. (function readSourcePaths() {
  80. var sourcePath = modulePaths.shift();
  81. fs.stat(sourcePath, function(err, stat) {
  82. if (err) {
  83. return cb(err);
  84. }
  85. if (stat.isFile() && extensionPattern.test(sourcePath)) {
  86. sourcePath = sourcePath.replace(extensionPattern, '');
  87. if (allmodules.indexOf(sourcePath) === -1) {
  88. allmodules.push(sourcePath);
  89. }
  90. if (modulePaths.length) {
  91. readSourcePaths();
  92. } else {
  93. cb(null, allmodules);
  94. }
  95. } else if (stat.isDirectory()) {
  96. if (opts.exclude) {
  97. addMatcher('exclude', sourcePath, opts);
  98. }
  99. if (opts.filter) {
  100. addMatcher('filter', sourcePath, opts);
  101. }
  102. walk(sourcePath, function (err, list) {
  103. if (err) {
  104. return cb(err);
  105. }
  106. list.sort();
  107. var modules = list.filter(function (filePath) {
  108. if (!extensionPattern.test(filePath)) {
  109. return false;
  110. }
  111. if (opts.exclude && fileMatcher.exclude.match(filePath, Matchers.exclude)) {
  112. return false;
  113. }
  114. if (opts.filter && !fileMatcher.filter.match(filePath, Matchers.filter)) {
  115. return false;
  116. }
  117. var filename = filePath.split(path.sep).slice(-1)[0];
  118. if (opts.filename_filter) {
  119. return minimatch(filename, opts.filename_filter);
  120. }
  121. if (opts.tag_filter || opts.skiptags) {
  122. return fileMatcher.tags.match(filePath, opts);
  123. }
  124. return true;
  125. });
  126. modules.forEach(function(item) {
  127. var filename = item.replace(extensionPattern, '');
  128. if (allmodules.indexOf(filename) === -1) {
  129. allmodules.push(filename);
  130. }
  131. });
  132. if (modulePaths.length) {
  133. readSourcePaths();
  134. } else {
  135. cb(null, allmodules);
  136. }
  137. }, opts);
  138. } else {
  139. if (modulePaths.length) {
  140. readSourcePaths();
  141. } else {
  142. cb(null, allmodules);
  143. }
  144. }
  145. });
  146. })();
  147. }
  148. };