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 { ConfigFile, readConfig, writeConfig } from '@storybook/csf-tools';
|
||||||
import { getStorybookInfo } from '@storybook/core-common';
|
import { getStorybookInfo } from '@storybook/core-common';
|
||||||
|
|
||||||
|
import { readJson, writeJson } from 'fs-extra';
|
||||||
import { findEslintFile, SUPPORTED_ESLINT_EXTENSIONS } from '../helpers/getEslintInfo';
|
import { findEslintFile, SUPPORTED_ESLINT_EXTENSIONS } from '../helpers/getEslintInfo';
|
||||||
|
|
||||||
import type { Fix } from '../types';
|
import type { Fix } from '../types';
|
||||||
@ -89,16 +90,23 @@ export const eslintPlugin: Fix<EslintPluginRunOptions> = {
|
|||||||
`);
|
`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const eslint = await readConfig(eslintFile);
|
logger.info(`✅ Adding Storybook plugin to ${eslintFile}`);
|
||||||
logger.info(`✅ Configuring eslint rules in ${eslint.fileName}`);
|
|
||||||
|
|
||||||
if (!dryRun) {
|
if (!dryRun) {
|
||||||
logger.info(`✅ Adding Storybook to extends list`);
|
if (eslintFile.endsWith('json')) {
|
||||||
const extendsConfig = eslint.getFieldValue(['extends']) || [];
|
const eslintConfig = (await readJson(eslintFile)) as { extends?: string[] };
|
||||||
const existingConfigValue = Array.isArray(extendsConfig) ? extendsConfig : [extendsConfig];
|
const existingConfigValue = Array.isArray(eslintConfig.extends)
|
||||||
eslint.setFieldValue(['extends'], [...existingConfigValue, 'plugin:storybook/recommended']);
|
? 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);
|
await writeConfig(eslint);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import fse from 'fs-extra';
|
import fse from 'fs-extra';
|
||||||
|
|
||||||
export const SUPPORTED_ESLINT_EXTENSIONS = ['js', 'cjs'];
|
export const SUPPORTED_ESLINT_EXTENSIONS = ['js', 'cjs', 'json'];
|
||||||
const UNSUPPORTED_ESLINT_EXTENSIONS = ['yaml', 'yml', 'json'];
|
const UNSUPPORTED_ESLINT_EXTENSIONS = ['yaml', 'yml'];
|
||||||
|
|
||||||
export const findEslintFile = () => {
|
export const findEslintFile = () => {
|
||||||
const filePrefix = '.eslintrc';
|
const filePrefix = '.eslintrc';
|
||||||
|
Loading…
x
Reference in New Issue
Block a user