blob: ad25bdf9b6a0c0f6a7db4be2ab236ed101e138d9 [file] [log] [blame]
Mathias Bynens79e2cf02020-05-29 16:46:17 +02001'use strict';
2
Mathias Bynens79e2cf02020-05-29 16:46:17 +02003const createStylelint = require('./createStylelint');
4const globby = require('globby');
5const path = require('path');
6
Tim van der Lippe16b82282021-11-08 13:50:26 +00007/** @typedef {import('stylelint').Config} StylelintConfig */
Mathias Bynens79e2cf02020-05-29 16:46:17 +02008
9/**
Tim van der Lippe16b82282021-11-08 13:50:26 +000010 * @param {import('stylelint').LinterOptions} options
Mathias Bynens79e2cf02020-05-29 16:46:17 +020011 * @returns {Promise<StylelintConfig | null>}
12 */
Tim van der Lippe16b82282021-11-08 13:50:26 +000013module.exports = function printConfig({
14 code,
15 config,
16 configBasedir,
17 configFile,
18 globbyOptions,
19 files,
20}) {
Mathias Bynens79e2cf02020-05-29 16:46:17 +020021 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 Bynens79e2cf02020-05-29 16:46:17 +020039 });
40
Tim van der Lippe16b82282021-11-08 13:50:26 +000041 const cwd = (globbyOptions && globbyOptions.cwd) || process.cwd();
Mathias Bynens79e2cf02020-05-29 16:46:17 +020042 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 Lippe16b82282021-11-08 13:50:26 +000048 return stylelint.getConfigForFile(configSearchPath, absoluteFilePath).then((result) => {
Mathias Bynens79e2cf02020-05-29 16:46:17 +020049 if (result === null) {
50 return result;
51 }
52
53 return result.config;
54 });
55};