diff --git a/packages/eslint/src/migrations/update-16-0-0-add-nx-packages/update-16-0-0-add-nx-packages.spec.ts b/packages/eslint/src/migrations/update-16-0-0-add-nx-packages/update-16-0-0-add-nx-packages.spec.ts index 9fb6348ba61d7a..1c66d74de0c4e8 100644 --- a/packages/eslint/src/migrations/update-16-0-0-add-nx-packages/update-16-0-0-add-nx-packages.spec.ts +++ b/packages/eslint/src/migrations/update-16-0-0-add-nx-packages/update-16-0-0-add-nx-packages.spec.ts @@ -24,13 +24,13 @@ describe('update-16-0-0-add-nx-packages', () => { ).not.toBeDefined(); }); - it('should add a dependency on @nx/eslint', async () => { + it('should add a dependency on @nx/linter', async () => { await replacePackage(tree); const packageJson = readJson(tree, 'package.json'); const newDependencyVersion = - packageJson.devDependencies['@nx/eslint'] ?? - packageJson.dependencies['@nx/eslint']; + packageJson.devDependencies['@nx/linter'] ?? + packageJson.dependencies['@nx/linter']; expect(newDependencyVersion).toBeDefined(); }); diff --git a/packages/eslint/src/migrations/update-16-0-0-add-nx-packages/update-16-0-0-add-nx-packages.ts b/packages/eslint/src/migrations/update-16-0-0-add-nx-packages/update-16-0-0-add-nx-packages.ts index 7ffac6b10a0d3c..318e1816ffba8c 100644 --- a/packages/eslint/src/migrations/update-16-0-0-add-nx-packages/update-16-0-0-add-nx-packages.ts +++ b/packages/eslint/src/migrations/update-16-0-0-add-nx-packages/update-16-0-0-add-nx-packages.ts @@ -2,7 +2,7 @@ import { Tree, formatFiles } from '@nx/devkit'; import { replaceNrwlPackageWithNxPackage } from '@nx/devkit/src/utils/replace-package'; export default async function replacePackage(tree: Tree): Promise { - await replaceNrwlPackageWithNxPackage(tree, '@nrwl/linter', '@nx/eslint'); + await replaceNrwlPackageWithNxPackage(tree, '@nrwl/linter', '@nx/linter'); await formatFiles(tree); } diff --git a/packages/eslint/src/migrations/update-17-0-0-rename-to-eslint/update-17-0-0-rename-to-eslint.spec.ts b/packages/eslint/src/migrations/update-17-0-0-rename-to-eslint/update-17-0-0-rename-to-eslint.spec.ts new file mode 100644 index 00000000000000..b3a05ec241b244 --- /dev/null +++ b/packages/eslint/src/migrations/update-17-0-0-rename-to-eslint/update-17-0-0-rename-to-eslint.spec.ts @@ -0,0 +1,75 @@ +import { + Tree, + addProjectConfiguration, + readJson, + updateJson, +} from '@nx/devkit'; +import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; +import replacePackage from './update-17-0-0-rename-to-eslint'; + +describe('update-17-0-0-rename-to-eslint', () => { + let tree: Tree; + beforeEach(() => { + tree = createTreeWithEmptyWorkspace(); + + updateJson(tree, 'package.json', (json) => { + json.devDependencies['@nx/linter'] = '17.0.0'; + return json; + }); + + updateJson(tree, 'nx.json', (json) => { + json.targetDefaults = { + lint: { + executor: '@nx/linter:eslint', + }, + }; + return json; + }); + }); + + it('should remove the dependency on @nx/linter', async () => { + await replacePackage(tree); + + expect( + readJson(tree, 'package.json').dependencies['@nx/linter'] + ).not.toBeDefined(); + expect( + readJson(tree, 'package.json').devDependencies['@nx/linter'] + ).not.toBeDefined(); + }); + + it('should add a dependency on @nx/eslint', async () => { + await replacePackage(tree); + + const packageJson = readJson(tree, 'package.json'); + const newDependencyVersion = + packageJson.devDependencies['@nx/eslint'] ?? + packageJson.dependencies['@nx/eslint']; + + expect(newDependencyVersion).toBeDefined(); + }); + + it('should update the targetDefaults', async () => { + await replacePackage(tree); + + const nxJson = readJson(tree, 'nx.json'); + expect(nxJson.targetDefaults.lint.executor).toEqual('@nx/eslint:lint'); + }); + + it('should update the target executor', async () => { + addProjectConfiguration(tree, 'test-lib', { + root: 'libs/test-lib', + projectType: 'library', + targets: { + lint: { + executor: '@nx/linter:eslint', + }, + }, + }); + + await replacePackage(tree); + + const projJson = readJson(tree, 'libs/test-lib/project.json'); + expect(projJson.targets.lint.executor).toEqual('@nx/eslint:lint'); + }); +}); diff --git a/packages/eslint/src/migrations/update-17-0-0-rename-to-eslint/update-17-0-0-rename-to-eslint.ts b/packages/eslint/src/migrations/update-17-0-0-rename-to-eslint/update-17-0-0-rename-to-eslint.ts new file mode 100644 index 00000000000000..b9dce789ba830d --- /dev/null +++ b/packages/eslint/src/migrations/update-17-0-0-rename-to-eslint/update-17-0-0-rename-to-eslint.ts @@ -0,0 +1,66 @@ +import { + NxJsonConfiguration, + Tree, + formatFiles, + getProjects, + readNxJson, + updateNxJson, + updateProjectConfiguration, +} from '@nx/devkit'; +import { replaceNrwlPackageWithNxPackage } from '@nx/devkit/src/utils/replace-package'; + +export default async function replacePackage(tree: Tree): Promise { + await replaceNrwlPackageWithNxPackage(tree, '@nx/linter', '@nx/eslint'); + + // executor name change from :eslint to :lint + updateNxJsonExecutor(tree); + updateProjectExecutor(tree); + + await formatFiles(tree); +} + +function updateNxJsonExecutor(tree: Tree) { + if (!tree.exists('nx.json')) { + return; + } + + const nxJson: NxJsonConfiguration = readNxJson(tree); + let needsUpdate = false; + + for (const [targetName, targetConfig] of Object.entries( + nxJson.targetDefaults ?? {} + )) { + if (targetConfig.executor !== '@nx/eslint:eslint') { + continue; + } + needsUpdate = true; + nxJson.targetDefaults[targetName].executor = '@nx/eslint:lint'; + } + + if (needsUpdate) { + updateNxJson(tree, nxJson); + } +} + +function updateProjectExecutor(tree: Tree) { + const projects = getProjects(tree); + + for (const [projectName, projectConfiguration] of projects) { + let needsUpdate = false; + + for (const [targetName, targetConfig] of Object.entries( + projectConfiguration.targets ?? {} + )) { + if (targetConfig.executor !== '@nx/eslint:eslint') { + continue; + } + + needsUpdate = true; + projectConfiguration.targets[targetName].executor = '@nx/eslint:lint'; + } + + if (needsUpdate) { + updateProjectConfiguration(tree, projectName, projectConfiguration); + } + } +}