feat(core): change the signature of createNodes to return a project root map instead of project name map (#20102)
This commit is contained in:
parent
c7d0d21761
commit
08a4891494
@ -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
|
||||
|
||||
@ -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 |
|
||||
|
||||
@ -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,
|
||||
},
|
||||
};
|
||||
},
|
||||
@ -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] : [],
|
||||
},
|
||||
},
|
||||
|
||||
@ -37,8 +37,9 @@ export const createNodes: CreateNodes<PluginOptions> = [
|
||||
|
||||
return {
|
||||
projects: {
|
||||
[name]: {
|
||||
[root]: {
|
||||
root,
|
||||
name,
|
||||
targets: {
|
||||
build: {
|
||||
executor: "nx:run-commands",
|
||||
|
||||
@ -40,7 +40,7 @@ describe('nx package.json workspaces plugin', () => {
|
||||
.toMatchInlineSnapshot(`
|
||||
{
|
||||
"projects": {
|
||||
"root": {
|
||||
".": {
|
||||
"name": "root",
|
||||
"projectType": "library",
|
||||
"root": ".",
|
||||
@ -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",
|
||||
@ -96,7 +96,7 @@ describe('nx package.json workspaces plugin', () => {
|
||||
.toMatchInlineSnapshot(`
|
||||
{
|
||||
"projects": {
|
||||
"lib-b": {
|
||||
"packages/lib-b": {
|
||||
"implicitDependencies": [
|
||||
"lib-a",
|
||||
],
|
||||
|
||||
@ -30,13 +30,14 @@ export function getNxPackageJsonWorkspacesPlugin(root: string): NxPluginV2 {
|
||||
|
||||
export function createNodeFromPackageJson(pkgJsonPath: string, root: string) {
|
||||
const json: PackageJson = readJsonFile(join(root, pkgJsonPath));
|
||||
return {
|
||||
projects: {
|
||||
[json.name]: buildProjectConfigurationFromPackageJson(
|
||||
const project = buildProjectConfigurationFromPackageJson(
|
||||
json,
|
||||
pkgJsonPath,
|
||||
readNxJson(root)
|
||||
),
|
||||
);
|
||||
return {
|
||||
projects: {
|
||||
[project.root]: project,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@ -56,30 +56,42 @@ 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 (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."
|
||||
@ -97,7 +109,8 @@ export function toOldFormat(w: any) {
|
||||
renamePropertyWithStableKeys(target, 'executor', 'builder');
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (w.generators) {
|
||||
renamePropertyWithStableKeys(w, 'generators', 'schematics');
|
||||
|
||||
@ -39,7 +39,7 @@ describe('nx project.json plugin', () => {
|
||||
.toMatchInlineSnapshot(`
|
||||
{
|
||||
"projects": {
|
||||
"root": {
|
||||
".": {
|
||||
"name": "root",
|
||||
"root": ".",
|
||||
"targets": {
|
||||
@ -53,7 +53,7 @@ describe('nx project.json plugin', () => {
|
||||
.toMatchInlineSnapshot(`
|
||||
{
|
||||
"projects": {
|
||||
"lib-a": {
|
||||
"packages/lib-a": {
|
||||
"name": "lib-a",
|
||||
"root": "packages/lib-a",
|
||||
"targets": {
|
||||
|
||||
@ -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,
|
||||
},
|
||||
};
|
||||
},
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
@ -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';
|
||||
|
||||
/**
|
||||
@ -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>;
|
||||
};
|
||||
|
||||
@ -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),
|
||||
},
|
||||
},
|
||||
@ -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>>;
|
||||
|
||||
@ -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(
|
||||
|
||||
@ -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(
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user