nx/packages/workspace/src/generators/move/lib/update-eslint-config.ts
Nicholas Cunningham dec21662b6
feat(core): Add ESM support for Eslint config file (#29613)
This pull request includes changes to migrate ESLint configuration files
from CommonJS (`.cjs`) to ECMAScript modules (`.mjs`) as the default.

### ESLint Configuration Generation Changes

The changes also ensure consistent generated eslint configs based on the
base eslint config.
- If the workspace root has an `eslint.config.cjs` or `eslint.config.js`
with `module.exports`. When you create a library or application it will
generate an accompanying config at path
`{projectRoot}/eslint.config.cjs` of the same format.
- If the workspace root has an `eslint.config.mjs` or
`eslint.config.mjs` with `export default`. When you create a library or
application it will generate an accompanying config at path
`{projectRoot}/eslint.config.mjs`.
- If no eslint config is found at the workspace root one will be created
`eslint.config.mjs`
2025-01-17 13:39:45 -05:00

46 lines
1.3 KiB
TypeScript

import { output, ProjectConfiguration, Tree } from '@nx/devkit';
import { NormalizedSchema } from '../schema';
/**
* Update the .eslintrc file of the project if it exists.
*
* @param schema The options provided to the schematic
*/
export function updateEslintConfig(
tree: Tree,
schema: NormalizedSchema,
project: ProjectConfiguration
) {
// if there is no suitable eslint config, we don't need to do anything
if (
!tree.exists('.eslintrc.json') &&
!tree.exists('eslint.config.js') &&
!tree.exists('eslint.config.cjs') &&
!tree.exists('eslint.config.mjs') &&
!tree.exists('.eslintrc.base.json') &&
!tree.exists('eslint.base.config.js') &&
!tree.exists('eslint.base.config.cjs') &&
!tree.exists('eslint.base.config.mjs')
) {
return;
}
try {
const {
updateRelativePathsInConfig,
// nx-ignore-next-line
} = require('@nx/eslint/src/generators/utils/eslint-file');
updateRelativePathsInConfig(
tree,
project.root,
schema.relativeToRootDestination
);
} catch {
output.warn({
title: `Could not update the eslint config file.`,
bodyLines: [
'The @nx/eslint package could not be loaded. Please update the paths in eslint config manually.',
],
});
}
}