glob-util.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**
  2. * @fileoverview Utilities for working with globs and the filesystem.
  3. * @author Ian VanSchooten
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const lodash = require("lodash"),
  10. fs = require("fs"),
  11. path = require("path"),
  12. GlobSync = require("./glob"),
  13. pathUtil = require("./path-util"),
  14. IgnoredPaths = require("../ignored-paths");
  15. const debug = require("debug")("eslint:glob-util");
  16. //------------------------------------------------------------------------------
  17. // Helpers
  18. //------------------------------------------------------------------------------
  19. /**
  20. * Checks if a provided path is a directory and returns a glob string matching
  21. * all files under that directory if so, the path itself otherwise.
  22. *
  23. * Reason for this is that `glob` needs `/**` to collect all the files under a
  24. * directory where as our previous implementation without `glob` simply walked
  25. * a directory that is passed. So this is to maintain backwards compatibility.
  26. *
  27. * Also makes sure all path separators are POSIX style for `glob` compatibility.
  28. *
  29. * @param {Object} [options] An options object
  30. * @param {string[]} [options.extensions=[".js"]] An array of accepted extensions
  31. * @param {string} [options.cwd=process.cwd()] The cwd to use to resolve relative pathnames
  32. * @returns {Function} A function that takes a pathname and returns a glob that
  33. * matches all files with the provided extensions if
  34. * pathname is a directory.
  35. */
  36. function processPath(options) {
  37. const cwd = (options && options.cwd) || process.cwd();
  38. let extensions = (options && options.extensions) || [".js"];
  39. extensions = extensions.map(ext => ext.replace(/^\./, ""));
  40. let suffix = "/**";
  41. if (extensions.length === 1) {
  42. suffix += `/*.${extensions[0]}`;
  43. } else {
  44. suffix += `/*.{${extensions.join(",")}}`;
  45. }
  46. /**
  47. * A function that converts a directory name to a glob pattern
  48. *
  49. * @param {string} pathname The directory path to be modified
  50. * @returns {string} The glob path or the file path itself
  51. * @private
  52. */
  53. return function(pathname) {
  54. let newPath = pathname;
  55. const resolvedPath = path.resolve(cwd, pathname);
  56. if (fs.existsSync(resolvedPath) && fs.statSync(resolvedPath).isDirectory()) {
  57. newPath = pathname.replace(/[/\\]$/, "") + suffix;
  58. }
  59. return pathUtil.convertPathToPosix(newPath);
  60. };
  61. }
  62. //------------------------------------------------------------------------------
  63. // Public Interface
  64. //------------------------------------------------------------------------------
  65. /**
  66. * Resolves any directory patterns into glob-based patterns for easier handling.
  67. * @param {string[]} patterns File patterns (such as passed on the command line).
  68. * @param {Object} options An options object.
  69. * @returns {string[]} The equivalent glob patterns and filepath strings.
  70. */
  71. function resolveFileGlobPatterns(patterns, options) {
  72. const processPathExtensions = processPath(options);
  73. return patterns.filter(p => p.length).map(processPathExtensions);
  74. }
  75. const dotfilesPattern = /(?:(?:^\.)|(?:[/\\]\.))[^/\\.].*/;
  76. /**
  77. * Build a list of absolute filesnames on which ESLint will act.
  78. * Ignored files are excluded from the results, as are duplicates.
  79. *
  80. * @param {string[]} globPatterns Glob patterns.
  81. * @param {Object} [providedOptions] An options object.
  82. * @param {string} [providedOptions.cwd] CWD (considered for relative filenames)
  83. * @param {boolean} [providedOptions.ignore] False disables use of .eslintignore.
  84. * @param {string} [providedOptions.ignorePath] The ignore file to use instead of .eslintignore.
  85. * @param {string} [providedOptions.ignorePattern] A pattern of files to ignore.
  86. * @returns {string[]} Resolved absolute filenames.
  87. */
  88. function listFilesToProcess(globPatterns, providedOptions) {
  89. const options = providedOptions || { ignore: true };
  90. const files = [];
  91. const added = {};
  92. const cwd = options.cwd || process.cwd();
  93. const getIgnorePaths = lodash.memoize(
  94. optionsObj =>
  95. new IgnoredPaths(optionsObj)
  96. );
  97. /**
  98. * Executes the linter on a file defined by the `filename`. Skips
  99. * unsupported file extensions and any files that are already linted.
  100. * @param {string} filename The file to be processed
  101. * @param {boolean} shouldWarnIgnored Whether or not a report should be made if
  102. * the file is ignored
  103. * @param {IgnoredPaths} ignoredPaths An instance of IgnoredPaths
  104. * @returns {void}
  105. */
  106. function addFile(filename, shouldWarnIgnored, ignoredPaths) {
  107. let ignored = false;
  108. let isSilentlyIgnored;
  109. if (ignoredPaths.contains(filename, "default")) {
  110. ignored = (options.ignore !== false) && shouldWarnIgnored;
  111. isSilentlyIgnored = !shouldWarnIgnored;
  112. }
  113. if (options.ignore !== false) {
  114. if (ignoredPaths.contains(filename, "custom")) {
  115. if (shouldWarnIgnored) {
  116. ignored = true;
  117. } else {
  118. isSilentlyIgnored = true;
  119. }
  120. }
  121. }
  122. if (isSilentlyIgnored && !ignored) {
  123. return;
  124. }
  125. if (added[filename]) {
  126. return;
  127. }
  128. files.push({ filename, ignored });
  129. added[filename] = true;
  130. }
  131. debug("Creating list of files to process.");
  132. globPatterns.forEach(pattern => {
  133. const file = path.resolve(cwd, pattern);
  134. if (fs.existsSync(file) && fs.statSync(file).isFile()) {
  135. const ignoredPaths = getIgnorePaths(options);
  136. addFile(fs.realpathSync(file), true, ignoredPaths);
  137. } else {
  138. // regex to find .hidden or /.hidden patterns, but not ./relative or ../relative
  139. const globIncludesDotfiles = dotfilesPattern.test(pattern);
  140. let newOptions = options;
  141. if (!options.dotfiles) {
  142. newOptions = Object.assign({}, options, { dotfiles: globIncludesDotfiles });
  143. }
  144. const ignoredPaths = getIgnorePaths(newOptions);
  145. const shouldIgnore = ignoredPaths.getIgnoredFoldersGlobChecker();
  146. const globOptions = {
  147. nodir: true,
  148. dot: true,
  149. cwd
  150. };
  151. new GlobSync(pattern, globOptions, shouldIgnore).found.forEach(globMatch => {
  152. addFile(path.resolve(cwd, globMatch), false, ignoredPaths);
  153. });
  154. }
  155. });
  156. return files;
  157. }
  158. module.exports = {
  159. resolveFileGlobPatterns,
  160. listFilesToProcess
  161. };