UNPKG

1.16 kBJavaScriptView Raw
1'use strict';
2
3const createStylelint = require('./createStylelint');
4const 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 */
19module.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};