From 23bc60c35b0f7245916ffa9347cb99d82d10cb5c Mon Sep 17 00:00:00 2001 From: Craigory Coppola Date: Wed, 26 Apr 2023 12:42:16 -0400 Subject: [PATCH] feat(core): update inputs object form to match dependsOn form --- packages/nx/schemas/nx-schema.json | 35 +++++++++++++-- packages/nx/schemas/project-schema.json | 37 +++++++++++++-- .../src/config/workspace-json-project-json.ts | 4 +- .../update-depends-on-to-tokens.spec.ts | 45 ++++++++++++++----- .../update-depends-on-to-tokens.ts | 40 +++++++++++++++-- 5 files changed, 138 insertions(+), 23 deletions(-) diff --git a/packages/nx/schemas/nx-schema.json b/packages/nx/schemas/nx-schema.json index b541d26c4acab..5a9c255927789 100644 --- a/packages/nx/schemas/nx-schema.json +++ b/packages/nx/schemas/nx-schema.json @@ -98,15 +98,44 @@ "type": "object", "properties": { "projects": { - "type": "string", - "description": "The projects that the targets belong to.", - "enum": ["self", "dependencies"] + "oneOf": [ + { + "type": "string", + "description": "The project that the targets belong to." + }, + { + "type": "array", + "description": "The projects that the targets belong to.", + "items": { "type": "string" } + } + ] + }, + "dependencies": { + "type": "boolean", + "description": "Include files belonging to the input for all the project dependencies of this target." }, "input": { "type": "string", "description": "The name of the input." } }, + "oneOf": [ + { + "required": ["projects", "input"] + }, + { + "required": ["dependencies", "input"] + }, + { + "required": ["input"], + "not": { + "anyOf": [ + { "required": ["projects"] }, + { "required": ["dependencies"] } + ] + } + } + ], "additionalProperties": false }, { diff --git a/packages/nx/schemas/project-schema.json b/packages/nx/schemas/project-schema.json index 4337d5c4c82de..34e9305c998f7 100644 --- a/packages/nx/schemas/project-schema.json +++ b/packages/nx/schemas/project-schema.json @@ -147,15 +147,44 @@ "type": "object", "properties": { "projects": { - "type": "string", - "description": "The projects that the input belong to.", - "enum": ["self", "dependencies"] + "oneOf": [ + { + "type": "string", + "description": "The project that the targets belong to." + }, + { + "type": "array", + "description": "The projects that the targets belong to.", + "items": { "type": "string" } + } + ] + }, + "dependencies": { + "type": "boolean", + "description": "Include files belonging to the input for all the project dependencies of this target." }, "input": { "type": "string", - "description": "Named input." + "description": "The name of the input." } }, + "oneOf": [ + { + "required": ["projects", "input"] + }, + { + "required": ["dependencies", "input"] + }, + { + "required": ["input"], + "not": { + "anyOf": [ + { "required": ["projects"] }, + { "required": ["dependencies"] } + ] + } + } + ], "additionalProperties": false }, { diff --git a/packages/nx/src/config/workspace-json-project-json.ts b/packages/nx/src/config/workspace-json-project-json.ts index 661bd80d222ca..f0a5a8efa7c87 100644 --- a/packages/nx/src/config/workspace-json-project-json.ts +++ b/packages/nx/src/config/workspace-json-project-json.ts @@ -126,7 +126,9 @@ export interface TargetDependencyConfig { } export type InputDefinition = - | { input: string; projects: 'self' | 'dependencies' } + | { input: string; projects: string | string[] } + | { input: string; dependencies: true } + | { input: string } | { fileset: string } | { runtime: string } | { env: string }; diff --git a/packages/nx/src/migrations/update-16-0-0/update-depends-on-to-tokens.spec.ts b/packages/nx/src/migrations/update-16-0-0/update-depends-on-to-tokens.spec.ts index d1acc7b30dca8..70daa8bdceced 100644 --- a/packages/nx/src/migrations/update-16-0-0/update-depends-on-to-tokens.spec.ts +++ b/packages/nx/src/migrations/update-16-0-0/update-depends-on-to-tokens.spec.ts @@ -19,6 +19,7 @@ describe('update-depends-on-to-tokens', () => { projects: 'self', }, ], + inputs: [{ projects: 'self', input: 'someInput' }], }, test: { dependsOn: [ @@ -26,6 +27,7 @@ describe('update-depends-on-to-tokens', () => { projects: 'dependencies', }, ], + inputs: [{ projects: 'dependencies', input: 'someInput' }], }, other: { dependsOn: ['^deps'], @@ -35,11 +37,21 @@ describe('update-depends-on-to-tokens', () => { }); await update(tree); const nxJson = readNxJson(tree); - const build = nxJson.targetDefaults.build.dependsOn[0] as any; - const test = nxJson.targetDefaults.test.dependsOn[0] as any; - expect(build.projects).not.toBeDefined(); - expect(test.projects).not.toBeDefined(); - expect(test.dependencies).toEqual(true); + const buildDependencyConfiguration = nxJson.targetDefaults.build + .dependsOn[0] as any; + const testDependencyConfiguration = nxJson.targetDefaults.test + .dependsOn[0] as any; + const buildInputConfiguration = nxJson.targetDefaults.build + .inputs[0] as any; + const testInputConfiguration = nxJson.targetDefaults.test.inputs[0] as any; + expect(buildDependencyConfiguration.projects).not.toBeDefined(); + expect(buildDependencyConfiguration.dependencies).not.toBeDefined(); + expect(buildInputConfiguration.projects).not.toBeDefined(); + expect(buildInputConfiguration.dependencies).not.toBeDefined(); + expect(testInputConfiguration.projects).not.toBeDefined(); + expect(testInputConfiguration.dependencies).toEqual(true); + expect(testDependencyConfiguration.projects).not.toBeDefined(); + expect(testDependencyConfiguration.dependencies).toEqual(true); expect(nxJson.targetDefaults.other.dependsOn).toEqual(['^deps']); }); @@ -55,6 +67,7 @@ describe('update-depends-on-to-tokens', () => { target: 'build', }, ], + inputs: [{ projects: 'self', input: 'someInput' }], }, test: { dependsOn: [ @@ -63,6 +76,7 @@ describe('update-depends-on-to-tokens', () => { target: 'test', }, ], + inputs: [{ projects: 'dependencies', input: 'someInput' }], }, other: { dependsOn: ['^deps'], @@ -71,13 +85,22 @@ describe('update-depends-on-to-tokens', () => { }); await update(tree); const project = readProjectConfiguration(tree, 'proj1'); - const build = project.targets.build.dependsOn[0] as any; - const test = project.targets.test.dependsOn[0] as any; - expect(build.projects).not.toBeDefined(); - expect(build.dependencies).not.toBeDefined(); - expect(test.projects).not.toBeDefined(); - expect(test.dependencies).toEqual(true); + const buildDependencyConfiguration = project.targets.build + .dependsOn[0] as any; + const testDependencyConfiguration = project.targets.test + .dependsOn[0] as any; + const buildInputConfiguration = project.targets.build.inputs[0] as any; + const testInputConfiguration = project.targets.test.inputs[0] as any; + expect(buildDependencyConfiguration.projects).not.toBeDefined(); + expect(buildDependencyConfiguration.dependencies).not.toBeDefined(); + expect(buildInputConfiguration.projects).not.toBeDefined(); + expect(buildInputConfiguration.dependencies).not.toBeDefined(); + expect(testDependencyConfiguration.projects).not.toBeDefined(); + expect(testDependencyConfiguration.dependencies).toEqual(true); + expect(testInputConfiguration.projects).not.toBeDefined(); + expect(testInputConfiguration.dependencies).toEqual(true); expect(project.targets.other.dependsOn).toEqual(['^deps']); + expect(project.targets.other.inputs).not.toBeDefined(); }); it('should not throw on nulls', async () => { diff --git a/packages/nx/src/migrations/update-16-0-0/update-depends-on-to-tokens.ts b/packages/nx/src/migrations/update-16-0-0/update-depends-on-to-tokens.ts index f3e347ed1a90b..e72014d85199b 100644 --- a/packages/nx/src/migrations/update-16-0-0/update-depends-on-to-tokens.ts +++ b/packages/nx/src/migrations/update-16-0-0/update-depends-on-to-tokens.ts @@ -7,7 +7,7 @@ import { import { Tree } from '../../generators/tree'; export default async function (tree: Tree) { - updateDependsOnInsideNxJson(tree); + updateDependsOnAndInputsInsideNxJson(tree); const projectsConfigurations = getProjects(tree); for (const [projectName, projectConfiguration] of projectsConfigurations) { @@ -21,19 +21,35 @@ export default async function (tree: Tree) { delete dependency.projects; projectChanged = true; } else if (dependency.projects === 'dependencies') { - delete dependency.projects + delete dependency.projects; dependency.dependencies = true; projectChanged = true; } } } + for (let i = 0; i < targetConfiguration.inputs?.length ?? 0; i++) { + const input = targetConfiguration.inputs[i]; + if (typeof input !== 'string') { + if ('projects' in input && input.projects === 'self') { + delete input.projects; + projectChanged = true; + } else if ('projects' in input && input.projects === 'dependencies') { + delete input.projects; + targetConfiguration.inputs[i] = { + ...input, + dependencies: true, + }; + projectChanged = true; + } + } + } } if (projectChanged) { updateProjectConfiguration(tree, projectName, projectConfiguration); } } } -function updateDependsOnInsideNxJson(tree: Tree) { +function updateDependsOnAndInputsInsideNxJson(tree: Tree) { const nxJson = readNxJson(tree); let nxJsonChanged = false; for (const [target, defaults] of Object.entries( @@ -45,12 +61,28 @@ function updateDependsOnInsideNxJson(tree: Tree) { delete dependency.projects; nxJsonChanged = true; } else if (dependency.projects === 'dependencies') { - delete dependency.projects + delete dependency.projects; dependency.dependencies = true; nxJsonChanged = true; } } } + for (let i = 0; i < defaults.inputs?.length ?? 0; i++) { + const input = defaults.inputs[i]; + if (typeof input !== 'string') { + if ('projects' in input && input.projects === 'self') { + delete input.projects; + nxJsonChanged = true; + } else if ('projects' in input && input.projects === 'dependencies') { + delete input.projects; + defaults.inputs[i] = { + ...input, + dependencies: true, + }; + nxJsonChanged = true; + } + } + } } if (nxJsonChanged) { updateNxJson(tree, nxJson);