getFileIgnorer.js 929 B

12345678910111213141516171819202122232425262728293031323334
  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, ignorePattern?: string[] }} options
  10. * @return {import('ignore').Ignore}
  11. */
  12. module.exports = function getFileIgnorer(options) {
  13. const ignoreFilePath = options.ignorePath || DEFAULT_IGNORE_FILENAME;
  14. const absoluteIgnoreFilePath = path.isAbsolute(ignoreFilePath)
  15. ? ignoreFilePath
  16. : path.resolve(options.cwd, ignoreFilePath);
  17. let ignoreText = '';
  18. try {
  19. ignoreText = fs.readFileSync(absoluteIgnoreFilePath, 'utf8');
  20. } catch (readError) {
  21. if (!isPathNotFoundError(readError)) {
  22. throw readError;
  23. }
  24. }
  25. return ignore()
  26. .add(ignoreText)
  27. .add(options.ignorePattern || []);
  28. };