Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI: support .json in eslint-plugin migration #19511

Merged
merged 7 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions code/lib/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"commander": "^6.2.1",
"cross-spawn": "^7.0.3",
"degit": "^2.8.4",
"detect-indent": "^6.1.0",
"envinfo": "^7.7.3",
"execa": "^5.0.0",
"express": "^4.17.1",
Expand Down
41 changes: 28 additions & 13 deletions code/lib/cli/src/automigrate/fixes/eslint-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import chalk from 'chalk';
import { dedent } from 'ts-dedent';
import { ConfigFile, readConfig, writeConfig } from '@storybook/csf-tools';
import { getStorybookInfo } from '@storybook/core-common';
import { readFile, readJson, writeJson } from 'fs-extra';
import detectIndent from 'detect-indent';

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

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

if (!dryRun && unsupportedExtension) {
throw new Error(dedent`
⚠️ The plugin was successfuly installed but failed to configure.
logger.info(dedent`
⚠️ The plugin was successfully installed but failed to configure.

Found an .eslintrc config file with an unsupported automigration format: ${unsupportedExtension}.
Supported formats for automigration are: ${SUPPORTED_ESLINT_EXTENSIONS.join(', ')}.
Found an eslint config file with an unsupported automigration format: .eslintrc.${unsupportedExtension}.
The supported formats for this automigration are: ${SUPPORTED_ESLINT_EXTENSIONS.join(
', '
)}.

Please refer to https://github.com/storybookjs/eslint-plugin-storybook#usage to finish setting up the plugin manually.
`);
return;
}

const eslint = await readConfig(eslintFile);
logger.info(`✅ Configuring eslint rules in ${eslint.fileName}`);

logger.info(`✅ Adding Storybook plugin to ${eslintFile}`);
yannbf marked this conversation as resolved.
Show resolved Hide resolved
if (!dryRun) {
logger.info(`✅ Adding Storybook to extends list`);
const extendsConfig = eslint.getFieldValue(['extends']) || [];
const existingConfigValue = Array.isArray(extendsConfig) ? extendsConfig : [extendsConfig];
eslint.setFieldValue(['extends'], [...existingConfigValue, 'plugin:storybook/recommended']);

await writeConfig(eslint);
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'];

const eslintFileContents = await readFile(eslintFile, 'utf8');
const spaces = detectIndent(eslintFileContents).amount || 2;
await writeJson(eslintFile, eslintConfig, { spaces });
} 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);
}
}
},
};
4 changes: 2 additions & 2 deletions code/lib/cli/src/automigrate/helpers/getEslintInfo.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
3 changes: 2 additions & 1 deletion code/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7318,6 +7318,7 @@ __metadata:
commander: ^6.2.1
cross-spawn: ^7.0.3
degit: ^2.8.4
detect-indent: ^6.1.0
envinfo: ^7.7.3
execa: ^5.0.0
express: ^4.17.1
Expand Down Expand Up @@ -16755,7 +16756,7 @@ __metadata:
languageName: node
linkType: hard

"detect-indent@npm:^6.0.0":
"detect-indent@npm:^6.0.0, detect-indent@npm:^6.1.0":
version: 6.1.0
resolution: "detect-indent@npm:6.1.0"
checksum: dd83cdeda9af219cf77f5e9a0dc31d828c045337386cfb55ce04fad94ba872ee7957336834154f7647b89b899c3c7acc977c57a79b7c776b506240993f97acc7
Expand Down