From ef4035a56ad389bcc65dcb54026dfabd6dc56e90 Mon Sep 17 00:00:00 2001 From: Emily Xiong Date: Mon, 27 May 2024 11:06:29 -0400 Subject: [PATCH] feat(graph): add target group for scripts (#26035) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Current Behavior ## Expected Behavior Screenshot 2024-05-24 at 3 58 30 PM ## Related Issue(s) Fixes # --- ...get-configuration-details-group-header.tsx | 2 +- .../nx/src/config/to-project-name.spec.ts | 35 ++++++++++++------- .../create-nodes.spec.ts | 22 ++++++++++++ .../package-json-workspaces/create-nodes.ts | 2 ++ .../package-json-next-to-project-json.spec.ts | 7 ++++ .../package-json-next-to-project-json.ts | 2 ++ packages/nx/src/utils/package-json.ts | 13 +++++++ 7 files changed, 69 insertions(+), 14 deletions(-) diff --git a/graph/ui-project-details/src/lib/target-configuration-details-group-header/target-configuration-details-group-header.tsx b/graph/ui-project-details/src/lib/target-configuration-details-group-header/target-configuration-details-group-header.tsx index 06b7a72c08..cbe4f6ee2b 100644 --- a/graph/ui-project-details/src/lib/target-configuration-details-group-header/target-configuration-details-group-header.tsx +++ b/graph/ui-project-details/src/lib/target-configuration-details-group-header/target-configuration-details-group-header.tsx @@ -12,7 +12,7 @@ export const TargetConfigurationGroupHeader = ({ className = '', }: TargetConfigurationGroupHeaderProps) => { return ( -
+
{targetGroupName}{' '} { return res; } ); - expect(projects['packages/my-package']).toEqual({ - name: 'my-package', - root: 'packages/my-package', - sourceRoot: 'packages/my-package', - projectType: 'library', - targets: { - 'nx-release-publish': { - configurations: {}, - dependsOn: ['^nx-release-publish'], - executor: '@nx/js:release-publish', - options: {}, + expect(projects['packages/my-package']).toMatchInlineSnapshot(` + { + "metadata": { + "targetGroups": { + "NPM Scripts": [], + }, }, - }, - }); + "name": "my-package", + "projectType": "library", + "root": "packages/my-package", + "sourceRoot": "packages/my-package", + "targets": { + "nx-release-publish": { + "configurations": {}, + "dependsOn": [ + "^nx-release-publish", + ], + "executor": "@nx/js:release-publish", + "options": {}, + }, + }, + } + `); }); }); }); diff --git a/packages/nx/src/plugins/package-json-workspaces/create-nodes.spec.ts b/packages/nx/src/plugins/package-json-workspaces/create-nodes.spec.ts index 42ea4e7f4c..511ae154ce 100644 --- a/packages/nx/src/plugins/package-json-workspaces/create-nodes.spec.ts +++ b/packages/nx/src/plugins/package-json-workspaces/create-nodes.spec.ts @@ -41,6 +41,13 @@ describe('nx package.json workspaces plugin', () => { { "projects": { ".": { + "metadata": { + "targetGroups": { + "NPM Scripts": [ + "echo", + ], + }, + }, "name": "root", "projectType": "library", "root": ".", @@ -73,6 +80,13 @@ describe('nx package.json workspaces plugin', () => { { "projects": { "packages/lib-a": { + "metadata": { + "targetGroups": { + "NPM Scripts": [ + "test", + ], + }, + }, "name": "lib-a", "projectType": "library", "root": "packages/lib-a", @@ -112,6 +126,14 @@ describe('nx package.json workspaces plugin', () => { "build", "test", ], + "metadata": { + "targetGroups": { + "NPM Scripts": [ + "build", + "test", + ], + }, + }, "name": "lib-b", "projectType": "library", "root": "packages/lib-b", diff --git a/packages/nx/src/plugins/package-json-workspaces/create-nodes.ts b/packages/nx/src/plugins/package-json-workspaces/create-nodes.ts index 78c3bbcb53..0ee7c34ca7 100644 --- a/packages/nx/src/plugins/package-json-workspaces/create-nodes.ts +++ b/packages/nx/src/plugins/package-json-workspaces/create-nodes.ts @@ -11,6 +11,7 @@ import { NX_PREFIX } from '../../utils/logger'; import { output } from '../../utils/output'; import { PackageJson, + getMetadataFromPackageJson, readTargetsFromPackageJson, } from '../../utils/package-json'; import { joinPathFragments } from '../../utils/path'; @@ -101,6 +102,7 @@ export function buildProjectConfigurationFromPackageJson( projectType, ...packageJson.nx, targets: readTargetsFromPackageJson(packageJson), + metadata: getMetadataFromPackageJson(packageJson), }; } diff --git a/packages/nx/src/plugins/project-json/build-nodes/package-json-next-to-project-json.spec.ts b/packages/nx/src/plugins/project-json/build-nodes/package-json-next-to-project-json.spec.ts index 76cbfaf967..125601cb96 100644 --- a/packages/nx/src/plugins/project-json/build-nodes/package-json-next-to-project-json.spec.ts +++ b/packages/nx/src/plugins/project-json/build-nodes/package-json-next-to-project-json.spec.ts @@ -46,6 +46,13 @@ describe('nx project.json plugin', () => { { "projects": { "lib-a": { + "metadata": { + "targetGroups": { + "NPM Scripts": [ + "test", + ], + }, + }, "name": "lib-a", "root": "packages/lib-a", "targets": { diff --git a/packages/nx/src/plugins/project-json/build-nodes/package-json-next-to-project-json.ts b/packages/nx/src/plugins/project-json/build-nodes/package-json-next-to-project-json.ts index 19421aea54..cd748ce872 100644 --- a/packages/nx/src/plugins/project-json/build-nodes/package-json-next-to-project-json.ts +++ b/packages/nx/src/plugins/project-json/build-nodes/package-json-next-to-project-json.ts @@ -5,6 +5,7 @@ import { readJsonFile } from '../../../utils/fileutils'; import { ProjectConfiguration } from '../../../config/workspace-json-project-json'; import { PackageJson, + getMetadataFromPackageJson, readTargetsFromPackageJson, } from '../../../utils/package-json'; @@ -54,6 +55,7 @@ function createProjectFromPackageJsonNextToProjectJson( name, root, targets: readTargetsFromPackageJson(packageJson), + metadata: getMetadataFromPackageJson(packageJson), } as ProjectConfiguration; } catch (e) { console.log(e); diff --git a/packages/nx/src/utils/package-json.ts b/packages/nx/src/utils/package-json.ts index ea68784b9d..ccf6b47017 100644 --- a/packages/nx/src/utils/package-json.ts +++ b/packages/nx/src/utils/package-json.ts @@ -2,6 +2,7 @@ import { existsSync } from 'fs'; import { dirname, join } from 'path'; import { InputDefinition, + ProjectMetadata, TargetConfiguration, } from '../config/workspace-json-project-json'; import { mergeTargetConfigurations } from '../project-graph/utils/project-configuration-utils'; @@ -138,6 +139,18 @@ export function buildTargetFromScript( let packageManagerCommand: PackageManagerCommands | undefined; +export function getMetadataFromPackageJson( + packageJson: PackageJson +): ProjectMetadata { + const { scripts, nx } = packageJson ?? {}; + const includedScripts = nx?.includedScripts || Object.keys(scripts ?? {}); + return { + targetGroups: { + 'NPM Scripts': includedScripts, + }, + }; +} + export function readTargetsFromPackageJson(packageJson: PackageJson) { const { scripts, nx, private: isPrivate } = packageJson ?? {}; const res: Record = {};