index.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. 'use strict';
  2. var minimatch = require('minimatch');
  3. var path = require('path');
  4. function IgnoreMatcher(str) {
  5. var negated = [];
  6. this.negated = negated;
  7. var rooted = [];
  8. this.rooted = rooted;
  9. this.matchers = str.split(/\r?\n|\r/).map(function (line) {
  10. var negatedLine = line[0] === '!';
  11. var commentLine = line[0] === '#';
  12. var rootedLine = line[0] === '/';
  13. if (negatedLine || commentLine || rootedLine) {
  14. line = line.slice(1);
  15. }
  16. var emptyLine = line === '';
  17. if (emptyLine) {
  18. return null;
  19. }
  20. var isShellGlob = line.indexOf('/') >= 0;
  21. negated[negated.length] = negatedLine;
  22. rooted[rooted.length] = rootedLine || isShellGlob;
  23. return minimatch.makeRe(line, {
  24. comment: commentLine,
  25. empty: emptyLine,
  26. matchBase: !rootedLine,
  27. negated: true // negated
  28. });
  29. }).filter(Boolean);
  30. return this;
  31. }
  32. IgnoreMatcher.prototype.delimiter = path.sep;
  33. IgnoreMatcher.prototype.shouldIgnore = function (filename) {
  34. var isMatching = false;
  35. for (var i = 0; i < this.matchers.length; i++) {
  36. var matcher = this.matchers[i];
  37. if (this.rooted[i]) {
  38. if (matcher.test(filename)) {
  39. isMatching = !this.negated[i];
  40. }
  41. } else if (filename.split(this.delimiter).some(function (part) {
  42. return matcher.test(part);
  43. })) {
  44. isMatching = !this.negated[i];
  45. }
  46. }
  47. return isMatching;
  48. };
  49. exports.createMatcher = function (ignoreFileStr) {
  50. return new IgnoreMatcher(ignoreFileStr);
  51. };