blob: 1d843df103810e1b53dd2cc27f78ca9a98378381 [file] [log] [blame]
Mathias Bynens79e2cf02020-05-29 16:46:17 +02001'use strict';
2
Mathias Bynens79e2cf02020-05-29 16:46:17 +02003const configurationError = require('./utils/configurationError');
4const path = require('path');
Tim van der Lippe16b82282021-11-08 13:50:26 +00005const { augmentConfigFull } = require('./augmentConfig');
6const { cosmiconfig } = require('cosmiconfig');
Mathias Bynens79e2cf02020-05-29 16:46:17 +02007
Tim van der Lippe16b82282021-11-08 13:50:26 +00008const IS_TEST = process.env.NODE_ENV === 'test';
9const STOP_DIR = IS_TEST ? process.cwd() : undefined;
10
11/** @typedef {import('stylelint').InternalApi} StylelintInternalApi */
12/** @typedef {import('stylelint').Config} StylelintConfig */
13/** @typedef {import('stylelint').CosmiconfigResult} StylelintCosmiconfigResult */
Mathias Bynens79e2cf02020-05-29 16:46:17 +020014
15/**
Tim van der Lippe16b82282021-11-08 13:50:26 +000016 * @param {StylelintInternalApi} stylelint
Mathias Bynens79e2cf02020-05-29 16:46:17 +020017 * @param {string} [searchPath]
Tim van der Lippe16b82282021-11-08 13:50:26 +000018 * @param {string} [filePath]
19 * @returns {Promise<StylelintCosmiconfigResult>}
Mathias Bynens79e2cf02020-05-29 16:46:17 +020020 */
Tim van der Lippe4cb09742022-01-07 14:25:03 +010021module.exports = async function getConfigForFile(
22 stylelint,
23 searchPath = stylelint._options.cwd,
24 filePath,
25) {
Mathias Bynens79e2cf02020-05-29 16:46:17 +020026 const optionsConfig = stylelint._options.config;
Tim van der Lippe4cb09742022-01-07 14:25:03 +010027 const cwd = stylelint._options.cwd;
Mathias Bynens79e2cf02020-05-29 16:46:17 +020028
29 if (optionsConfig !== undefined) {
Tim van der Lippe16b82282021-11-08 13:50:26 +000030 const cached = stylelint._specifiedConfigCache.get(optionsConfig);
Mathias Bynens79e2cf02020-05-29 16:46:17 +020031
Tim van der Lippe16b82282021-11-08 13:50:26 +000032 // If config has overrides the resulting config might be different for some files.
33 // Cache results only if resulted config is the same for all linted files.
34 if (cached && !optionsConfig.overrides) {
35 return cached;
36 }
Mathias Bynens79e2cf02020-05-29 16:46:17 +020037
Tim van der Lippe16b82282021-11-08 13:50:26 +000038 const augmentedResult = augmentConfigFull(stylelint, filePath, {
Mathias Bynens79e2cf02020-05-29 16:46:17 +020039 config: optionsConfig,
40 // Add the extra path part so that we can get the directory without being
41 // confused
Tim van der Lippe4cb09742022-01-07 14:25:03 +010042 filepath: path.join(cwd, 'argument-config'),
Mathias Bynens79e2cf02020-05-29 16:46:17 +020043 });
44
45 stylelint._specifiedConfigCache.set(optionsConfig, augmentedResult);
46
47 return augmentedResult;
48 }
49
Tim van der Lippe16b82282021-11-08 13:50:26 +000050 const configExplorer = cosmiconfig('stylelint', {
51 transform: (cosmiconfigResult) => augmentConfigFull(stylelint, filePath, cosmiconfigResult),
52 stopDir: STOP_DIR,
53 });
Mathias Bynens79e2cf02020-05-29 16:46:17 +020054
Tim van der Lippe16b82282021-11-08 13:50:26 +000055 let config = stylelint._options.configFile
56 ? await configExplorer.load(stylelint._options.configFile)
57 : await configExplorer.search(searchPath);
Mathias Bynens79e2cf02020-05-29 16:46:17 +020058
Tim van der Lippe16b82282021-11-08 13:50:26 +000059 if (!config) {
Tim van der Lippe4cb09742022-01-07 14:25:03 +010060 config = await configExplorer.search(cwd);
Tim van der Lippe16b82282021-11-08 13:50:26 +000061 }
Mathias Bynens79e2cf02020-05-29 16:46:17 +020062
Tim van der Lippe16b82282021-11-08 13:50:26 +000063 if (!config) {
64 return Promise.reject(
65 configurationError(`No configuration provided${searchPath ? ` for ${searchPath}` : ''}`),
66 );
67 }
Mathias Bynens79e2cf02020-05-29 16:46:17 +020068
Tim van der Lippe16b82282021-11-08 13:50:26 +000069 return config;
Mathias Bynens79e2cf02020-05-29 16:46:17 +020070};