isPathIgnored.cjs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // NOTICE: This file is generated by Rollup. To modify it,
  2. // please instead edit the ESM counterpart and rebuild with Rollup (npm run build).
  3. 'use strict';
  4. const path = require('path');
  5. const micromatch = require('micromatch');
  6. const filterFilePaths = require('./utils/filterFilePaths.cjs');
  7. const getConfigForFile = require('./getConfigForFile.cjs');
  8. const getFileIgnorer = require('./utils/getFileIgnorer.cjs');
  9. /**
  10. * To find out if a path is ignored, we need to load the config,
  11. * which may have an ignoreFiles property. We then check the path
  12. * against these.
  13. * @param {import('stylelint').InternalApi} stylelint
  14. * @param {string} [filePath]
  15. * @returns {Promise<boolean>}
  16. */
  17. async function isPathIgnored(stylelint, filePath) {
  18. if (!filePath) {
  19. return false;
  20. }
  21. const cwd = stylelint._options.cwd;
  22. const result = await getConfigForFile({ stylelint, searchPath: filePath, filePath });
  23. if (!result) {
  24. return true;
  25. }
  26. const ignoreFiles = result.config.ignoreFiles || [];
  27. const absoluteFilePath = path.isAbsolute(filePath) ? filePath : path.resolve(cwd, filePath);
  28. if (micromatch([absoluteFilePath], ignoreFiles).length > 0) {
  29. return true;
  30. }
  31. const ignorer = getFileIgnorer(stylelint._options);
  32. if (filterFilePaths(ignorer, [path.relative(cwd, absoluteFilePath)]).length === 0) {
  33. return true;
  34. }
  35. return false;
  36. }
  37. module.exports = isPathIgnored;