blob: d602fbdd48090b1d97a4a7bbf9da1cd71052a649 [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 Lippe16b82282021-11-08 13:50:26 +000021module.exports = async function getConfigForFile(stylelint, searchPath = process.cwd(), filePath) {
Mathias Bynens79e2cf02020-05-29 16:46:17 +020022 const optionsConfig = stylelint._options.config;
23
24 if (optionsConfig !== undefined) {
Tim van der Lippe16b82282021-11-08 13:50:26 +000025 const cached = stylelint._specifiedConfigCache.get(optionsConfig);
Mathias Bynens79e2cf02020-05-29 16:46:17 +020026
Tim van der Lippe16b82282021-11-08 13:50:26 +000027 // If config has overrides the resulting config might be different for some files.
28 // Cache results only if resulted config is the same for all linted files.
29 if (cached && !optionsConfig.overrides) {
30 return cached;
31 }
Mathias Bynens79e2cf02020-05-29 16:46:17 +020032
Tim van der Lippe16b82282021-11-08 13:50:26 +000033 const augmentedResult = augmentConfigFull(stylelint, filePath, {
Mathias Bynens79e2cf02020-05-29 16:46:17 +020034 config: optionsConfig,
35 // Add the extra path part so that we can get the directory without being
36 // confused
37 filepath: path.join(process.cwd(), 'argument-config'),
38 });
39
40 stylelint._specifiedConfigCache.set(optionsConfig, augmentedResult);
41
42 return augmentedResult;
43 }
44
Tim van der Lippe16b82282021-11-08 13:50:26 +000045 const configExplorer = cosmiconfig('stylelint', {
46 transform: (cosmiconfigResult) => augmentConfigFull(stylelint, filePath, cosmiconfigResult),
47 stopDir: STOP_DIR,
48 });
Mathias Bynens79e2cf02020-05-29 16:46:17 +020049
Tim van der Lippe16b82282021-11-08 13:50:26 +000050 let config = stylelint._options.configFile
51 ? await configExplorer.load(stylelint._options.configFile)
52 : await configExplorer.search(searchPath);
Mathias Bynens79e2cf02020-05-29 16:46:17 +020053
Tim van der Lippe16b82282021-11-08 13:50:26 +000054 if (!config) {
55 config = await configExplorer.search(process.cwd());
56 }
Mathias Bynens79e2cf02020-05-29 16:46:17 +020057
Tim van der Lippe16b82282021-11-08 13:50:26 +000058 if (!config) {
59 return Promise.reject(
60 configurationError(`No configuration provided${searchPath ? ` for ${searchPath}` : ''}`),
61 );
62 }
Mathias Bynens79e2cf02020-05-29 16:46:17 +020063
Tim van der Lippe16b82282021-11-08 13:50:26 +000064 return config;
Mathias Bynens79e2cf02020-05-29 16:46:17 +020065};