nx/packages/react-native/src/utils/add-linting.ts
Emily Xiong b4aadccac8
fix(react-native): fix react native storybook for lib (#29210)
<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes https://github.com/nrwl/nx/issues/28802
2024-12-11 15:01:17 -05:00

83 lines
2.2 KiB
TypeScript

import { Linter, LinterType, lintProjectGenerator } from '@nx/eslint';
import {
addDependenciesToPackageJson,
GeneratorCallback,
runTasksInSerial,
Tree,
} from '@nx/devkit';
import { extraEslintDependencies } from '@nx/react/src/utils/lint';
import {
addExtendsToLintConfig,
addIgnoresToLintConfig,
addOverrideToLintConfig,
addPredefinedConfigToFlatLintConfig,
isEslintConfigSupported,
} from '@nx/eslint/src/generators/utils/eslint-file';
import { useFlatConfig } from '@nx/eslint/src/utils/flat-config';
interface NormalizedSchema {
linter?: Linter | LinterType;
projectName: string;
projectRoot: string;
setParserOptionsProject?: boolean;
tsConfigPaths: string[];
skipPackageJson?: boolean;
addPlugin?: boolean;
}
export async function addLinting(host: Tree, options: NormalizedSchema) {
if (options.linter === Linter.None) {
return () => {};
}
const tasks: GeneratorCallback[] = [];
const lintTask = await lintProjectGenerator(host, {
linter: options.linter,
project: options.projectName,
tsConfigPaths: options.tsConfigPaths,
skipFormat: true,
skipPackageJson: options.skipPackageJson,
setParserOptionsProject: options.setParserOptionsProject,
addPlugin: options.addPlugin,
});
tasks.push(lintTask);
if (isEslintConfigSupported(host)) {
if (useFlatConfig(host)) {
addPredefinedConfigToFlatLintConfig(
host,
options.projectRoot,
'flat/react'
);
// Add an empty rules object to users know how to add/override rules
addOverrideToLintConfig(host, options.projectRoot, {
files: ['*.ts', '*.tsx', '*.js', '*.jsx'],
rules: {},
});
} else {
const addExtendsTask = addExtendsToLintConfig(host, options.projectRoot, {
name: 'plugin:@nx/react',
needCompatFixup: true,
});
tasks.push(addExtendsTask);
}
addIgnoresToLintConfig(host, options.projectRoot, [
'public',
'.cache',
'node_modules',
]);
}
if (!options.skipPackageJson) {
const installTask = addDependenciesToPackageJson(
host,
extraEslintDependencies.dependencies,
extraEslintDependencies.devDependencies
);
tasks.push(installTask);
}
return runTasksInSerial(...tasks);
}