Skip to content

Commit

Permalink
feat(core): update inputs object form to match dependsOn form
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed Apr 26, 2023
1 parent 9d28096 commit 23bc60c
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 23 deletions.
35 changes: 32 additions & 3 deletions packages/nx/schemas/nx-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
{
Expand Down
37 changes: 33 additions & 4 deletions packages/nx/schemas/project-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
{
Expand Down
4 changes: 3 additions & 1 deletion packages/nx/src/config/workspace-json-project-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ describe('update-depends-on-to-tokens', () => {
projects: 'self',
},
],
inputs: [{ projects: 'self', input: 'someInput' }],
},
test: {
dependsOn: [
{
projects: 'dependencies',
},
],
inputs: [{ projects: 'dependencies', input: 'someInput' }],
},
other: {
dependsOn: ['^deps'],
Expand All @@ -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']);
});

Expand All @@ -55,6 +67,7 @@ describe('update-depends-on-to-tokens', () => {
target: 'build',
},
],
inputs: [{ projects: 'self', input: 'someInput' }],
},
test: {
dependsOn: [
Expand All @@ -63,6 +76,7 @@ describe('update-depends-on-to-tokens', () => {
target: 'test',
},
],
inputs: [{ projects: 'dependencies', input: 'someInput' }],
},
other: {
dependsOn: ['^deps'],
Expand All @@ -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 () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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(
Expand All @@ -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);
Expand Down

0 comments on commit 23bc60c

Please sign in to comment.