fileUtils.js 1004 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. 'use strict';
  2. /**
  3. * Module dependencies.
  4. */
  5. var _ = require('lodash'),
  6. glob = require('glob');
  7. /**
  8. * Get files by glob patterns
  9. */
  10. module.exports.getGlobbedFiles = function(globPatterns, removeRoot) {
  11. // For context switching
  12. var _this = this;
  13. // URL paths regex
  14. var urlRegex = new RegExp('^(?:[a-z]+:)?\/\/', 'i');
  15. // The output array
  16. var output = [];
  17. // If glob pattern is array so we use each pattern in a recursive way, otherwise we use glob
  18. if (_.isArray(globPatterns)) {
  19. globPatterns.forEach(function(globPattern) {
  20. output = _.union(output, _this.getGlobbedFiles(globPattern, removeRoot));
  21. });
  22. } else if (_.isString(globPatterns)) {
  23. if (urlRegex.test(globPatterns)) {
  24. output.push(globPatterns);
  25. } else {
  26. glob(globPatterns, {
  27. sync: true
  28. }, function(err, files) {
  29. if (removeRoot) {
  30. files = files.map(function(file) {
  31. return file.replace(removeRoot, '');
  32. });
  33. }
  34. output = _.union(output, files);
  35. });
  36. }
  37. }
  38. return output;
  39. };