4
0

getConfigForFile.cjs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 path = require('node:path');
  5. const process = require('node:process');
  6. const cosmiconfig = require('cosmiconfig');
  7. const errors = require('./utils/errors.cjs');
  8. const augmentConfig = require('./augmentConfig.cjs');
  9. const IS_TEST = process.env.NODE_ENV === 'test';
  10. const STOP_DIR = IS_TEST ? process.cwd() : undefined;
  11. /** @import {CosmiconfigResult, InternalApi} from 'stylelint' */
  12. /**
  13. * Get a configuration by the following way:
  14. *
  15. * 1. If the `config` option is given, it's returned.
  16. * 2. If the `configFile` option is given, the file's config is returned.
  17. * 3. If the options above are not given, a config found in `searchPath` is returned.
  18. *
  19. * @param {Object} options
  20. * @param {InternalApi} options.stylelint
  21. * @param {string} [options.searchPath] - Defaults to `cwd`.
  22. * @param {string} [options.filePath] - For applying overrides.
  23. * @param {boolean} [options.failIfNoConfig=true] - Throws an error if a config is not found.
  24. * @returns {Promise<CosmiconfigResult>}
  25. */
  26. async function getConfigForFile({
  27. stylelint,
  28. searchPath = stylelint._options.cwd,
  29. filePath,
  30. failIfNoConfig = true,
  31. }) {
  32. const optionsConfig = stylelint._options.config;
  33. const cwd = stylelint._options.cwd;
  34. if (optionsConfig) {
  35. const filePathAsCacheKey = filePath ?? '';
  36. /** @type {Map<string, CosmiconfigResult>} */
  37. const cachedForFiles = stylelint._specifiedConfigCache.get(optionsConfig) ?? new Map();
  38. const cached = cachedForFiles.get(filePathAsCacheKey);
  39. if (cached) {
  40. return cached;
  41. }
  42. const augmentedResult = await augmentConfig.augmentConfigFull(stylelint, filePath, {
  43. config: optionsConfig,
  44. // Add the extra path part so that we can get the directory without being
  45. // confused
  46. filepath: path.join(cwd, 'argument-config'),
  47. });
  48. cachedForFiles.set(filePathAsCacheKey, augmentedResult);
  49. stylelint._specifiedConfigCache.set(optionsConfig, cachedForFiles);
  50. return augmentedResult;
  51. }
  52. const configExplorer = cosmiconfig.cosmiconfig('stylelint', {
  53. transform: (cosmiconfigResult) => augmentConfig.augmentConfigFull(stylelint, filePath, cosmiconfigResult),
  54. stopDir: STOP_DIR,
  55. searchStrategy: 'global', // for backward compatibility
  56. });
  57. const configFile = stylelint._options.configFile;
  58. let config = configFile
  59. ? await configExplorer.load(configFile)
  60. : await configExplorer.search(searchPath);
  61. if (!config) {
  62. config = await configExplorer.search(cwd);
  63. }
  64. if (!config && failIfNoConfig) {
  65. throw new errors.ConfigurationError(
  66. `No configuration provided${searchPath ? ` for ${searchPath}` : ''}`,
  67. );
  68. }
  69. return config;
  70. }
  71. module.exports = getConfigForFile;