@@ -2,6 +2,8 @@ import chalk from 'chalk';
22import { dedent } from 'ts-dedent' ;
33import { ConfigFile , readConfig , writeConfig } from '@storybook/csf-tools' ;
44import { getStorybookInfo } from '@storybook/core-common' ;
5+ import { readFile , readJson , writeJson } from 'fs-extra' ;
6+ import detectIndent from 'detect-indent' ;
57
68import { 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} ;
0 commit comments