Skip to content

Commit

Permalink
fix(misc): fix migrations updating target options to consider target …
Browse files Browse the repository at this point in the history
…defaults (#18064)
  • Loading branch information
leosvelperez authored Jul 11, 2023
1 parent 5c98726 commit 8be6558
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import {
ProjectConfiguration,
ProjectGraph,
Tree,
addProjectConfiguration,
readProjectConfiguration,
} from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import migration from './explicitly-set-projects-to-update-buildable-deps';

let projectGraph: ProjectGraph;
jest.mock('@nx/devkit', () => ({
...jest.requireActual('@nx/devkit'),
createProjectGraphAsync: () => Promise.resolve(projectGraph),
}));

describe('explicitly-set-projects-to-update-buildable-deps migration', () => {
let tree: Tree;

beforeEach(() => {
tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' });
projectGraph = { dependencies: {}, nodes: {} };
});

it.each([
Expand All @@ -22,7 +30,7 @@ describe('explicitly-set-projects-to-update-buildable-deps migration', () => {
])(
'should set updateBuildableProjectDepsInPackageJson option to "true" when not specified in target using "%s"',
async (executor) => {
addProjectConfiguration(tree, 'lib1', {
addProject(tree, 'lib1', {
root: 'libs/lib1',
projectType: 'library',
targets: { build: { executor, options: {} } },
Expand All @@ -37,6 +45,29 @@ describe('explicitly-set-projects-to-update-buildable-deps migration', () => {
}
);

it.each([
'@nx/angular:ng-packagr-lite',
'@nrwl/angular:ng-packagr-lite',
'@nx/angular:package',
'@nrwl/angular:package',
])(
'should set updateBuildableProjectDepsInPackageJson option to "true" when target has no options object defined using "%s"',
async (executor) => {
addProject(tree, 'lib1', {
root: 'libs/lib1',
projectType: 'library',
targets: { build: { executor } },
});

await migration(tree);

const project = readProjectConfiguration(tree, 'lib1');
expect(
project.targets.build.options.updateBuildableProjectDepsInPackageJson
).toBe(true);
}
);

it.each([
'@nx/angular:ng-packagr-lite',
'@nrwl/angular:ng-packagr-lite',
Expand All @@ -45,7 +76,7 @@ describe('explicitly-set-projects-to-update-buildable-deps migration', () => {
])(
'should not overwrite updateBuildableProjectDepsInPackageJson option when it is specified in target using "%s"',
async (executor) => {
addProjectConfiguration(tree, 'lib1', {
addProject(tree, 'lib1', {
root: 'libs/lib1',
projectType: 'library',
targets: {
Expand Down Expand Up @@ -76,11 +107,29 @@ describe('explicitly-set-projects-to-update-buildable-deps migration', () => {
},
},
};
addProjectConfiguration(tree, 'lib1', originalProjectConfig);
addProject(tree, 'lib1', originalProjectConfig);

await migration(tree);

const project = readProjectConfiguration(tree, 'lib1');
expect(project.targets).toStrictEqual(originalProjectConfig.targets);
});
});

function addProject(
tree: Tree,
projectName: string,
config: ProjectConfiguration
): void {
projectGraph = {
dependencies: {},
nodes: {
[projectName]: {
data: config,
name: projectName,
type: config.projectType === 'application' ? 'app' : 'lib',
},
},
};
addProjectConfiguration(tree, projectName, config);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
createProjectGraphAsync,
formatFiles,
getProjects,
readProjectConfiguration,
Tree,
updateProjectConfiguration,
} from '@nx/devkit';
Expand All @@ -13,31 +14,37 @@ const executors = new Set([
]);

export default async function (tree: Tree) {
const projects = getProjects(tree);
// use project graph to get the expanded target configurations
const projectGraph = await createProjectGraphAsync();

for (const [projectName, project] of projects) {
if (project.projectType !== 'library') {
for (const [projectName, { data: projectData }] of Object.entries(
projectGraph.nodes
)) {
if (projectData.projectType !== 'library') {
continue;
}

let updated = false;
for (const [, target] of Object.entries(project.targets || {})) {
for (const [targetName, target] of Object.entries(
projectData.targets || {}
)) {
if (!executors.has(target.executor)) {
continue;
}

if (
target.options &&
!target.options ||
target.options.updateBuildableProjectDepsInPackageJson === undefined
) {
target.options.updateBuildableProjectDepsInPackageJson = true;
updated = true;
// read the project configuration to write the explicit project configuration
// and avoid writing the expanded target configuration
const project = readProjectConfiguration(tree, projectName);
project.targets[targetName].options ??= {};
project.targets[
targetName
].options.updateBuildableProjectDepsInPackageJson = true;
updateProjectConfiguration(tree, projectName, project);
}
}

if (updated) {
updateProjectConfiguration(tree, projectName, project);
}
}

await formatFiles(tree);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import {
ProjectConfiguration,
ProjectGraph,
Tree,
addProjectConfiguration,
readProjectConfiguration,
} from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import migration from './explicitly-set-projects-to-update-buildable-deps';

let projectGraph: ProjectGraph;
jest.mock('@nx/devkit', () => ({
...jest.requireActual('@nx/devkit'),
createProjectGraphAsync: () => Promise.resolve(projectGraph),
}));

describe('explicitly-set-projects-to-update-buildable-deps migration', () => {
let tree: Tree;

Expand All @@ -17,7 +24,7 @@ describe('explicitly-set-projects-to-update-buildable-deps migration', () => {
it.each(['@nx/js:swc', '@nrwl/js:swc', '@nx/js:tsc', '@nrwl/js:tsc'])(
'should set updateBuildableProjectDepsInPackageJson option to "true" when not specified in target using "%s"',
async (executor) => {
addProjectConfiguration(tree, 'lib1', {
addProject(tree, 'lib1', {
root: 'libs/lib1',
projectType: 'library',
targets: { build: { executor, options: {} } },
Expand All @@ -32,10 +39,28 @@ describe('explicitly-set-projects-to-update-buildable-deps migration', () => {
}
);

it.each(['@nx/js:swc', '@nrwl/js:swc', '@nx/js:tsc', '@nrwl/js:tsc'])(
'should set updateBuildableProjectDepsInPackageJson option to "true" when target has no options object defined using "%s"',
async (executor) => {
addProject(tree, 'lib1', {
root: 'libs/lib1',
projectType: 'library',
targets: { build: { executor } },
});

await migration(tree);

const project = readProjectConfiguration(tree, 'lib1');
expect(
project.targets.build.options.updateBuildableProjectDepsInPackageJson
).toBe(true);
}
);

it.each(['@nx/js:swc', '@nrwl/js:swc', '@nx/js:tsc', '@nrwl/js:tsc'])(
'should not overwrite updateBuildableProjectDepsInPackageJson option when it is specified in target using "%s"',
async (executor) => {
addProjectConfiguration(tree, 'lib1', {
addProject(tree, 'lib1', {
root: 'libs/lib1',
projectType: 'library',
targets: {
Expand Down Expand Up @@ -66,11 +91,29 @@ describe('explicitly-set-projects-to-update-buildable-deps migration', () => {
},
},
};
addProjectConfiguration(tree, 'lib1', originalProjectConfig);
addProject(tree, 'lib1', originalProjectConfig);

await migration(tree);

const project = readProjectConfiguration(tree, 'lib1');
expect(project.targets).toStrictEqual(originalProjectConfig.targets);
});
});

function addProject(
tree: Tree,
projectName: string,
config: ProjectConfiguration
): void {
projectGraph = {
dependencies: {},
nodes: {
[projectName]: {
data: config,
name: projectName,
type: config.projectType === 'application' ? 'app' : 'lib',
},
},
};
addProjectConfiguration(tree, projectName, config);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
createProjectGraphAsync,
formatFiles,
getProjects,
readProjectConfiguration,
Tree,
updateProjectConfiguration,
} from '@nx/devkit';
Expand All @@ -13,31 +14,37 @@ const executors = new Set([
]);

export default async function (tree: Tree) {
const projects = getProjects(tree);
// use project graph to get the expanded target configurations
const projectGraph = await createProjectGraphAsync();

for (const [projectName, project] of projects) {
if (project.projectType !== 'library') {
for (const [projectName, { data: projectData }] of Object.entries(
projectGraph.nodes
)) {
if (projectData.projectType !== 'library') {
continue;
}

let updated = false;
for (const [, target] of Object.entries(project.targets || {})) {
for (const [targetName, target] of Object.entries(
projectData.targets || {}
)) {
if (!executors.has(target.executor)) {
continue;
}

if (
target.options &&
!target.options ||
target.options.updateBuildableProjectDepsInPackageJson === undefined
) {
target.options.updateBuildableProjectDepsInPackageJson = true;
updated = true;
// read the project configuration to write the explicit project configuration
// and avoid writing the expanded target configuration
const project = readProjectConfiguration(tree, projectName);
project.targets[targetName].options ??= {};
project.targets[
targetName
].options.updateBuildableProjectDepsInPackageJson = true;
updateProjectConfiguration(tree, projectName, project);
}
}

if (updated) {
updateProjectConfiguration(tree, projectName, project);
}
}

await formatFiles(tree);
Expand Down
Loading

1 comment on commit 8be6558

@vercel
Copy link

@vercel vercel bot commented on 8be6558 Jul 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-five.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx-dev-nrwl.vercel.app
nx.dev

Please sign in to comment.