Skip to content

Commit

Permalink
feat(linter): migrate to create-nodes-v2 (#26302)
Browse files Browse the repository at this point in the history
<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

## Current Behavior
No cache for eslint nodes

## Expected Behavior
v2 cache for eslint nodes

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #

---------

Co-authored-by: FrozenPandaz <[email protected]>
  • Loading branch information
AgentEnder and FrozenPandaz authored Jun 4, 2024
1 parent cf0142d commit 0594deb
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
type TargetConfiguration,
type Tree,
} from '@nx/devkit';
import { createNodes, EslintPluginOptions } from '../../plugins/plugin';
import { migrateExecutorToPluginV1 } from '@nx/devkit/src/generators/plugin-migrations/executor-to-plugin-migrator';
import { createNodesV2, EslintPluginOptions } from '../../plugins/plugin';
import { migrateExecutorToPlugin } from '@nx/devkit/src/generators/plugin-migrations/executor-to-plugin-migrator';
import { targetOptionsToCliMap } from './lib/target-options-map';
import { interpolate } from 'nx/src/tasks-runner/utils';

Expand All @@ -19,26 +19,26 @@ export async function convertToInferred(tree: Tree, options: Schema) {
const projectGraph = await createProjectGraphAsync();

const migratedProjectsModern =
await migrateExecutorToPluginV1<EslintPluginOptions>(
await migrateExecutorToPlugin<EslintPluginOptions>(
tree,
projectGraph,
'@nx/eslint:lint',
'@nx/eslint/plugin',
(targetName) => ({ targetName }),
postTargetTransformer,
createNodes,
createNodesV2,
options.project
);

const migratedProjectsLegacy =
await migrateExecutorToPluginV1<EslintPluginOptions>(
await migrateExecutorToPlugin<EslintPluginOptions>(
tree,
projectGraph,
'@nrwl/linter:eslint',
'@nx/eslint/plugin',
(targetName) => ({ targetName }),
postTargetTransformer,
createNodes,
createNodesV2,
options.project
);

Expand Down
12 changes: 6 additions & 6 deletions packages/eslint/src/generators/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import {
Tree,
updateNxJson,
} from '@nx/devkit';
import { addPluginV1 } from '@nx/devkit/src/utils/add-plugin';
import { addPlugin } from '@nx/devkit/src/utils/add-plugin';
import { eslintVersion, nxVersion } from '../../utils/versions';
import { findEslintFile } from '../utils/eslint-file';
import { createNodes } from '../../plugins/plugin';
import { createNodesV2 } from '../../plugins/plugin';
import { hasEslintPlugin } from '../utils/plugin';

export interface LinterInitOptions {
Expand Down Expand Up @@ -73,11 +73,11 @@ export async function initEsLint(
];

if (rootEslintFile && options.addPlugin && !hasPlugin) {
await addPluginV1(
await addPlugin(
tree,
graph,
'@nx/eslint/plugin',
createNodes,
createNodesV2,
{
targetName: lintTargetNames,
},
Expand All @@ -94,11 +94,11 @@ export async function initEsLint(
updateProductionFileset(tree);

if (options.addPlugin) {
await addPluginV1(
await addPlugin(
tree,
graph,
'@nx/eslint/plugin',
createNodes,
createNodesV2,
{
targetName: lintTargetNames,
},
Expand Down
59 changes: 35 additions & 24 deletions packages/eslint/src/plugins/plugin.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import { CreateNodesContext } from '@nx/devkit';
import { CreateNodesContextV2 } from '@nx/devkit';
import { minimatch } from 'minimatch';
import { TempFs } from 'nx/src/internal-testing-utils/temp-fs';
import { createNodes } from './plugin';
import { createNodesV2 } from './plugin';
import { mkdirSync, rmdirSync } from 'fs';

jest.mock('nx/src/utils/cache-directory', () => ({
...jest.requireActual('nx/src/utils/cache-directory'),
projectGraphCacheDirectory: 'tmp/project-graph-cache',
}));

describe('@nx/eslint/plugin', () => {
let context: CreateNodesContext;
let context: CreateNodesContextV2;
let tempFs: TempFs;
let configFiles: string[] = [];

beforeEach(async () => {
mkdirSync('tmp/project-graph-cache', { recursive: true });
tempFs = new TempFs('eslint-plugin');
context = {
nxJsonConfiguration: {
Expand All @@ -24,14 +32,14 @@ describe('@nx/eslint/plugin', () => {
},
},
workspaceRoot: tempFs.tempDir,
configFiles: [],
};
});

afterEach(() => {
jest.resetModules();
tempFs.cleanup();
tempFs = null;
rmdirSync('tmp/project-graph-cache', { recursive: true });
});

it('should not create any nodes when there are no eslint configs', async () => {
Expand Down Expand Up @@ -617,27 +625,30 @@ describe('@nx/eslint/plugin', () => {

function createFiles(fileSys: Record<string, string>) {
tempFs.createFilesSync(fileSys);
// @ts-expect-error update otherwise readonly property for testing
context.configFiles = getMatchingFiles(Object.keys(fileSys));
configFiles = getMatchingFiles(Object.keys(fileSys));
}
});

function getMatchingFiles(allConfigFiles: string[]): string[] {
return allConfigFiles.filter((file) =>
minimatch(file, createNodes[0], { dot: true })
);
}
function getMatchingFiles(allConfigFiles: string[]): string[] {
return allConfigFiles.filter((file) =>
minimatch(file, createNodesV2[0], { dot: true })
);
}

async function invokeCreateNodesOnMatchingFiles(
context: CreateNodesContext,
targetName: string
) {
const aggregateProjects: Record<string, any> = {};
for (const file of context.configFiles) {
const nodes = await createNodes[1](file, { targetName }, context);
Object.assign(aggregateProjects, nodes.projects);
async function invokeCreateNodesOnMatchingFiles(
context: CreateNodesContextV2,
targetName: string
) {
const aggregateProjects: Record<string, any> = {};
const results = await createNodesV2[1](
configFiles,
{ targetName },
context
);
for (const [, nodes] of results) {
Object.assign(aggregateProjects, nodes.projects);
}
return {
projects: aggregateProjects,
};
}
return {
projects: aggregateProjects,
};
}
});
Loading

0 comments on commit 0594deb

Please sign in to comment.