mirror of
https://github.com/storybookjs/storybook.git
synced 2025-04-01 05:05:25 +08:00
CLI: support .json in eslint plugin migration
This commit is contained in:
parent
77638cd959
commit
e313b6ae0a
@ -3,6 +3,7 @@ import { dedent } from 'ts-dedent';
|
||||
import { ConfigFile, readConfig, writeConfig } from '@storybook/csf-tools';
|
||||
import { getStorybookInfo } from '@storybook/core-common';
|
||||
|
||||
import { readJson, writeJson } from 'fs-extra';
|
||||
import { findEslintFile, SUPPORTED_ESLINT_EXTENSIONS } from '../helpers/getEslintInfo';
|
||||
|
||||
import type { Fix } from '../types';
|
||||
@ -89,16 +90,23 @@ export const eslintPlugin: Fix<EslintPluginRunOptions> = {
|
||||
`);
|
||||
}
|
||||
|
||||
const eslint = await readConfig(eslintFile);
|
||||
logger.info(`✅ Configuring eslint rules in ${eslint.fileName}`);
|
||||
|
||||
logger.info(`✅ Adding Storybook plugin to ${eslintFile}`);
|
||||
if (!dryRun) {
|
||||
logger.info(`✅ Adding Storybook to extends list`);
|
||||
if (eslintFile.endsWith('json')) {
|
||||
const eslintConfig = (await readJson(eslintFile)) as { extends?: string[] };
|
||||
const existingConfigValue = Array.isArray(eslintConfig.extends)
|
||||
? eslintConfig.extends
|
||||
: [eslintConfig.extends];
|
||||
eslintConfig.extends = [...(existingConfigValue || []), 'plugin:storybook/recommended'];
|
||||
await writeJson(eslintFile, eslintConfig, { spaces: 2 });
|
||||
} else {
|
||||
const eslint = await readConfig(eslintFile);
|
||||
const extendsConfig = eslint.getFieldValue(['extends']) || [];
|
||||
const existingConfigValue = Array.isArray(extendsConfig) ? extendsConfig : [extendsConfig];
|
||||
eslint.setFieldValue(['extends'], [...existingConfigValue, 'plugin:storybook/recommended']);
|
||||
|
||||
await writeConfig(eslint);
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
|
@ -1,7 +1,7 @@
|
||||
import fse from 'fs-extra';
|
||||
|
||||
export const SUPPORTED_ESLINT_EXTENSIONS = ['js', 'cjs'];
|
||||
const UNSUPPORTED_ESLINT_EXTENSIONS = ['yaml', 'yml', 'json'];
|
||||
export const SUPPORTED_ESLINT_EXTENSIONS = ['js', 'cjs', 'json'];
|
||||
const UNSUPPORTED_ESLINT_EXTENSIONS = ['yaml', 'yml'];
|
||||
|
||||
export const findEslintFile = () => {
|
||||
const filePrefix = '.eslintrc';
|
||||
|
Loading…
x
Reference in New Issue
Block a user