Skip to content

Commit

Permalink
chore(core): move project-json plugin to src (#18803)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz authored Aug 23, 2023
1 parent 165250e commit 72fc94b
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 39 deletions.
2 changes: 1 addition & 1 deletion packages/nx/src/generators/utils/project-configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
buildProjectConfigurationFromPackageJson,
getGlobPatternsFromPackageManagerWorkspaces,
} from '../../../plugins/package-json-workspaces';
import { buildProjectFromProjectJson } from '../../../plugins/project-json';
import { buildProjectFromProjectJson } from '../../plugins/project-json/build-nodes/project-json';
import { renamePropertyWithStableKeys } from '../../adapter/angular-json';
import {
ProjectConfiguration,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
import * as memfs from 'memfs';

import '../src/utils/testing/mock-fs';
import '../../../utils/testing/mock-fs';

import { PackageJson } from '../src/utils/package-json';
import { PackageJson } from '../../../utils/package-json';

import {
getNxProjectJsonPlugin,
CreateProjectJsonProjectsPlugin,
mergeNpmScriptsWithTargets,
} from './project-json';
import { CreateNodesContext } from '../../../utils/nx-plugin';
const { createNodes } = CreateProjectJsonProjectsPlugin;

describe('nx project.json plugin', () => {
let context: CreateNodesContext;
beforeEach(() => {
context = {
nxJsonConfiguration: {},
workspaceRoot: '/root',
};
});

it('should build projects from project.json', () => {
memfs.vol.fromJSON(
{
Expand Down Expand Up @@ -37,8 +47,7 @@ describe('nx project.json plugin', () => {
'/root'
);

const plugin = getNxProjectJsonPlugin('/root');
expect(plugin.createNodes[1]('project.json', null)).toMatchInlineSnapshot(`
expect(createNodes[1]('project.json', context)).toMatchInlineSnapshot(`
{
"projects": {
"root": {
Expand All @@ -51,7 +60,7 @@ describe('nx project.json plugin', () => {
},
}
`);
expect(plugin.createNodes[1]('packages/lib-a/project.json', null))
expect(createNodes[1]('packages/lib-a/project.json', context))
.toMatchInlineSnapshot(`
{
"projects": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,32 @@ import { existsSync } from 'node:fs';
import {
ProjectConfiguration,
TargetConfiguration,
} from '../src/config/workspace-json-project-json';
import { toProjectName } from '../src/config/workspaces';
import { readJsonFile } from '../src/utils/fileutils';
import { NxPluginV2 } from '../src/utils/nx-plugin';
} from '../../../config/workspace-json-project-json';
import { toProjectName } from '../../../config/workspaces';
import { readJsonFile } from '../../../utils/fileutils';
import { NxPluginV2 } from '../../../utils/nx-plugin';
import {
PackageJson,
readTargetsFromPackageJson,
} from '../src/utils/package-json';
} from '../../../utils/package-json';

export function getNxProjectJsonPlugin(root: string): NxPluginV2 {
return {
name: 'nx-core-build-project-json-nodes',
createNodes: [
'{project.json,**/project.json}',
(file) => {
const json = readJsonFile<ProjectConfiguration>(join(root, file));
const project = buildProjectFromProjectJson(json, file);
mergePackageJsonConfigurationWithProjectJson(project, root);
return {
projects: {
[project.name]: project,
},
};
},
],
};
}
export const CreateProjectJsonProjectsPlugin: NxPluginV2 = {
name: 'nx-core-build-project-json-nodes',
createNodes: [
'{project.json,**/project.json}',
(file, context) => {
const root = context.workspaceRoot;
const json = readJsonFile<ProjectConfiguration>(join(root, file));
const project = buildProjectFromProjectJson(json, file);
mergePackageJsonConfigurationWithProjectJson(project, root);
return {
projects: {
[project.name]: project,
},
};
},
],
};

export function buildProjectFromProjectJson(
json: Partial<ProjectConfiguration>,
Expand Down
1 change: 0 additions & 1 deletion packages/nx/src/project-graph/build-nodes/index.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/nx/src/project-graph/build-project-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
writeCache,
} from './nx-deps-cache';
import { buildImplicitProjectDependencies } from './build-dependencies';
import { normalizeProjectNodes } from './build-nodes';
import { normalizeProjectNodes } from './utils/normalize-project-nodes';
import { isNxPluginV1, isNxPluginV2, loadNxPlugins } from '../utils/nx-plugin';
import { getRootTsConfigPath } from '../plugins/js/utils/typescript';
import {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ProjectGraphProjectNode } from '../../config/project-graph';
import {
normalizeImplicitDependencies,
normalizeProjectTargets,
} from './workspace-projects';
} from './normalize-project-nodes';

describe('workspace-projects', () => {
let projectGraph: Record<string, ProjectGraphProjectNode> = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { join } from 'path';
import { workspaceRoot } from '../../utils/workspace-root';
import {
ProjectGraphProcessorContext,
ProjectGraphProjectNode,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { basename } from 'node:path';

import { getNxPackageJsonWorkspacesPlugin } from '../../../plugins/package-json-workspaces';
import { getNxProjectJsonPlugin } from '../../../plugins/project-json';
import { CreateProjectJsonProjectsPlugin } from '../../plugins/project-json/build-nodes/project-json';
import { NxJsonConfiguration, TargetDefaults } from '../../config/nx-json';
import { ProjectGraphExternalNode } from '../../config/project-graph';
import {
Expand Down Expand Up @@ -93,19 +93,19 @@ export function buildProjectsConfigurationsFromProjectPathsAndPlugins(
// We push the nx core node builder onto the end, s.t. it overwrites any user specified behavior
plugins.push(
getNxPackageJsonWorkspacesPlugin(root),
getNxProjectJsonPlugin(root)
CreateProjectJsonProjectsPlugin
);

// We iterate over plugins first - this ensures that plugins specified first take precedence.
for (const plugin of plugins) {
const [pattern, configurationConstructor] = plugin.createNodes ?? [];
const [pattern, createNodes] = plugin.createNodes ?? [];
if (!pattern) {
continue;
}
for (const file of projectFiles) {
if (minimatch(file, pattern, { dot: true })) {
const { projects: projectNodes, externalNodes: pluginExternalNodes } =
configurationConstructor(file, {
createNodes(file, {
nxJsonConfiguration: nxJson,
workspaceRoot: root,
});
Expand Down

1 comment on commit 72fc94b

@vercel
Copy link

@vercel vercel bot commented on 72fc94b Aug 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-nrwl.vercel.app
nx-five.vercel.app
nx.dev
nx-dev-git-master-nrwl.vercel.app

Please sign in to comment.