1 | 'use strict';
|
2 |
|
3 | const configurationError = require('./utils/configurationError');
|
4 | const path = require('path');
|
5 | const { augmentConfigFull } = require('./augmentConfig');
|
6 | const { cosmiconfig } = require('cosmiconfig');
|
7 |
|
8 | const IS_TEST = process.env.NODE_ENV === 'test';
|
9 | const STOP_DIR = IS_TEST ? process.cwd() : undefined;
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | module.exports = async function getConfigForFile(
|
22 | stylelint,
|
23 | searchPath = stylelint._options.cwd,
|
24 | filePath,
|
25 | ) {
|
26 | const optionsConfig = stylelint._options.config;
|
27 | const cwd = stylelint._options.cwd;
|
28 |
|
29 | if (optionsConfig !== undefined) {
|
30 | const cached = stylelint._specifiedConfigCache.get(optionsConfig);
|
31 |
|
32 |
|
33 |
|
34 | if (cached && !optionsConfig.overrides) {
|
35 | return cached;
|
36 | }
|
37 |
|
38 | const augmentedResult = augmentConfigFull(stylelint, filePath, {
|
39 | config: optionsConfig,
|
40 |
|
41 |
|
42 | filepath: path.join(cwd, 'argument-config'),
|
43 | });
|
44 |
|
45 | stylelint._specifiedConfigCache.set(optionsConfig, augmentedResult);
|
46 |
|
47 | return augmentedResult;
|
48 | }
|
49 |
|
50 | const configExplorer = cosmiconfig('stylelint', {
|
51 | transform: (cosmiconfigResult) => augmentConfigFull(stylelint, filePath, cosmiconfigResult),
|
52 | stopDir: STOP_DIR,
|
53 | });
|
54 |
|
55 | let config = stylelint._options.configFile
|
56 | ? await configExplorer.load(stylelint._options.configFile)
|
57 | : await configExplorer.search(searchPath);
|
58 |
|
59 | if (!config) {
|
60 | config = await configExplorer.search(cwd);
|
61 | }
|
62 |
|
63 | if (!config) {
|
64 | return Promise.reject(
|
65 | configurationError(`No configuration provided${searchPath ? ` for ${searchPath}` : ''}`),
|
66 | );
|
67 | }
|
68 |
|
69 | return config;
|
70 | };
|