diff --git a/packages/storybook/src/generators/configuration/lib/util-functions.ts b/packages/storybook/src/generators/configuration/lib/util-functions.ts index cf572c1f4fdf4e..2791a47d1c7cad 100644 --- a/packages/storybook/src/generators/configuration/lib/util-functions.ts +++ b/packages/storybook/src/generators/configuration/lib/util-functions.ts @@ -29,6 +29,7 @@ import { StorybookConfigureSchema } from '../schema'; import { UiFramework7 } from '../../../utils/models'; import { nxVersion } from '../../../utils/versions'; import { findEslintFile } from '@nx/linter/src/generators/utils/eslint-file'; +import { useFlatConfig } from '@nx/linter/src/utils/flat-config'; const DEFAULT_PORT = 4400; @@ -382,12 +383,38 @@ export function updateLintConfig(tree: Tree, schema: StorybookConfigureSchema) { ]); }); - if (!findEslintFile(tree)) { + const eslintFile = findEslintFile(tree, root); + if (!eslintFile) { return; } - if (tree.exists(join(root, '.eslintrc.json'))) { - updateJson(tree, join(root, '.eslintrc.json'), (json) => { + const parserConfigPath = join( + root, + schema.uiFramework === '@storybook/angular' + ? '.storybook/tsconfig.json' + : 'tsconfig.storybook.json' + ); + + if (useFlatConfig()) { + let config = tree.read(eslintFile, 'utf-8'); + const projectRegex = RegExp(/project:\s?\[?['"](.*)['"]\]?/g); + let match; + while ((match = projectRegex.exec(config)) !== null) { + const matchSet = new Set( + match[1].split(',').map((p) => p.trim().replace(/['"]/g, '')) + ); + matchSet.add(parserConfigPath); + const insert = `project: [${Array.from(matchSet) + .map((p) => `'${p}'`) + .join(', ')}]`; + config = + config.slice(0, match.index) + + insert + + config.slice(match.index + match[0].length); + } + tree.write(eslintFile, config); + } else { + updateJson(tree, join(root, eslintFile), (json) => { if (typeof json.parserOptions?.project === 'string') { json.parserOptions.project = [json.parserOptions.project]; } @@ -395,9 +422,7 @@ export function updateLintConfig(tree: Tree, schema: StorybookConfigureSchema) { if (Array.isArray(json.parserOptions?.project)) { json.parserOptions.project = dedupe([ ...json.parserOptions.project, - schema.uiFramework === '@storybook/angular' - ? join(root, '.storybook/tsconfig.json') - : join(root, 'tsconfig.storybook.json'), + parserConfigPath, ]); } @@ -409,9 +434,7 @@ export function updateLintConfig(tree: Tree, schema: StorybookConfigureSchema) { if (Array.isArray(o.parserOptions?.project)) { o.parserOptions.project = dedupe([ ...o.parserOptions.project, - schema.uiFramework === '@storybook/angular' - ? join(root, '.storybook/tsconfig.json') - : join(root, 'tsconfig.storybook.json'), + parserConfigPath, ]); } }