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 | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame^] | 21 | module.exports = async function getConfigForFile(stylelint, searchPath = process.cwd(), filePath) { |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 22 | const optionsConfig = stylelint._options.config; |
| 23 | |
| 24 | if (optionsConfig !== undefined) { |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame^] | 25 | const cached = stylelint._specifiedConfigCache.get(optionsConfig); |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 26 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame^] | 27 | // 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 Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 32 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame^] | 33 | const augmentedResult = augmentConfigFull(stylelint, filePath, { |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 34 | 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 Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame^] | 45 | const configExplorer = cosmiconfig('stylelint', { |
| 46 | transform: (cosmiconfigResult) => augmentConfigFull(stylelint, filePath, cosmiconfigResult), |
| 47 | stopDir: STOP_DIR, |
| 48 | }); |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 49 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame^] | 50 | let config = stylelint._options.configFile |
| 51 | ? await configExplorer.load(stylelint._options.configFile) |
| 52 | : await configExplorer.search(searchPath); |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 53 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame^] | 54 | if (!config) { |
| 55 | config = await configExplorer.search(process.cwd()); |
| 56 | } |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 57 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame^] | 58 | if (!config) { |
| 59 | return Promise.reject( |
| 60 | configurationError(`No configuration provided${searchPath ? ` for ${searchPath}` : ''}`), |
| 61 | ); |
| 62 | } |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 63 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame^] | 64 | return config; |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 65 | }; |