feat(@schematics/update): add packageGroup version map support

Fixes #13015
This commit is contained in:
Hans Larsen 2018-11-20 16:11:23 -08:00 committed by Minko Gechev
parent a3a657f7e7
commit 851d6948a4
5 changed files with 110 additions and 14 deletions

View File

@ -76,7 +76,8 @@ interface PackageInfo {
}
interface UpdateMetadata {
packageGroup: string[];
packageGroupName?: string;
packageGroup: { [ packageName: string ]: string };
requirements: { [packageName: string]: string };
migrations?: string;
}
@ -88,9 +89,9 @@ function _updatePeerVersion(infoMap: Map<string, PackageInfo>, name: string, ran
return range;
}
if (maybePackageInfo.target) {
name = maybePackageInfo.target.updateMetadata.packageGroup[0] || name;
name = maybePackageInfo.target.updateMetadata.packageGroupName || name;
} else {
name = maybePackageInfo.installed.updateMetadata.packageGroup[0] || name;
name = maybePackageInfo.installed.updateMetadata.packageGroupName || name;
}
const maybeTransform = peerCompatibleWhitelist[name];
@ -353,7 +354,7 @@ function _getUpdateMetadata(
const metadata = packageJson['ng-update'];
const result: UpdateMetadata = {
packageGroup: [],
packageGroup: {},
requirements: {},
};
@ -363,15 +364,28 @@ function _getUpdateMetadata(
if (metadata['packageGroup']) {
const packageGroup = metadata['packageGroup'];
// Verify that packageGroup is an array of strings. This is not an error but we still warn
// the user and ignore the packageGroup keys.
if (!Array.isArray(packageGroup) || packageGroup.some(x => typeof x != 'string')) {
// Verify that packageGroup is an array of strings or an map of versions. This is not an error
// but we still warn the user and ignore the packageGroup keys.
if (Array.isArray(packageGroup) && packageGroup.every(x => typeof x == 'string')) {
result.packageGroup = packageGroup.reduce((group, name) => {
group[name] = packageJson.version;
return group;
}, result.packageGroup);
} else if (typeof packageGroup == 'object' && packageGroup
&& Object.values(packageGroup).every(x => typeof x == 'string')) {
result.packageGroup = packageGroup;
} else {
logger.warn(
`packageGroup metadata of package ${packageJson.name} is malformed. Ignoring.`,
);
} else {
result.packageGroup = packageGroup;
}
result.packageGroupName = Object.keys(result.packageGroup)[0];
}
if (typeof metadata['packageGroupName'] == 'string') {
result.packageGroupName = metadata['packageGroupName'];
}
if (metadata['requirements']) {
@ -654,21 +668,33 @@ function _addPackageGroup(
return;
}
const packageGroup = ngUpdateMetadata['packageGroup'];
let packageGroup = ngUpdateMetadata['packageGroup'];
if (!packageGroup) {
return;
}
if (!Array.isArray(packageGroup) || packageGroup.some(x => typeof x != 'string')) {
if (Array.isArray(packageGroup) && !packageGroup.some(x => typeof x != 'string')) {
packageGroup = packageGroup.reduce((acc, curr) => {
acc[curr] = maybePackage;
return acc;
}, {} as { [name: string]: string });
}
// Only need to check if it's an object because we set it right the time before.
if (typeof packageGroup != 'object'
|| packageGroup === null
|| Object.values(packageGroup).some(v => typeof v != 'string')
) {
logger.warn(`packageGroup metadata of package ${npmPackageJson.name} is malformed.`);
return;
}
packageGroup
Object.keys(packageGroup)
.filter(name => !packages.has(name)) // Don't override names from the command line.
.filter(name => allDependencies.has(name)) // Remove packages that aren't installed.
.forEach(name => {
packages.set(name, maybePackage);
packages.set(name, packageGroup[name]);
});
}

View File

@ -222,6 +222,40 @@ describe('@schematics/update', () => {
).toPromise().then(done, done.fail);
}, 45000);
it('uses packageGroup for versioning', async () => {
// Add the basic migration package.
const content = virtualFs.fileBufferToString(host.sync.read(normalize('/package.json')));
const packageJson = JSON.parse(content);
const dependencies = packageJson['dependencies'];
dependencies['@angular-devkit-tests/update-package-group-1'] = '1.0.0';
dependencies['@angular-devkit-tests/update-package-group-2'] = '1.0.0';
host.sync.write(
normalize('/package.json'),
virtualFs.stringToFileBuffer(JSON.stringify(packageJson)),
);
await schematicRunner.runSchematicAsync('update', {
packages: ['@angular-devkit-tests/update-package-group-1'],
}, appTree).pipe(
map(tree => {
const packageJson = JSON.parse(tree.readContent('/package.json'));
const deps = packageJson['dependencies'];
expect(deps['@angular-devkit-tests/update-package-group-1']).toBe('1.2.0');
expect(deps['@angular-devkit-tests/update-package-group-2']).toBe('2.0.0');
// Check install task.
expect(schematicRunner.tasks).toEqual([
{
name: 'node-package',
options: jasmine.objectContaining({
command: 'install',
}),
},
]);
}),
).toPromise();
}, 45000);
it('can migrate only', done => {
// Add the basic migration package.
const content = virtualFs.fileBufferToString(host.sync.read(normalize('/package.json')));

View File

@ -0,0 +1,14 @@
## Update Package Group packages
* `update-package-group-1@1.0.0` -> `update-package-group2@^1`
* `update-package-group-2@1.0.0` -> `update-package-group1@1.0.0`
----
* `update-package-group-1@1.0.0` -> `update-package-group2@^1`
* `update-package-group-2@1.1.0` -> `update-package-group1@1.0.0`
----
* `update-package-group-1@1.2.0` -> `update-package-group2@^2`
* `update-package-group-2@2.0.0` -> `update-package-group1@^1`
----

View File

@ -0,0 +1,11 @@
{
"name": "@angular-devkit-tests/update-package-group-1",
"version": "1.2.0",
"description": "Tests",
"ng-update": {
"packageGroup": {
"@angular-devkit-tests/update-package-group-1": "",
"@angular-devkit-tests/update-package-group-2": "^2"
}
}
}

View File

@ -0,0 +1,11 @@
{
"name": "@angular-devkit-tests/update-package-group-2",
"version": "2.0.0",
"description": "Tests",
"ng-update": {
"packageGroup": {
"@angular-devkit-tests/update-package-group-1": "^1",
"@angular-devkit-tests/update-package-group-2": ""
}
}
}