fix(core): fix convert to monorepo eslint issue (#19713)

This commit is contained in:
Emily Xiong 2023-10-19 13:20:25 -04:00 committed by GitHub
parent 85bda9ab8a
commit 40a946a11d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 9 deletions

View File

@ -39,6 +39,8 @@ describe('@nx/workspace:convert-to-monorepo', () => {
expect(() => runCLI(`build ${reactApp}`)).not.toThrow();
expect(() => runCLI(`test ${reactApp}`)).not.toThrow();
expect(() => runCLI(`lint ${reactApp}`)).not.toThrow();
expect(() => runCLI(`lint e2e`)).not.toThrow();
expect(() => runCLI(`e2e e2e`)).not.toThrow();
});
});

View File

@ -82,15 +82,15 @@ export function updateRelativePathsInConfig(
const config = tree.read(configPath, 'utf-8');
tree.write(
configPath,
replaceFlatConfigPaths(config, sourcePath, offset, destinationPath)
replaceFlatConfigPaths(config, sourcePath, offset, destinationPath, tree)
);
} else {
updateJson(tree, configPath, (json) => {
if (typeof json.extends === 'string') {
json.extends = offsetFilePath(sourcePath, json.extends, offset);
json.extends = offsetFilePath(sourcePath, json.extends, offset, tree);
} else if (json.extends) {
json.extends = json.extends.map((extend: string) =>
offsetFilePath(sourcePath, extend, offset)
offsetFilePath(sourcePath, extend, offset, tree)
);
}
@ -114,7 +114,8 @@ function replaceFlatConfigPaths(
config: string,
sourceRoot: string,
offset: string,
destinationRoot: string
destinationRoot: string,
tree: Tree
): string {
let match;
let newConfig = config;
@ -122,7 +123,7 @@ function replaceFlatConfigPaths(
// replace requires
const requireRegex = RegExp(/require\(['"](.*)['"]\)/g);
while ((match = requireRegex.exec(newConfig)) !== null) {
const newPath = offsetFilePath(sourceRoot, match[1], offset);
const newPath = offsetFilePath(sourceRoot, match[1], offset, tree);
newConfig =
newConfig.slice(0, match.index) +
`require('${newPath}')` +
@ -143,8 +144,20 @@ function replaceFlatConfigPaths(
function offsetFilePath(
projectRoot: string,
pathToFile: string,
offset: string
offset: string,
tree: Tree
): string {
if (
eslintConfigFileWhitelist.some((eslintFile) =>
pathToFile.includes(eslintFile)
)
) {
// if the file is point to base eslint
const rootEslint = findEslintFile(tree);
if (rootEslint) {
return joinPathFragments(offset, rootEslint);
}
}
if (!pathToFile.startsWith('..')) {
// not a relative path
return pathToFile;

View File

@ -20,7 +20,7 @@ export async function monorepoGenerator(tree: Tree, options: {}) {
// Need to determine libs vs packages directory base on the type of root project.
for (const [, project] of projects) {
if (project.root === '.') rootProject = project;
projectsToMove.push(project);
projectsToMove.unshift(project); // move the root project 1st
}
// Currently, Nx only handles apps+libs or packages. You cannot mix and match them.

View File

@ -38,7 +38,7 @@ export function maybeExtractEslintConfigIfRootProject(
// Only need to handle migrating the root rootProject.
// If other libs/apps exist, then this migration is already done by `@nx/eslint:lint-rootProject` generator.
migrateConfigToMonorepoStyle?.(
[rootProject.name],
[rootProject],
tree,
tree.exists(joinPathFragments(rootProject.root, 'jest.config.ts')) ||
tree.exists(joinPathFragments(rootProject.root, 'jest.config.js'))

View File

@ -12,7 +12,12 @@ export function updateEslintConfig(
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')) {
if (
!tree.exists('.eslintrc.json') &&
!tree.exists('eslint.config.js') &&
!tree.exists('.eslintrc.base.json') &&
!tree.exists('eslint.base.config.js')
) {
return;
}
try {