Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame^] | 1 | 'use strict'; |
| 2 | |
| 3 | const _ = require('lodash'); |
| 4 | const createStylelint = require('./createStylelint'); |
| 5 | const globby = require('globby'); |
| 6 | const path = require('path'); |
| 7 | |
| 8 | /** @typedef {import('stylelint').StylelintConfig} StylelintConfig */ |
| 9 | |
| 10 | /** |
| 11 | * @param {import('stylelint').StylelintStandaloneOptions} options |
| 12 | * @returns {Promise<StylelintConfig | null>} |
| 13 | */ |
| 14 | module.exports = function (options) { |
| 15 | const code = options.code; |
| 16 | const config = options.config; |
| 17 | const configBasedir = options.configBasedir; |
| 18 | const configFile = options.configFile; |
| 19 | const configOverrides = options.configOverrides; |
| 20 | const globbyOptions = options.globbyOptions; |
| 21 | const files = options.files; |
| 22 | |
| 23 | const isCodeNotFile = code !== undefined; |
| 24 | |
| 25 | if (!files || files.length !== 1 || isCodeNotFile) { |
| 26 | return Promise.reject( |
| 27 | new Error('The --print-config option must be used with exactly one file path.'), |
| 28 | ); |
| 29 | } |
| 30 | |
| 31 | const filePath = files[0]; |
| 32 | |
| 33 | if (globby.hasMagic(filePath)) { |
| 34 | return Promise.reject(new Error('The --print-config option does not support globs.')); |
| 35 | } |
| 36 | |
| 37 | const stylelint = createStylelint({ |
| 38 | config, |
| 39 | configFile, |
| 40 | configBasedir, |
| 41 | configOverrides, |
| 42 | }); |
| 43 | |
| 44 | const cwd = _.get(globbyOptions, 'cwd', process.cwd()); |
| 45 | const absoluteFilePath = !path.isAbsolute(filePath) |
| 46 | ? path.join(cwd, filePath) |
| 47 | : path.normalize(filePath); |
| 48 | |
| 49 | const configSearchPath = stylelint._options.configFile || absoluteFilePath; |
| 50 | |
| 51 | return stylelint.getConfigForFile(configSearchPath).then((result) => { |
| 52 | if (result === null) { |
| 53 | return result; |
| 54 | } |
| 55 | |
| 56 | return result.config; |
| 57 | }); |
| 58 | }; |