UNPKG

1.83 kBJavaScriptView Raw
1/* @flow */
2"use strict";
3
4const augmentConfigFull = require("./augmentConfig").augmentConfigFull;
5const configurationError = require("./utils/configurationError");
6const path = require("path");
7
8/*:: type configPromise = Promise<?{ config: stylelint$config, filepath: string }>*/
9
10module.exports = function(
11 stylelint /*: stylelint$internalApi*/,
12 searchPath /*:: ?: string*/
13) /*: configPromise*/ {
14 searchPath = searchPath || process.cwd();
15
16 const optionsConfig = stylelint._options.config;
17
18 if (optionsConfig !== undefined) {
19 const cached /*: configPromise*/ = stylelint._specifiedConfigCache.get(
20 optionsConfig
21 );
22
23 if (cached) return cached;
24
25 // stylelint._fullExplorer (cosmiconfig) is already configured to
26 // run augmentConfigFull; but since we're making up the result here,
27 // we need to manually run the transform
28 const augmentedResult = augmentConfigFull(stylelint, {
29 config: optionsConfig,
30 // Add the extra path part so that we can get the directory without being
31 // confused
32 filepath: path.join(process.cwd(), "argument-config")
33 });
34
35 stylelint._specifiedConfigCache.set(optionsConfig, augmentedResult);
36
37 return augmentedResult;
38 }
39
40 const searchForConfig = stylelint._options.configFile
41 ? stylelint._fullExplorer.load(stylelint._options.configFile)
42 : stylelint._fullExplorer.search(searchPath);
43
44 return searchForConfig
45 .then(config => {
46 // If no config was found, try looking from process.cwd
47 if (!config) return stylelint._fullExplorer.search(process.cwd());
48
49 return config;
50 })
51 .then(config => {
52 if (!config) {
53 const ending = searchPath ? ` for ${searchPath}` : "";
54
55 throw configurationError(`No configuration provided${ending}`);
56 }
57
58 return config;
59 });
60};