mirror of
https://github.com/react/react-native-devtools-frontend.git
synced 2026-09-30 01:07:01 +08:00
Stylelint v16 deprecated CJS so I moved our lint script to ESM, and fixed all the deprecation warning. The only place I needed to silence them were tests, as we import CJS there. Bug: none Change-Id: Iae4d2df76c855a9738b774a2d035f22509770215 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6138610 Commit-Queue: Nikolay Vitkov <nvitkov@chromium.org> Reviewed-by: Benedikt Meurer <bmeurer@chromium.org>
61 lines
1.7 KiB
JavaScript
61 lines
1.7 KiB
JavaScript
import { EXIT_CODE_INVALID_CONFIG } from '../constants.mjs';
|
|
import dynamicImport from './dynamicImport.mjs';
|
|
import { existsSync } from 'node:fs';
|
|
import formatters from '../formatters/index.mjs';
|
|
import getConfigForFile from '../getConfigForFile.mjs';
|
|
import getFormatterOptionsText from './getFormatterOptionsText.mjs';
|
|
import { resolve } from 'node:path';
|
|
|
|
/** @import {Formatter, InternalApi} from 'stylelint' */
|
|
|
|
/**
|
|
* @param {InternalApi} stylelint
|
|
* @returns {Promise<Formatter>}
|
|
*/
|
|
export default async function getFormatter(stylelint) {
|
|
const cwd = stylelint._options.cwd;
|
|
const configPath = stylelint._options.configFile || cwd;
|
|
let formatter = stylelint._options.formatter;
|
|
|
|
if (!formatter) {
|
|
let configForFile;
|
|
|
|
try {
|
|
configForFile = await getConfigForFile(stylelint, configPath);
|
|
} catch (err) {
|
|
if (err instanceof Error && 'code' in err && err.code === EXIT_CODE_INVALID_CONFIG) {
|
|
configForFile = undefined;
|
|
} else {
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
formatter = configForFile?.config.formatter;
|
|
}
|
|
|
|
if (typeof formatter === 'string') {
|
|
let formatterFunction = formatters[formatter];
|
|
|
|
if (formatterFunction === undefined) {
|
|
if (existsSync(formatter)) {
|
|
formatterFunction = await dynamicImport(resolve(formatter)).then((m) => m.default);
|
|
} else {
|
|
const formattersText = getFormatterOptionsText(', ', '"');
|
|
|
|
throw new Error(`You must use a valid formatter option: ${formattersText} or a function`);
|
|
}
|
|
}
|
|
|
|
return formatterFunction;
|
|
}
|
|
|
|
// Assume a function or a promise of a function.
|
|
if (typeof formatter === 'function' || formatter) {
|
|
return Promise.resolve(formatter);
|
|
}
|
|
|
|
formatter ??= stylelint._options._defaultFormatter ?? 'json';
|
|
|
|
return formatters[formatter];
|
|
}
|