getFileIgnorer.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. 'use strict';
  2. // Try to get file ignorer from '.stylelintignore'
  3. const fs = require('fs');
  4. const path = require('path');
  5. const { default: ignore } = require('ignore');
  6. const isPathNotFoundError = require('./isPathNotFoundError');
  7. const DEFAULT_IGNORE_FILENAME = '.stylelintignore';
  8. /**
  9. * @param {{ cwd: string, ignorePath?: string | string[], ignorePattern?: string[] }} options
  10. * @return {import('ignore').Ignore}
  11. */
  12. module.exports = function getFileIgnorer(options) {
  13. const ignorer = ignore();
  14. const ignorePaths = [options.ignorePath || []].flat();
  15. if (ignorePaths.length === 0) {
  16. ignorePaths.push(DEFAULT_IGNORE_FILENAME);
  17. }
  18. for (const ignoreFilePath of ignorePaths) {
  19. const absoluteIgnoreFilePath = path.isAbsolute(ignoreFilePath)
  20. ? ignoreFilePath
  21. : path.resolve(options.cwd, ignoreFilePath);
  22. try {
  23. const ignoreText = fs.readFileSync(absoluteIgnoreFilePath, 'utf8');
  24. ignorer.add(ignoreText);
  25. } catch (readError) {
  26. if (!isPathNotFoundError(readError)) {
  27. throw readError;
  28. }
  29. }
  30. }
  31. ignorer.add(options.ignorePattern || []);
  32. return ignorer;
  33. };