fileUtils.js 999 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. var files = glob(globPatterns, { sync: true});
  27. if (removeRoot) {
  28. files = files.map(function(file) {
  29. return file.replace(removeRoot, '');
  30. });
  31. }
  32. output = _.union(output, files);
  33. }
  34. }
  35. return output;
  36. };