Skip to content

Commit

Permalink
fix(linter): add support for flat config in cypress
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav committed Aug 10, 2023
1 parent f722b21 commit 9723e93
Showing 1 changed file with 32 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -382,22 +383,46 @@ 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];
}

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,
]);
}

Expand All @@ -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,
]);
}
}
Expand Down

0 comments on commit 9723e93

Please sign in to comment.