Skip to content

Commit

Permalink
feat(linter): add migration for executor and package
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav committed Oct 10, 2023
1 parent c10d4ab commit ed377ef
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
await replaceNrwlPackageWithNxPackage(tree, '@nrwl/linter', '@nx/eslint');
await replaceNrwlPackageWithNxPackage(tree, '@nrwl/linter', '@nx/linter');

await formatFiles(tree);
}
Original file line number Diff line number Diff line change
@@ -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');
});
});
Original file line number Diff line number Diff line change
@@ -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<void> {
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);
}
}
}

0 comments on commit ed377ef

Please sign in to comment.