isPathIgnored.js 1.2 KB

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