Skip to content

Commit 72b3180

Browse files
authored
Merge pull request #19511 from storybookjs/feat/eslint-migration-json-support
CLI: support .json in eslint-plugin migration
2 parents 3390717 + 4f10f8a commit 72b3180

File tree

4 files changed

+33
-16
lines changed

4 files changed

+33
-16
lines changed

code/lib/cli/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"commander": "^6.2.1",
5656
"cross-spawn": "^7.0.3",
5757
"degit": "^2.8.4",
58+
"detect-indent": "^6.1.0",
5859
"envinfo": "^7.7.3",
5960
"execa": "^5.0.0",
6061
"express": "^4.17.1",

code/lib/cli/src/automigrate/fixes/eslint-plugin.ts

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import chalk from 'chalk';
22
import { dedent } from 'ts-dedent';
33
import { ConfigFile, readConfig, writeConfig } from '@storybook/csf-tools';
44
import { getStorybookInfo } from '@storybook/core-common';
5+
import { readFile, readJson, writeJson } from 'fs-extra';
6+
import detectIndent from 'detect-indent';
57

68
import { findEslintFile, SUPPORTED_ESLINT_EXTENSIONS } from '../helpers/getEslintInfo';
79

@@ -79,26 +81,39 @@ export const eslintPlugin: Fix<EslintPluginRunOptions> = {
7981
if (!dryRun) packageManager.addDependencies({ installAsDevDependencies: true }, deps);
8082

8183
if (!dryRun && unsupportedExtension) {
82-
throw new Error(dedent`
83-
⚠️ The plugin was successfuly installed but failed to configure.
84+
logger.info(dedent`
85+
⚠️ The plugin was successfully installed but failed to configure.
8486
85-
Found an .eslintrc config file with an unsupported automigration format: ${unsupportedExtension}.
86-
Supported formats for automigration are: ${SUPPORTED_ESLINT_EXTENSIONS.join(', ')}.
87+
Found an eslint config file with an unsupported automigration format: .eslintrc.${unsupportedExtension}.
88+
The supported formats for this automigration are: ${SUPPORTED_ESLINT_EXTENSIONS.join(
89+
', '
90+
)}.
8791
8892
Please refer to https://github.com/storybookjs/eslint-plugin-storybook#usage to finish setting up the plugin manually.
8993
`);
94+
return;
9095
}
9196

92-
const eslint = await readConfig(eslintFile);
93-
logger.info(`✅ Configuring eslint rules in ${eslint.fileName}`);
94-
97+
logger.info(`✅ Adding Storybook plugin to ${eslintFile}`);
9598
if (!dryRun) {
96-
logger.info(`✅ Adding Storybook to extends list`);
97-
const extendsConfig = eslint.getFieldValue(['extends']) || [];
98-
const existingConfigValue = Array.isArray(extendsConfig) ? extendsConfig : [extendsConfig];
99-
eslint.setFieldValue(['extends'], [...existingConfigValue, 'plugin:storybook/recommended']);
100-
101-
await writeConfig(eslint);
99+
if (eslintFile.endsWith('json')) {
100+
const eslintConfig = (await readJson(eslintFile)) as { extends?: string[] };
101+
const existingConfigValue = Array.isArray(eslintConfig.extends)
102+
? eslintConfig.extends
103+
: [eslintConfig.extends];
104+
eslintConfig.extends = [...(existingConfigValue || []), 'plugin:storybook/recommended'];
105+
106+
const eslintFileContents = await readFile(eslintFile, 'utf8');
107+
const spaces = detectIndent(eslintFileContents).amount || 2;
108+
await writeJson(eslintFile, eslintConfig, { spaces });
109+
} else {
110+
const eslint = await readConfig(eslintFile);
111+
const extendsConfig = eslint.getFieldValue(['extends']) || [];
112+
const existingConfigValue = Array.isArray(extendsConfig) ? extendsConfig : [extendsConfig];
113+
eslint.setFieldValue(['extends'], [...existingConfigValue, 'plugin:storybook/recommended']);
114+
115+
await writeConfig(eslint);
116+
}
102117
}
103118
},
104119
};

code/lib/cli/src/automigrate/helpers/getEslintInfo.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fse from 'fs-extra';
22

3-
export const SUPPORTED_ESLINT_EXTENSIONS = ['js', 'cjs'];
4-
const UNSUPPORTED_ESLINT_EXTENSIONS = ['yaml', 'yml', 'json'];
3+
export const SUPPORTED_ESLINT_EXTENSIONS = ['js', 'cjs', 'json'];
4+
const UNSUPPORTED_ESLINT_EXTENSIONS = ['yaml', 'yml'];
55

66
export const findEslintFile = () => {
77
const filePrefix = '.eslintrc';

code/yarn.lock

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7295,6 +7295,7 @@ __metadata:
72957295
commander: ^6.2.1
72967296
cross-spawn: ^7.0.3
72977297
degit: ^2.8.4
7298+
detect-indent: ^6.1.0
72987299
envinfo: ^7.7.3
72997300
execa: ^5.0.0
73007301
express: ^4.17.1
@@ -16699,7 +16700,7 @@ __metadata:
1669916700
languageName: node
1670016701
linkType: hard
1670116702

16702-
"detect-indent@npm:^6.0.0":
16703+
"detect-indent@npm:^6.0.0, detect-indent@npm:^6.1.0":
1670316704
version: 6.1.0
1670416705
resolution: "detect-indent@npm:6.1.0"
1670516706
checksum: dd83cdeda9af219cf77f5e9a0dc31d828c045337386cfb55ce04fad94ba872ee7957336834154f7647b89b899c3c7acc977c57a79b7c776b506240993f97acc7

0 commit comments

Comments
 (0)