Skip to content

Commit

Permalink
fix(core): fix convert to monorepo eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongemi committed Oct 18, 2023
1 parent 4f13f5a commit f1120e0
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 10 deletions.
2 changes: 2 additions & 0 deletions e2e/nx-misc/src/workspace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
Expand Down
28 changes: 21 additions & 7 deletions packages/eslint/src/generators/utils/eslint-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}

Expand All @@ -114,15 +114,16 @@ function replaceFlatConfigPaths(
config: string,
sourceRoot: string,
offset: string,
destinationRoot: string
destinationRoot: string,
tree: Tree
): string {
let match;
let newConfig = config;

// 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}')` +
Expand All @@ -143,12 +144,25 @@ function replaceFlatConfigPaths(
function offsetFilePath(
projectRoot: string,
pathToFile: string,
offset: string
offset: string,
tree: Tree
): string {
if (!pathToFile.startsWith('..')) {
if (!pathToFile.startsWith('..') && !pathToFile.startsWith('./')) {
// not a relative path
return pathToFile;
}
if (
eslintConfigFileWhitelist.some((eslintFile) =>
pathToFile.includes(eslintFile)
) &&
!tree.exists(joinPathFragments(projectRoot, pathToFile))
) {
// if the file is point to eslint
const rootEslint = findEslintFile(tree);
if (rootEslint) {
return joinPathFragments(offset, rootEslint);
}
}
return joinPathFragments(offset, projectRoot, pathToFile);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit f1120e0

Please sign in to comment.