Skip to content

Commit

Permalink
feat(core): change the signature of createNodes to return a project r…
Browse files Browse the repository at this point in the history
…oot map instead of project name map
  • Loading branch information
AgentEnder committed Nov 7, 2023
1 parent 0197444 commit ff76c60
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 37 deletions.
10 changes: 5 additions & 5 deletions docs/generated/devkit/CreateNodesFunction.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Type alias: CreateNodesFunction<T\>

Ƭ **CreateNodesFunction**<`T`\>: (`projectConfigurationFile`: `string`, `options`: `T` \| `undefined`, `context`: [`CreateNodesContext`](../../devkit/documents/CreateNodesContext)) => { `externalNodes?`: `Record`<`string`, [`ProjectGraphExternalNode`](../../devkit/documents/ProjectGraphExternalNode)\> ; `projects?`: `Record`<`string`, [`ProjectConfiguration`](../../devkit/documents/ProjectConfiguration)\> }
Ƭ **CreateNodesFunction**<`T`\>: (`projectConfigurationFile`: `string`, `options`: `T` \| `undefined`, `context`: [`CreateNodesContext`](../../devkit/documents/CreateNodesContext)) => { `externalNodes?`: `Record`<`string`, [`ProjectGraphExternalNode`](../../devkit/documents/ProjectGraphExternalNode)\> ; `projects?`: `Record`<`string`, `Optional`<[`ProjectConfiguration`](../../devkit/documents/ProjectConfiguration), `"root"`\>\> }

#### Type parameters

Expand All @@ -27,7 +27,7 @@ Used for creating nodes for the [ProjectGraph](../../devkit/documents/ProjectGra

`Object`

| Name | Type |
| :--------------- | :------------------------------------------------------------------------------------------------- |
| `externalNodes?` | `Record`<`string`, [`ProjectGraphExternalNode`](../../devkit/documents/ProjectGraphExternalNode)\> |
| `projects?` | `Record`<`string`, [`ProjectConfiguration`](../../devkit/documents/ProjectConfiguration)\> |
| Name | Type | Description |
| :--------------- | :---------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------------------------------- |
| `externalNodes?` | `Record`<`string`, [`ProjectGraphExternalNode`](../../devkit/documents/ProjectGraphExternalNode)\> | A map of external node name -> external node. External nodes do not have a root, so the key is their name. |
| `projects?` | `Record`<`string`, `Optional`<[`ProjectConfiguration`](../../devkit/documents/ProjectConfiguration), `"root"`\>\> | A map of project root -> project configuration |
12 changes: 5 additions & 7 deletions docs/shared/recipes/plugins/project-graph-plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ export const createNodes: CreateNodes = [
'**/project.json',
(projectConfigurationFile: string, opts, context: CreateNodesContext) => {
const projectConfiguration = readJson(projectConfigurationFile);
const projectRoot = dirname(projectConfigurationFile);
const projectName = projectConfiguration.name;
const root = dirname(projectConfigurationFile);
const name = projectConfiguration.name;

return {
projects: {
[projectName]: {
[root]: {
...projectConfiguration,
root: projectRoot,
root,
},
},
};
Expand Down Expand Up @@ -244,12 +244,10 @@ export const createNodes: CreateNodes<MyPluginOptions> = [
'**/project.json',
(fileName, opts, ctx) => {
const root = dirname(fileName);
const name = basename(fileName);

return {
projects: {
[name]: {
root,
[root]: {
tags: opts.tagName ? [opts.tagName] : [],
},
},
Expand Down
6 changes: 3 additions & 3 deletions packages/nx/plugins/package-json-workspaces.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('nx package.json workspaces plugin', () => {
.toMatchInlineSnapshot(`
{
"projects": {
"root": {
".": {
"name": "root",
"projectType": "library",
"root": ".",
Expand Down Expand Up @@ -68,7 +68,7 @@ describe('nx package.json workspaces plugin', () => {
.toMatchInlineSnapshot(`
{
"projects": {
"lib-a": {
"packages/lib-a": {
"name": "lib-a",
"projectType": "library",
"root": "packages/lib-a",
Expand Down Expand Up @@ -96,7 +96,7 @@ describe('nx package.json workspaces plugin', () => {
.toMatchInlineSnapshot(`
{
"projects": {
"lib-b": {
"packages/lib-b": {
"implicitDependencies": [
"lib-a",
],
Expand Down
11 changes: 6 additions & 5 deletions packages/nx/plugins/package-json-workspaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ export function getNxPackageJsonWorkspacesPlugin(root: string): NxPluginV2 {

export function createNodeFromPackageJson(pkgJsonPath: string, root: string) {
const json: PackageJson = readJsonFile(join(root, pkgJsonPath));
const project = buildProjectConfigurationFromPackageJson(
json,
pkgJsonPath,
readNxJson(root)
);
return {
projects: {
[json.name]: buildProjectConfigurationFromPackageJson(
json,
pkgJsonPath,
readNxJson(root)
),
[project.root]: project,
},
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('nx project.json plugin', () => {
.toMatchInlineSnapshot(`
{
"projects": {
"root": {
".": {
"name": "root",
"root": ".",
"targets": {
Expand All @@ -53,7 +53,7 @@ describe('nx project.json plugin', () => {
.toMatchInlineSnapshot(`
{
"projects": {
"lib-a": {
"packages/lib-a": {
"name": "lib-a",
"root": "packages/lib-a",
"targets": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ 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));
(file, _, { workspaceRoot }) => {
const json = readJsonFile<ProjectConfiguration>(
join(workspaceRoot, file)
);
const project = buildProjectFromProjectJson(json, file);
return {
projects: {
[project.name]: project,
[project.root]: project,
},
};
},
Expand Down
18 changes: 13 additions & 5 deletions packages/nx/src/project-graph/utils/project-configuration-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,19 @@ export function buildProjectsConfigurationsFromProjectPathsAndPlugins(
workspaceRoot: root,
});
for (const node in projectNodes) {
projectNodes[node].name ??= node;
mergeProjectConfigurationIntoRootMap(
projectRootMap,
projectNodes[node]
);
mergeProjectConfigurationIntoRootMap(projectRootMap, {
// If root is specified in config, that will overwrite this.
// Specifying it here though allows plugins to return something like
// {
// projects: {
// [root]: { targets: buildTargetsFromFile(f) }
// }
// }
// Otherwise, the root would have to be specified in the config as well
// which would be a bit redundant.
root: node,
...projectNodes[node],
});
}
Object.assign(externalNodes, pluginExternalNodes);
}
Expand Down
20 changes: 14 additions & 6 deletions packages/nx/src/utils/nx-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import {
} from '../adapter/angular-json';
import { getNxPackageJsonWorkspacesPlugin } from '../../plugins/package-json-workspaces';
import { CreateProjectJsonProjectsPlugin } from '../plugins/project-json/build-nodes/project-json';
import { FileMapCache } from '../project-graph/nx-deps-cache';
import { CreatePackageJsonProjectsNextToProjectJson } from '../plugins/project-json/build-nodes/package-json-next-to-project-json';

/**
Expand All @@ -60,7 +59,14 @@ export type CreateNodesFunction<T = unknown> = (
options: T | undefined,
context: CreateNodesContext
) => {
projects?: Record<string, ProjectConfiguration>;
/**
* A map of project root -> project configuration
*/
projects?: Record<string, Optional<ProjectConfiguration, 'root'>>;

/**
* A map of external node name -> external node. External nodes do not have a root, so the key is their name.
*/
externalNodes?: Record<string, ProjectGraphExternalNode>;
};

Expand Down Expand Up @@ -311,12 +317,12 @@ function ensurePluginIsV2(plugin: NxPlugin): NxPluginV2 {
createNodes: [
`*/**/${combineGlobPatterns(plugin.projectFilePatterns)}`,
(configFilePath) => {
const name = toProjectName(configFilePath);
const root = dirname(configFilePath);
return {
projects: {
[name]: {
name,
root: dirname(configFilePath),
[root]: {
name: toProjectName(configFilePath),
root,
targets: plugin.registerProjectTargets?.(configFilePath),
},
},
Expand Down Expand Up @@ -535,3 +541,5 @@ function getDefaultPluginsSync(root: string): LoadedNxPlugin[] {
plugin: p,
}));
}

type Optional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;

0 comments on commit ff76c60

Please sign in to comment.