You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.1 KiB

4 years ago
  1. 'use strict';
  2. var glob = require('glob');
  3. /**
  4. * Expand one or more patterns into an Array of files.
  5. *
  6. * ## Examples:
  7. *
  8. * ```
  9. * globs('../*.js', function (err, jsfiles) {
  10. * console.log(jsfiles);
  11. * })
  12. *
  13. * globs(['*.js', '../*.js'], function (err, jsfiles) {
  14. * console.log(jsfiles)
  15. * })
  16. *
  17. * globs(['*.js', '../*.js'], { cwd: '/foo' }, function (err, jsfiles) {
  18. * console.log(jsfiles)
  19. * })
  20. * ```
  21. *
  22. * @param {String|Array} patterns One or more patterns to match
  23. * @param {Object} [options] Options
  24. * @param {Function} callback Function which accepts two parameters: err, files
  25. */
  26. var globs = module.exports = function (patterns, options, callback) {
  27. var pending
  28. , groups = [];
  29. // not an Array? make it so!
  30. if (!Array.isArray(patterns)) {
  31. patterns = [ patterns ];
  32. }
  33. pending = patterns.length;
  34. // parameter shifting is really horrible, but i'm
  35. // mimicing glob's api...
  36. if (typeof options === 'function') {
  37. callback = options;
  38. options = {};
  39. }
  40. if (!pending) {
  41. // nothing to do
  42. // ensure callback called asynchronously
  43. return process.nextTick(function() {
  44. callback(null, []);
  45. })
  46. }
  47. // walk the patterns
  48. patterns.forEach(function (pattern) {
  49. // grab the files
  50. glob(pattern, options, function (err, files) {
  51. if (err) {
  52. return callback(err);
  53. }
  54. // add the files to the group
  55. groups = groups.concat(files);
  56. pending -= 1;
  57. // last pattern?
  58. if (!pending) {
  59. // done
  60. return callback(null, groups);
  61. }
  62. });
  63. });
  64. };
  65. /**
  66. * Synchronously Expand one or more patterns to an Array of files
  67. *
  68. * @api public
  69. * @param {String|Array} patterns
  70. * @param {Object} [options]
  71. * @return {Array}
  72. */
  73. globs.sync = function (patterns, options) {
  74. options = options || {};
  75. var groups = []
  76. , index
  77. , length;
  78. if (!Array.isArray(patterns)) {
  79. patterns = [ patterns ];
  80. }
  81. for (index = 0, length = patterns.length; index < length; index++) {
  82. groups = groups.concat(glob.sync(patterns[index], options));
  83. }
  84. return groups;
  85. };