Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: 3 additions & 9 deletions docs/shared/recipes/plugins/project-graph-plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,11 @@ 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);

return {
projects: {
[projectName]: {
...projectConfiguration,
root: projectRoot,
},
[root]: projectConfiguration,
},
};
},
Expand Down Expand Up @@ -244,12 +240,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
3 changes: 2 additions & 1 deletion e2e/plugin/src/nx-plugin.fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ export const createNodes: CreateNodes<PluginOptions> = [

return {
projects: {
[name]: {
[root]: {
root,
name,
targets: {
build: {
executor: "nx:run-commands",
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
53 changes: 33 additions & 20 deletions packages/nx/src/adapter/angular-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,48 +56,61 @@ function readAngularJson(angularCliWorkspaceRoot: string) {
}

export function toNewFormat(w: any): ProjectsConfigurations {
Object.values(w.projects || {}).forEach((projectConfig: any) => {
if (!w.projects) {
return w;
}
for (const name in w.projects ?? {}) {
const projectConfig = w.projects[name];
if (projectConfig.architect) {
renamePropertyWithStableKeys(projectConfig, 'architect', 'targets');
}
if (projectConfig.schematics) {
renamePropertyWithStableKeys(projectConfig, 'schematics', 'generators');
}
if (!projectConfig.name) {
projectConfig.name = name;
}

Object.values(projectConfig.targets || {}).forEach((target: any) => {
if (target.builder !== undefined) {
renamePropertyWithStableKeys(target, 'builder', 'executor');
}
});
});
}

if (w.schematics) {
renamePropertyWithStableKeys(w, 'schematics', 'generators');
}
if (w.version !== 2) {
w.version = 2;
}

return w;
}

export function toOldFormat(w: any) {
Object.values(w.projects || {}).forEach((projectConfig: any) => {
if (typeof projectConfig === 'string') {
throw new Error(
"'project.json' files are incompatible with version 1 workspace schemas."
);
}
if (projectConfig.targets) {
renamePropertyWithStableKeys(projectConfig, 'targets', 'architect');
}
if (projectConfig.generators) {
renamePropertyWithStableKeys(projectConfig, 'generators', 'schematics');
}
delete projectConfig.name;
Object.values(projectConfig.architect || {}).forEach((target: any) => {
if (target.executor !== undefined) {
renamePropertyWithStableKeys(target, 'executor', 'builder');
if (w.projects) {
for (const name in w.projects) {
const projectConfig = w.projects[name];
if (typeof projectConfig === 'string') {
throw new Error(
"'project.json' files are incompatible with version 1 workspace schemas."
);
}
});
});
if (projectConfig.targets) {
renamePropertyWithStableKeys(projectConfig, 'targets', 'architect');
}
if (projectConfig.generators) {
renamePropertyWithStableKeys(projectConfig, 'generators', 'schematics');
}
delete projectConfig.name;
Object.values(projectConfig.architect || {}).forEach((target: any) => {
if (target.executor !== undefined) {
renamePropertyWithStableKeys(target, 'executor', 'builder');
}
});
}
}

if (w.generators) {
renamePropertyWithStableKeys(w, 'generators', 'schematics');
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
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>>;
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,10 @@ export const createNodes: CreateNodes<EslintPluginOptions> = [
}

options = normalizeOptions(options);
const projectName = basename(projectRoot);

return {
projects: {
[projectName]: {
[projectRoot]: {
root: projectRoot,
projectType: 'library',
targets: buildEslintTargets(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ export const createNodes: CreateNodes<<%= className %>PluginOptions> = [
}

options = normalizeOptions(options);
const projectName = basename(projectRoot);

return {
projects: {
[projectName]: {
[projectRoot]: {
root: projectRoot,
projectType: 'library',
targets: build<%= className %>Targets(
Expand Down