From b581705212423f5c943fc1aa64163ec0c7d5dc58 Mon Sep 17 00:00:00 2001 From: Craigory Coppola Date: Tue, 6 Aug 2024 09:28:56 -0400 Subject: [PATCH] fix(core): script-based targets should be able to be modified in a project.json file --- .../src/plugins/package-json/create-nodes.spec.ts | 15 +++++++++++++++ .../nx/src/plugins/package-json/create-nodes.ts | 11 ++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/packages/nx/src/plugins/package-json/create-nodes.spec.ts b/packages/nx/src/plugins/package-json/create-nodes.spec.ts index 7560f89db2f13..fa6bdda6d70db 100644 --- a/packages/nx/src/plugins/package-json/create-nodes.spec.ts +++ b/packages/nx/src/plugins/package-json/create-nodes.spec.ts @@ -570,6 +570,7 @@ describe('nx package.json workspaces plugin', () => { name: 'root', scripts: { build: 'echo build', + test: 'echo test', }, }), 'packages/a/project.json': JSON.stringify({ @@ -577,6 +578,9 @@ describe('nx package.json workspaces plugin', () => { 'something-other-than-build': { command: 'echo something-other-than-build', }, + test: { + dependsOn: ['build-native'], + }, }, }), }, @@ -597,6 +601,7 @@ describe('nx package.json workspaces plugin', () => { "targetGroups": { "NPM Scripts": [ "build", + "test", ], }, }, @@ -624,6 +629,16 @@ describe('nx package.json workspaces plugin', () => { "executor": "@nx/js:release-publish", "options": {}, }, + "test": { + "executor": "nx:run-script", + "metadata": { + "runCommand": "npm run test", + "scriptContent": "echo test", + }, + "options": { + "script": "test", + }, + }, }, }, }, diff --git a/packages/nx/src/plugins/package-json/create-nodes.ts b/packages/nx/src/plugins/package-json/create-nodes.ts index d722ae90554c9..e983c8f7eaa3d 100644 --- a/packages/nx/src/plugins/package-json/create-nodes.ts +++ b/packages/nx/src/plugins/package-json/create-nodes.ts @@ -151,7 +151,16 @@ export function buildProjectConfigurationFromPackageJson( if (siblingProjectJson) { for (const target of Object.keys(siblingProjectJson?.targets ?? {})) { - delete packageJson.scripts?.[target]; + const { executor, command, options } = siblingProjectJson.targets[target]; + if ( + // will use run-commands, different target + command || + // Either uses a different executor or runs a different script + (executor && + (executor !== 'nx:run-script' || options?.script !== target)) + ) { + delete packageJson.scripts?.[target]; + } } }