Tim van der Lippe | 4cb0974 | 2022-01-07 14:25:03 +0100 | [diff] [blame^] | 1 | 'use strict'; |
| 2 | |
| 3 | const createStylelint = require('./createStylelint'); |
| 4 | const path = require('path'); |
| 5 | |
| 6 | /** |
| 7 | * Resolves the effective configuation for a given file. Resolves to `undefined` |
| 8 | * if no config is found. |
| 9 | * @param {string} filePath - The path to the file to get the config for. |
| 10 | * @param {Pick< |
| 11 | * import('stylelint').LinterOptions, |
| 12 | * | 'cwd' |
| 13 | * | 'config' |
| 14 | * | 'configBasedir' |
| 15 | * | 'configFile' |
| 16 | * >} [options] - The options to use when creating the Stylelint instance. |
| 17 | * @returns {Promise<import('stylelint').Config | undefined>} |
| 18 | */ |
| 19 | module.exports = async function resolveConfig( |
| 20 | filePath, |
| 21 | { cwd = process.cwd(), config, configBasedir, configFile } = {}, |
| 22 | ) { |
| 23 | if (!filePath) { |
| 24 | return undefined; |
| 25 | } |
| 26 | |
| 27 | const stylelint = createStylelint({ |
| 28 | config, |
| 29 | configFile, |
| 30 | configBasedir, |
| 31 | cwd, |
| 32 | }); |
| 33 | |
| 34 | const absoluteFilePath = !path.isAbsolute(filePath) |
| 35 | ? path.join(cwd, filePath) |
| 36 | : path.normalize(filePath); |
| 37 | |
| 38 | const configSearchPath = stylelint._options.configFile || absoluteFilePath; |
| 39 | |
| 40 | const resolved = await stylelint.getConfigForFile(configSearchPath, absoluteFilePath); |
| 41 | |
| 42 | if (!resolved) { |
| 43 | return undefined; |
| 44 | } |
| 45 | |
| 46 | return resolved.config; |
| 47 | }; |