feat(graph): add target group for scripts (#26035)

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

## Current Behavior
<!-- This is the behavior we have today -->

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->
<img width="1131" alt="Screenshot 2024-05-24 at 3 58 30 PM"
src="https://github.com/nrwl/nx/assets/16211801/9aadeac3-ca74-4449-baf6-21d7c97118aa">

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
This commit is contained in:
Emily Xiong 2024-05-27 11:06:29 -04:00 committed by GitHub
parent e0450f7969
commit ef4035a56a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 69 additions and 14 deletions

View File

@ -12,7 +12,7 @@ export const TargetConfigurationGroupHeader = ({
className = '',
}: TargetConfigurationGroupHeaderProps) => {
return (
<header className={`px-4 py-2 text-lg ${className}`}>
<header className={`px-4 py-2 text-lg capitalize ${className}`}>
{targetGroupName}{' '}
<Pill
text={

View File

@ -51,20 +51,29 @@ describe('Workspaces', () => {
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": {},
},
},
}
`);
});
});
});

View File

@ -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",

View File

@ -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),
};
}

View File

@ -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": {

View File

@ -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);

View File

@ -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<string, TargetConfiguration> = {};