fix(angular): check for devDeps & peerDeps when writing package version for dep libs

when determining the package version of dependent libraries, make sure that there's no devDeps or
peerDep already set. In such case don't touch the package.json
This commit is contained in:
Juri 2020-01-27 12:34:00 +01:00 committed by Victor Savkin
parent da1f8515c7
commit c27b33a6ec
3 changed files with 62 additions and 7 deletions

View File

@ -67,7 +67,6 @@ forEachCli('angular', cli => {
` `
); );
}; };
debugger;
createDep(parentLib, [childLib, childLib2]); createDep(parentLib, [childLib, childLib2]);
}); });

View File

@ -149,9 +149,18 @@ describe('AngularLibraryWebBuildBuilder', () => {
}); });
it('should update the package.json', async () => { it('should update the package.json', async () => {
spyOn(workspaceUtils, 'readJsonFile').and.returnValue({ spyOn(workspaceUtils, 'readJsonFile').and.callFake((path: string) => {
if (path.endsWith('buildable-parent/package.json')) {
return {
name: '@proj/buildable-parent',
version: '3.3.3'
};
} else {
return {
name: '@proj/buildable-child', name: '@proj/buildable-child',
version: '1.2.3' version: '1.2.3'
};
}
}); });
// act // act
@ -168,5 +177,35 @@ describe('AngularLibraryWebBuildBuilder', () => {
}) })
); );
}); });
['dependencies', 'devDependencies', 'peerDependencies'].forEach(
(depConfigName: string) => {
it(`should not update the package.json if the ${depConfigName} already contain a matching entry`, async () => {
spyOn(workspaceUtils, 'readJsonFile').and.callFake((path: string) => {
if (path.endsWith('buildable-parent/package.json')) {
return {
name: '@proj/buildable-parent',
version: '1.2.3',
[depConfigName]: {
'@proj/buildable-child': '1.1.1'
}
};
} else {
return {
name: '@proj/buildable-child',
version: '1.2.3'
};
}
});
// act
const result = await run(testOptions, context).toPromise();
// assert
expect(result.success).toBeTruthy();
expect(fileUtils.writeJsonFile).not.toHaveBeenCalled();
});
}
);
}); });
}); });

View File

@ -6,7 +6,7 @@ import {
import { JsonObject } from '@angular-devkit/core'; import { JsonObject } from '@angular-devkit/core';
import { stripIndents } from '@angular-devkit/core/src/utils/literals'; import { stripIndents } from '@angular-devkit/core/src/utils/literals';
import * as ng from '@angular/compiler-cli'; import * as ng from '@angular/compiler-cli';
import { readJsonFile } from '@nrwl/workspace'; import { readJsonFile, output } from '@nrwl/workspace';
import { import {
createProjectGraph, createProjectGraph,
ProjectGraphNode, ProjectGraphNode,
@ -149,6 +149,15 @@ export function calculateLibraryDependencies(
.filter(x => !!x); .filter(x => !!x);
} }
// verify whether the package.json already specifies the dep
function hasDependency(outputJson, depConfigName: string, packageName: string) {
if (outputJson[depConfigName]) {
return outputJson[depConfigName][packageName];
} else {
return false;
}
}
/** /**
* Updates the peerDependencies section in the `dist/lib/xyz/package.json` with * Updates the peerDependencies section in the `dist/lib/xyz/package.json` with
* the proper dependency and version * the proper dependency and version
@ -168,11 +177,16 @@ function updatePackageJsonDependencies(
const jsonOutputFile = `${distLibOutputPath}/package.json`; const jsonOutputFile = `${distLibOutputPath}/package.json`;
if (libDependencies && libDependencies.length > 0) { if (libDependencies && libDependencies.length > 0) {
const outputJson = readJsonFile(jsonOutputFile); const outputJson = readJsonFile(jsonOutputFile);
let writeJson = false;
outputJson.dependencies = outputJson.dependencies || {}; outputJson.dependencies = outputJson.dependencies || {};
libDependencies.forEach(entry => { libDependencies.forEach(entry => {
if (!outputJson.dependencies[entry.scope]) { if (
!hasDependency(outputJson, 'dependencies', entry.scope) &&
!hasDependency(outputJson, 'devDependencies', entry.scope) &&
!hasDependency(outputJson, 'peerDependencies', entry.scope)
) {
// read the lib version (should we read the one from the dist?) // read the lib version (should we read the one from the dist?)
const packageJsonPath = join( const packageJsonPath = join(
context.workspaceRoot, context.workspaceRoot,
@ -182,12 +196,15 @@ function updatePackageJsonDependencies(
const depNodePackageJson = readJsonFile(packageJsonPath); const depNodePackageJson = readJsonFile(packageJsonPath);
outputJson.dependencies[entry.scope] = depNodePackageJson.version; outputJson.dependencies[entry.scope] = depNodePackageJson.version;
writeJson = true;
} }
}); });
if (writeJson) {
writeJsonFile(jsonOutputFile, outputJson); writeJsonFile(jsonOutputFile, outputJson);
} }
} }
}
export function run( export function run(
options: BuildAngularLibraryBuilderOptions & JsonObject, options: BuildAngularLibraryBuilderOptions & JsonObject,