-
+
+ Learn more about how projects are configured
+
)}
diff --git a/packages/nx/src/config/workspace-json-project-json.ts b/packages/nx/src/config/workspace-json-project-json.ts
index 83b3ea352ed86..d2ef9a8987425 100644
--- a/packages/nx/src/config/workspace-json-project-json.ts
+++ b/packages/nx/src/config/workspace-json-project-json.ts
@@ -124,6 +124,7 @@ export interface ProjectMetadata {
}
export interface TargetMetadata {
+ [k: string]: any;
description?: string;
technologies?: string[];
}
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 dc3aa60310b61..42ea4e7f4c504 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
@@ -48,6 +48,10 @@ describe('nx package.json workspaces plugin', () => {
"targets": {
"echo": {
"executor": "nx:run-script",
+ "metadata": {
+ "runCommand": "npm run echo",
+ "scriptContent": "echo root project",
+ },
"options": {
"script": "echo",
},
@@ -83,6 +87,10 @@ describe('nx package.json workspaces plugin', () => {
},
"test": {
"executor": "nx:run-script",
+ "metadata": {
+ "runCommand": "npm run test",
+ "scriptContent": "jest",
+ },
"options": {
"script": "test",
},
@@ -111,6 +119,10 @@ describe('nx package.json workspaces plugin', () => {
"targets": {
"build": {
"executor": "nx:run-script",
+ "metadata": {
+ "runCommand": "npm run build",
+ "scriptContent": "tsc",
+ },
"options": {
"script": "build",
},
@@ -127,6 +139,10 @@ describe('nx package.json workspaces plugin', () => {
},
"test": {
"executor": "nx:run-script",
+ "metadata": {
+ "runCommand": "npm run test",
+ "scriptContent": "jest",
+ },
"options": {
"script": "test",
},
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 cc55fa3f7f968..76cbfaf967c6d 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
@@ -58,6 +58,10 @@ describe('nx project.json plugin', () => {
},
"test": {
"executor": "nx:run-script",
+ "metadata": {
+ "runCommand": "npm run test",
+ "scriptContent": "jest",
+ },
"options": {
"script": "test",
},
diff --git a/packages/nx/src/plugins/target-defaults/target-defaults-plugin.spec.ts b/packages/nx/src/plugins/target-defaults/target-defaults-plugin.spec.ts
index 8dd59a38c9e37..4f000fe8833dc 100644
--- a/packages/nx/src/plugins/target-defaults/target-defaults-plugin.spec.ts
+++ b/packages/nx/src/plugins/target-defaults/target-defaults-plugin.spec.ts
@@ -78,6 +78,7 @@ describe('target-defaults plugin', () => {
"^build",
],
"executor": "nx:run-commands",
+ "metadata": {},
},
},
},
@@ -121,6 +122,10 @@ describe('target-defaults plugin', () => {
"test": {
"command": "jest",
"executor": "nx:run-script",
+ "metadata": {
+ "runCommand": "npm run test",
+ "scriptContent": "nx affected:test",
+ },
"options": {
"script": "test",
},
@@ -169,6 +174,10 @@ describe('target-defaults plugin', () => {
"test": {
"command": "jest",
"executor": "nx:run-script",
+ "metadata": {
+ "runCommand": "npm run test",
+ "scriptContent": "nx affected:test",
+ },
"options": {
"script": "test",
},
@@ -380,12 +389,14 @@ describe('target-defaults plugin', () => {
"targets": {
"echo": {
"executor": "nx:run-commands",
+ "metadata": {},
"options": {
"cwd": "{projectRoot}",
},
},
"echo2": {
"executor": "nx:run-commands",
+ "metadata": {},
"options": {
"cwd": "{projectRoot}",
},
@@ -445,12 +456,14 @@ describe('target-defaults plugin', () => {
"targets": {
"echo": {
"executor": "nx:run-commands",
+ "metadata": {},
"options": {
"cwd": "{projectRoot}",
},
},
"echo2": {
"executor": "nx:run-commands",
+ "metadata": {},
"options": {
"cwd": "{projectRoot}",
},
@@ -483,6 +496,7 @@ describe('target-defaults plugin', () => {
expect(result).toMatchInlineSnapshot(`
{
"command": "echo hi",
+ "metadata": {},
}
`);
});
@@ -504,6 +518,7 @@ describe('target-defaults plugin', () => {
expect(result).toMatchInlineSnapshot(`
{
"executor": "nx:run-commands",
+ "metadata": {},
"options": {
"command": "echo hi",
},
@@ -524,6 +539,7 @@ describe('target-defaults plugin', () => {
).toMatchInlineSnapshot(`
{
"executor": "nx:run-script",
+ "metadata": {},
"options": {
"script": "build",
},
diff --git a/packages/nx/src/plugins/target-defaults/target-defaults-plugin.ts b/packages/nx/src/plugins/target-defaults/target-defaults-plugin.ts
index f383e2c477fd8..b98770b9b0668 100644
--- a/packages/nx/src/plugins/target-defaults/target-defaults-plugin.ts
+++ b/packages/nx/src/plugins/target-defaults/target-defaults-plugin.ts
@@ -167,10 +167,15 @@ export function getTargetInfo(
...packageJsonTarget?.options,
...projectJsonTarget?.options,
};
+ const metadata = {
+ ...packageJsonTarget?.metadata,
+ ...projectJsonTarget?.metadata,
+ };
if (projectJsonTarget?.command) {
return {
command: projectJsonTarget?.command,
+ metadata,
};
}
@@ -181,6 +186,7 @@ export function getTargetInfo(
options: {
command: targetOptions?.command,
},
+ metadata,
};
} else if (targetOptions?.commands) {
return {
@@ -188,10 +194,12 @@ export function getTargetInfo(
options: {
commands: targetOptions.commands,
},
+ metadata,
};
}
return {
executor: 'nx:run-commands',
+ metadata,
};
}
@@ -201,6 +209,7 @@ export function getTargetInfo(
options: {
script: targetOptions?.script ?? target,
},
+ metadata,
};
}
diff --git a/packages/nx/src/utils/package-json.spec.ts b/packages/nx/src/utils/package-json.spec.ts
index 7503762451d5d..5508fd2a42c97 100644
--- a/packages/nx/src/utils/package-json.spec.ts
+++ b/packages/nx/src/utils/package-json.spec.ts
@@ -29,23 +29,35 @@ describe('readTargetsFromPackageJson', () => {
options: {
script: 'build',
},
+ metadata: {
+ runCommand: 'npm run build',
+ scriptContent: 'echo 1',
+ },
};
it('should read targets from project.json and package.json', () => {
const result = readTargetsFromPackageJson(packageJson);
- expect(result).toEqual({
- build: {
- executor: 'nx:run-script',
- options: {
- script: 'build',
+ expect(result).toMatchInlineSnapshot(`
+ {
+ "build": {
+ "executor": "nx:run-script",
+ "metadata": {
+ "runCommand": "npm run build",
+ "scriptContent": "echo 1",
+ },
+ "options": {
+ "script": "build",
+ },
},
- },
- 'nx-release-publish': {
- dependsOn: ['^nx-release-publish'],
- executor: '@nx/js:release-publish',
- options: {},
- },
- });
+ "nx-release-publish": {
+ "dependsOn": [
+ "^nx-release-publish",
+ ],
+ "executor": "@nx/js:release-publish",
+ "options": {},
+ },
+ }
+ `);
});
it('should contain extended options from nx property in package.json', () => {
@@ -86,17 +98,27 @@ describe('readTargetsFromPackageJson', () => {
},
});
- expect(result).toEqual({
- test: {
- executor: 'nx:run-script',
- options: { script: 'test' },
- },
- 'nx-release-publish': {
- dependsOn: ['^nx-release-publish'],
- executor: '@nx/js:release-publish',
- options: {},
- },
- });
+ expect(result).toMatchInlineSnapshot(`
+ {
+ "nx-release-publish": {
+ "dependsOn": [
+ "^nx-release-publish",
+ ],
+ "executor": "@nx/js:release-publish",
+ "options": {},
+ },
+ "test": {
+ "executor": "nx:run-script",
+ "metadata": {
+ "runCommand": "npm run test",
+ "scriptContent": "echo testing",
+ },
+ "options": {
+ "script": "test",
+ },
+ },
+ }
+ `);
});
it('should extend script based targets if matching config', () => {
@@ -117,6 +139,10 @@ describe('readTargetsFromPackageJson', () => {
expect(result.build).toMatchInlineSnapshot(`
{
"executor": "nx:run-script",
+ "metadata": {
+ "runCommand": "npm run build",
+ "scriptContent": "echo 1",
+ },
"options": {
"script": "build",
},
diff --git a/packages/nx/src/utils/package-json.ts b/packages/nx/src/utils/package-json.ts
index 0b7de85bf4b99..14f4e2fdea65e 100644
--- a/packages/nx/src/utils/package-json.ts
+++ b/packages/nx/src/utils/package-json.ts
@@ -7,6 +7,7 @@ import {
import { mergeTargetConfigurations } from '../project-graph/utils/project-configuration-utils';
import { readJsonFile } from './fileutils';
import { getNxRequirePaths } from './installation-directory';
+import { getPackageManagerCommand } from './package-manager';
export interface NxProjectPackageJsonConfiguration {
implicitDependencies?: string[];
@@ -115,12 +116,20 @@ export function readNxMigrateConfig(
};
}
-export function buildTargetFromScript(script: string): TargetConfiguration {
+export function buildTargetFromScript(
+ script: string,
+ scripts: Record
= {}
+): TargetConfiguration {
+ const packageManagerCommand = getPackageManagerCommand();
return {
executor: 'nx:run-script',
options: {
script,
},
+ metadata: {
+ scriptContent: scripts[script],
+ runCommand: packageManagerCommand.run(script),
+ },
};
}
@@ -130,7 +139,7 @@ export function readTargetsFromPackageJson(packageJson: PackageJson) {
const includedScripts = nx?.includedScripts || Object.keys(scripts ?? {});
//
for (const script of includedScripts) {
- res[script] = buildTargetFromScript(script);
+ res[script] = buildTargetFromScript(script, scripts);
}
for (const targetName in nx?.targets) {
res[targetName] = mergeTargetConfigurations(
diff --git a/packages/nx/src/utils/package-manager.ts b/packages/nx/src/utils/package-manager.ts
index b6668acc07e76..2fe5f28ef4776 100644
--- a/packages/nx/src/utils/package-manager.ts
+++ b/packages/nx/src/utils/package-manager.ts
@@ -26,7 +26,7 @@ export interface PackageManagerCommands {
exec: string;
dlx: string;
list: string;
- run: (script: string, args: string) => string;
+ run: (script: string, args?: string) => string;
getRegistryUrl: string;
}
@@ -100,7 +100,8 @@ export function getPackageManagerCommand(
rm: 'yarn remove',
exec: 'yarn',
dlx: useBerry ? 'yarn dlx' : 'yarn',
- run: (script: string, args: string) => `yarn ${script} ${args}`,
+ run: (script: string, args?: string) =>
+ `yarn ${script}${args ? ` ${args}` : ''}`,
list: useBerry ? 'yarn info --name-only' : 'yarn list',
getRegistryUrl: useBerry
? 'yarn config get npmRegistryServer'
@@ -122,10 +123,14 @@ export function getPackageManagerCommand(
rm: 'pnpm rm',
exec: modernPnpm ? 'pnpm exec' : 'pnpx',
dlx: modernPnpm ? 'pnpm dlx' : 'pnpx',
- run: (script: string, args: string) =>
- includeDoubleDashBeforeArgs
- ? `pnpm run ${script} -- ${args}`
- : `pnpm run ${script} ${args}`,
+ run: (script: string, args?: string) =>
+ `pnpm run ${script}${
+ args
+ ? includeDoubleDashBeforeArgs
+ ? ' -- ' + args
+ : ` ${args}`
+ : ''
+ }`,
list: 'pnpm ls --depth 100',
getRegistryUrl: 'pnpm config get registry',
};
@@ -143,7 +148,8 @@ export function getPackageManagerCommand(
rm: 'npm rm',
exec: 'npx',
dlx: 'npx',
- run: (script: string, args: string) => `npm run ${script} -- ${args}`,
+ run: (script: string, args?: string) =>
+ `npm run ${script}${args ? ' -- ' + args : ''}`,
list: 'npm ls',
getRegistryUrl: 'npm config get registry',
};