resolveConfig.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. 'use strict';
  2. const path = require('path');
  3. const createStylelint = require('./createStylelint');
  4. const getConfigForFile = require('./getConfigForFile');
  5. /**
  6. * Resolves the effective configuration for a given file. Resolves to `undefined`
  7. * if no config is found.
  8. * @param {string} filePath - The path to the file to get the config for.
  9. * @param {Pick<
  10. * import('stylelint').LinterOptions,
  11. * | 'cwd'
  12. * | 'config'
  13. * | 'configBasedir'
  14. * | 'configFile'
  15. * >} options - The options to use when creating the Stylelint instance.
  16. * @returns {Promise<import('stylelint').Config | undefined>}
  17. */
  18. module.exports = async function resolveConfig(
  19. filePath,
  20. { cwd = process.cwd(), config, configBasedir, configFile } = {},
  21. ) {
  22. if (!filePath) {
  23. return undefined;
  24. }
  25. const stylelint = createStylelint({
  26. config,
  27. configFile,
  28. configBasedir,
  29. cwd,
  30. });
  31. const absoluteFilePath = !path.isAbsolute(filePath)
  32. ? path.join(cwd, filePath)
  33. : path.normalize(filePath);
  34. const configSearchPath = stylelint._options.configFile || absoluteFilePath;
  35. const resolved = await getConfigForFile(stylelint, configSearchPath, absoluteFilePath);
  36. if (!resolved) {
  37. return undefined;
  38. }
  39. return resolved.config;
  40. };