index.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. 'use strict';
  2. var minimatch = require('minimatch');
  3. module.exports = function (file, condition, options) {
  4. if (!file) {
  5. throw new Error('gulp-match: vinyl file required');
  6. }
  7. if (typeof condition === 'boolean') {
  8. return condition;
  9. }
  10. if (typeof condition === 'function') {
  11. return !!condition(file);
  12. }
  13. if (typeof condition === 'string' && condition.match(/^\*\.[a-z\.]+$/)) {
  14. var newCond = condition.substring(1).replace(/\./g,'\\.')+'$';
  15. condition = new RegExp(newCond);
  16. }
  17. if (typeof condition === 'object' && typeof condition.test === 'function' && Object.getPrototypeOf(condition).hasOwnProperty('source')) {
  18. // FRAGILE: ASSUME: it's a regex
  19. return condition.test(file.relative);
  20. }
  21. if (typeof condition === 'string') {
  22. // FRAGILE: ASSUME: it's a minimatch expression
  23. return minimatch(file.relative, condition, options);
  24. }
  25. if (Array.isArray(condition)) {
  26. // FRAGILE: ASSUME: it's a minimatch expression
  27. if (!condition.length) {
  28. throw new Error('gulp-match: empty glob array');
  29. }
  30. var i = 0, step, ret = false;
  31. for (i = 0; i < condition.length; i++) {
  32. step = condition[i];
  33. if (step[0] === '!') {
  34. if (minimatch(file.relative, step.slice(1), options)) {
  35. return false;
  36. }
  37. } else if (minimatch(file.relative, step, options)) {
  38. ret = true;
  39. }
  40. }
  41. return ret;
  42. }
  43. if (typeof condition === 'object') {
  44. if (condition.hasOwnProperty('isFile') || condition.hasOwnProperty('isDirectory')) {
  45. if (!file.hasOwnProperty('stat')) {
  46. return false; // TODO: what's a better status?
  47. }
  48. if (condition.hasOwnProperty('isFile')) {
  49. return (condition.isFile === file.stat.isFile());
  50. }
  51. if (condition.hasOwnProperty('isDirectory')) {
  52. return (condition.isDirectory === file.stat.isDirectory());
  53. }
  54. }
  55. }
  56. return !!condition;
  57. };