index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*!
  2. * parse-glob <https://github.com/jonschlinkert/parse-glob>
  3. *
  4. * Copyright (c) 2015, Jon Schlinkert.
  5. * Licensed under the MIT License.
  6. */
  7. 'use strict';
  8. var isGlob = require('is-glob');
  9. var findBase = require('glob-base');
  10. var extglob = require('is-extglob');
  11. var dotfile = require('is-dotfile');
  12. /**
  13. * Expose `cache`
  14. */
  15. var cache = module.exports.cache = {};
  16. /**
  17. * Parse a glob pattern into tokens.
  18. *
  19. * When no paths or '**' are in the glob, we use a
  20. * different strategy for parsing the filename, since
  21. * file names can contain braces and other difficult
  22. * patterns. such as:
  23. *
  24. * - `*.{a,b}`
  25. * - `(**|*.js)`
  26. */
  27. module.exports = function parseGlob(glob) {
  28. if (cache.hasOwnProperty(glob)) {
  29. return cache[glob];
  30. }
  31. var tok = {};
  32. tok.orig = glob;
  33. tok.is = {};
  34. // unescape dots and slashes in braces/brackets
  35. glob = escape(glob);
  36. var parsed = findBase(glob);
  37. tok.is.glob = parsed.isGlob;
  38. tok.glob = parsed.glob;
  39. tok.base = parsed.base;
  40. var segs = /([^\/]*)$/.exec(glob);
  41. tok.path = {};
  42. tok.path.dirname = '';
  43. tok.path.basename = segs[1] || '';
  44. tok.path.dirname = glob.split(tok.path.basename).join('') || '';
  45. var basename = (tok.path.basename || '').split('.') || '';
  46. tok.path.filename = basename[0] || '';
  47. tok.path.extname = basename.slice(1).join('.') || '';
  48. tok.path.ext = '';
  49. if (isGlob(tok.path.dirname) && !tok.path.basename) {
  50. if (!/\/$/.test(tok.glob)) {
  51. tok.path.basename = tok.glob;
  52. }
  53. tok.path.dirname = tok.base;
  54. }
  55. if (glob.indexOf('/') === -1 && !tok.is.globstar) {
  56. tok.path.dirname = '';
  57. tok.path.basename = tok.orig;
  58. }
  59. var dot = tok.path.basename.indexOf('.');
  60. if (dot !== -1) {
  61. tok.path.filename = tok.path.basename.slice(0, dot);
  62. tok.path.extname = tok.path.basename.slice(dot);
  63. }
  64. if (tok.path.extname.charAt(0) === '.') {
  65. var exts = tok.path.extname.split('.');
  66. tok.path.ext = exts[exts.length - 1];
  67. }
  68. // unescape dots and slashes in braces/brackets
  69. tok.glob = unescape(tok.glob);
  70. tok.path.dirname = unescape(tok.path.dirname);
  71. tok.path.basename = unescape(tok.path.basename);
  72. tok.path.filename = unescape(tok.path.filename);
  73. tok.path.extname = unescape(tok.path.extname);
  74. // Booleans
  75. var is = (glob && tok.is.glob);
  76. tok.is.negated = glob && glob.charAt(0) === '!';
  77. tok.is.extglob = glob && extglob(glob);
  78. tok.is.braces = has(is, glob, '{');
  79. tok.is.brackets = has(is, glob, '[:');
  80. tok.is.globstar = has(is, glob, '**');
  81. tok.is.dotfile = dotfile(tok.path.basename) || dotfile(tok.path.filename);
  82. tok.is.dotdir = dotdir(tok.path.dirname);
  83. return (cache[glob] = tok);
  84. }
  85. /**
  86. * Returns true if the glob matches dot-directories.
  87. *
  88. * @param {Object} `tok` The tokens object
  89. * @param {Object} `path` The path object
  90. * @return {Object}
  91. */
  92. function dotdir(base) {
  93. if (base.indexOf('/.') !== -1) {
  94. return true;
  95. }
  96. if (base.charAt(0) === '.' && base.charAt(1) !== '/') {
  97. return true;
  98. }
  99. return false;
  100. }
  101. /**
  102. * Returns true if the pattern has the given `ch`aracter(s)
  103. *
  104. * @param {Object} `glob` The glob pattern.
  105. * @param {Object} `ch` The character to test for
  106. * @return {Object}
  107. */
  108. function has(is, glob, ch) {
  109. return is && glob.indexOf(ch) !== -1;
  110. }
  111. /**
  112. * Escape/unescape utils
  113. */
  114. function escape(str) {
  115. var re = /\{([^{}]*?)}|\(([^()]*?)\)|\[([^\[\]]*?)\]/g;
  116. return str.replace(re, function (outter, braces, parens, brackets) {
  117. var inner = braces || parens || brackets;
  118. if (!inner) { return outter; }
  119. return outter.split(inner).join(esc(inner));
  120. });
  121. }
  122. function esc(str) {
  123. str = str.split('/').join('__SLASH__');
  124. str = str.split('.').join('__DOT__');
  125. return str;
  126. }
  127. function unescape(str) {
  128. str = str.split('__SLASH__').join('/');
  129. str = str.split('__DOT__').join('.');
  130. return str;
  131. }