resolveConfig.js 1.1 KB

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