getFileIgnorer.cjs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 node_fs = require('node:fs');
  5. const node_path = require('node:path');
  6. const ignore = require('ignore');
  7. const constants = require('../constants.cjs');
  8. /**
  9. * @typedef {import('stylelint').LinterOptions} LinterOptions
  10. *
  11. * @param {Pick<LinterOptions, 'ignorePath' | 'ignorePattern'> & { cwd: string }} options
  12. * @returns {import('ignore').Ignore}
  13. */
  14. function getFileIgnorer({ ignorePath, ignorePattern, cwd }) {
  15. const ignorer = ignore();
  16. const ignorePaths = [ignorePath || []].flat();
  17. if (ignorePaths.length === 0) {
  18. ignorePaths.push(constants.DEFAULT_IGNORE_FILENAME);
  19. }
  20. for (const ignoreFilePath of ignorePaths) {
  21. const absoluteIgnoreFilePath = node_path.isAbsolute(ignoreFilePath)
  22. ? ignoreFilePath
  23. : node_path.resolve(cwd, ignoreFilePath);
  24. if (!node_fs.existsSync(absoluteIgnoreFilePath)) continue;
  25. const ignoreText = node_fs.readFileSync(absoluteIgnoreFilePath, {
  26. // utf must remain lowercased to hit the fast path
  27. // see nodejs/node#49888
  28. encoding: 'utf8',
  29. flag: 'r',
  30. });
  31. ignorer.add(ignoreText);
  32. }
  33. if (ignorePattern) ignorer.add(ignorePattern);
  34. return ignorer;
  35. }
  36. module.exports = getFileIgnorer;