Skip to content

Commit

Permalink
fix(angular): update library package.json only with direct dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
leosvelperez committed Mar 16, 2022
1 parent 2d08719 commit e7d1e9f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 21 deletions.
Expand Up @@ -31,6 +31,7 @@ describe('NgPackagrLite executor', () => {
).mockImplementation(() => ({
target: {},
dependencies: [],
topLevelDependencies: [],
}));

ngPackagrBuildMock = jest.fn(() => Promise.resolve());
Expand Down
Expand Up @@ -31,6 +31,7 @@ describe('Package executor', () => {
).mockImplementation(() => ({
target: {},
dependencies: [],
topLevelDependencies: [],
}));
ngPackagrBuildMock = jest.fn(() => Promise.resolve());
ngPackagerWatchSubject = new BehaviorSubject<void>(undefined);
Expand Down
19 changes: 10 additions & 9 deletions packages/angular/src/executors/package/package.impl.ts
Expand Up @@ -66,13 +66,14 @@ export function createLibraryExecutor(
options: BuildAngularLibraryExecutorOptions,
context: ExecutorContext
) {
const { target, dependencies } = calculateProjectDependencies(
readCachedProjectGraph(),
context.root,
context.projectName,
context.targetName,
context.configurationName
);
const { target, dependencies, topLevelDependencies } =
calculateProjectDependencies(
readCachedProjectGraph(),
context.root,
context.projectName,
context.targetName,
context.configurationName
);
if (
!checkDependentProjectsHaveBeenBuilt(
context.root,
Expand All @@ -86,7 +87,7 @@ export function createLibraryExecutor(

function updatePackageJson(): void {
if (
dependencies.length > 0 &&
topLevelDependencies.length > 0 &&
options.updateBuildableProjectDepsInPackageJson
) {
updateBuildableProjectPackageJsonDependencies(
Expand All @@ -95,7 +96,7 @@ export function createLibraryExecutor(
context.targetName,
context.configurationName,
target,
dependencies,
topLevelDependencies,
options.buildableProjectDepsInPackageJsonType
);
}
Expand Down
37 changes: 25 additions & 12 deletions packages/workspace/src/utilities/buildable-libs-utils.ts
Expand Up @@ -38,20 +38,23 @@ export function calculateProjectDependencies(
target: ProjectGraphProjectNode;
dependencies: DependentBuildableProjectNode[];
nonBuildableDependencies: string[];
topLevelDependencies: DependentBuildableProjectNode[];
} {
const target = projGraph.nodes[projectName];
// gather the library dependencies
const nonBuildableDependencies = [];
const topLevelDependencies: DependentBuildableProjectNode[] = [];
const dependencies = collectDependencies(projectName, projGraph, [], shallow)
.map((dep) => {
.map(({ name: dep, isTopLevel }) => {
let project: DependentBuildableProjectNode = null;
const depNode = projGraph.nodes[dep] || projGraph.externalNodes[dep];
if (depNode.type === ProjectType.lib) {
if (isBuildable(targetName, depNode)) {
const libPackageJson = readJsonFile(
join(root, depNode.data.root, 'package.json')
);

return {
project = {
name: libPackageJson.name, // i.e. @workspace/mylib
outputs: getOutputsForTargetAndConfiguration(
{
Expand All @@ -70,30 +73,40 @@ export function calculateProjectDependencies(
nonBuildableDependencies.push(dep);
}
} else if (depNode.type === 'npm') {
return {
project = {
name: depNode.data.packageName,
outputs: [],
node: depNode,
};
} else {
return null;
}

if (project && isTopLevel) {
topLevelDependencies.push(project);
}

return project;
})
.filter((x) => !!x);
return { target, dependencies, nonBuildableDependencies };
return {
target,
dependencies,
nonBuildableDependencies,
topLevelDependencies,
};
}

function collectDependencies(
project: string,
projGraph: ProjectGraph,
acc: string[],
shallow?: boolean
) {
acc: { name: string; isTopLevel: boolean }[],
shallow?: boolean,
areTopLevelDeps = true
): { name: string; isTopLevel: boolean }[] {
(projGraph.dependencies[project] || []).forEach((dependency) => {
if (acc.indexOf(dependency.target) === -1) {
acc.push(dependency.target);
if (!acc.some((dep) => dep.name === dependency.target)) {
acc.push({ name: dependency.target, isTopLevel: areTopLevelDeps });
if (!shallow) {
collectDependencies(dependency.target, projGraph, acc);
collectDependencies(dependency.target, projGraph, acc, shallow, false);
}
}
});
Expand Down

0 comments on commit e7d1e9f

Please sign in to comment.