isPathIgnored.mjs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { isAbsolute, relative, resolve } from 'path';
  2. import micromatch from 'micromatch';
  3. import filterFilePaths from './utils/filterFilePaths.mjs';
  4. import getConfigForFile from './getConfigForFile.mjs';
  5. import getFileIgnorer from './utils/getFileIgnorer.mjs';
  6. /**
  7. * To find out if a path is ignored, we need to load the config,
  8. * which may have an ignoreFiles property. We then check the path
  9. * against these.
  10. * @param {import('stylelint').InternalApi} stylelint
  11. * @param {string} [filePath]
  12. * @returns {Promise<boolean>}
  13. */
  14. export default async function isPathIgnored(stylelint, filePath) {
  15. if (!filePath) {
  16. return false;
  17. }
  18. const cwd = stylelint._options.cwd;
  19. const result = await getConfigForFile({ stylelint, searchPath: filePath, filePath });
  20. if (!result) {
  21. return true;
  22. }
  23. const ignoreFiles = result.config.ignoreFiles || [];
  24. const absoluteFilePath = isAbsolute(filePath) ? filePath : resolve(cwd, filePath);
  25. if (micromatch([absoluteFilePath], ignoreFiles).length > 0) {
  26. return true;
  27. }
  28. const ignorer = getFileIgnorer(stylelint._options);
  29. if (filterFilePaths(ignorer, [relative(cwd, absoluteFilePath)]).length === 0) {
  30. return true;
  31. }
  32. return false;
  33. }