Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 1 | 'use strict'; |
| 2 | |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 3 | const configurationError = require('./utils/configurationError'); |
| 4 | const path = require('path'); |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 5 | const { augmentConfigFull } = require('./augmentConfig'); |
| 6 | const { cosmiconfig } = require('cosmiconfig'); |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 7 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 8 | const IS_TEST = process.env.NODE_ENV === 'test'; |
| 9 | const 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 Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 14 | |
| 15 | /** |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 16 | * @param {StylelintInternalApi} stylelint |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 17 | * @param {string} [searchPath] |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 18 | * @param {string} [filePath] |
| 19 | * @returns {Promise<StylelintCosmiconfigResult>} |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 20 | */ |
Tim van der Lippe | 4cb0974 | 2022-01-07 14:25:03 +0100 | [diff] [blame^] | 21 | module.exports = async function getConfigForFile( |
| 22 | stylelint, |
| 23 | searchPath = stylelint._options.cwd, |
| 24 | filePath, |
| 25 | ) { |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 26 | const optionsConfig = stylelint._options.config; |
Tim van der Lippe | 4cb0974 | 2022-01-07 14:25:03 +0100 | [diff] [blame^] | 27 | const cwd = stylelint._options.cwd; |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 28 | |
| 29 | if (optionsConfig !== undefined) { |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 30 | const cached = stylelint._specifiedConfigCache.get(optionsConfig); |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 31 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 32 | // 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 Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 37 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 38 | const augmentedResult = augmentConfigFull(stylelint, filePath, { |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 39 | config: optionsConfig, |
| 40 | // Add the extra path part so that we can get the directory without being |
| 41 | // confused |
Tim van der Lippe | 4cb0974 | 2022-01-07 14:25:03 +0100 | [diff] [blame^] | 42 | filepath: path.join(cwd, 'argument-config'), |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 43 | }); |
| 44 | |
| 45 | stylelint._specifiedConfigCache.set(optionsConfig, augmentedResult); |
| 46 | |
| 47 | return augmentedResult; |
| 48 | } |
| 49 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 50 | const configExplorer = cosmiconfig('stylelint', { |
| 51 | transform: (cosmiconfigResult) => augmentConfigFull(stylelint, filePath, cosmiconfigResult), |
| 52 | stopDir: STOP_DIR, |
| 53 | }); |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 54 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 55 | let config = stylelint._options.configFile |
| 56 | ? await configExplorer.load(stylelint._options.configFile) |
| 57 | : await configExplorer.search(searchPath); |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 58 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 59 | if (!config) { |
Tim van der Lippe | 4cb0974 | 2022-01-07 14:25:03 +0100 | [diff] [blame^] | 60 | config = await configExplorer.search(cwd); |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 61 | } |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 62 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 63 | if (!config) { |
| 64 | return Promise.reject( |
| 65 | configurationError(`No configuration provided${searchPath ? ` for ${searchPath}` : ''}`), |
| 66 | ); |
| 67 | } |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 68 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 69 | return config; |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 70 | }; |