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:
parent
da1f8515c7
commit
c27b33a6ec
@ -67,7 +67,6 @@ forEachCli('angular', cli => {
|
||||
`
|
||||
);
|
||||
};
|
||||
debugger;
|
||||
|
||||
createDep(parentLib, [childLib, childLib2]);
|
||||
});
|
||||
|
||||
@ -149,9 +149,18 @@ describe('AngularLibraryWebBuildBuilder', () => {
|
||||
});
|
||||
|
||||
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',
|
||||
version: '1.2.3'
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
// 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();
|
||||
});
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@ -6,7 +6,7 @@ import {
|
||||
import { JsonObject } from '@angular-devkit/core';
|
||||
import { stripIndents } from '@angular-devkit/core/src/utils/literals';
|
||||
import * as ng from '@angular/compiler-cli';
|
||||
import { readJsonFile } from '@nrwl/workspace';
|
||||
import { readJsonFile, output } from '@nrwl/workspace';
|
||||
import {
|
||||
createProjectGraph,
|
||||
ProjectGraphNode,
|
||||
@ -149,6 +149,15 @@ export function calculateLibraryDependencies(
|
||||
.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
|
||||
* the proper dependency and version
|
||||
@ -168,11 +177,16 @@ function updatePackageJsonDependencies(
|
||||
const jsonOutputFile = `${distLibOutputPath}/package.json`;
|
||||
if (libDependencies && libDependencies.length > 0) {
|
||||
const outputJson = readJsonFile(jsonOutputFile);
|
||||
let writeJson = false;
|
||||
|
||||
outputJson.dependencies = outputJson.dependencies || {};
|
||||
|
||||
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?)
|
||||
const packageJsonPath = join(
|
||||
context.workspaceRoot,
|
||||
@ -182,11 +196,14 @@ function updatePackageJsonDependencies(
|
||||
const depNodePackageJson = readJsonFile(packageJsonPath);
|
||||
|
||||
outputJson.dependencies[entry.scope] = depNodePackageJson.version;
|
||||
writeJson = true;
|
||||
}
|
||||
});
|
||||
|
||||
if (writeJson) {
|
||||
writeJsonFile(jsonOutputFile, outputJson);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function run(
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user