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