Skip to content

Commit 6a9bd36

Browse files
hanslfilipesilva
authored andcommitted
fix(@schematics/update): group packages together
Packages will be reduced to their package name. Added the spec for the new packageGroupName argument (default to the first item of the packageGroup). Fixes angular/angular-cli#10248 Fixes angular/angular-cli#10247
1 parent cbd3239 commit 6a9bd36

File tree

2 files changed

+43
-24
lines changed

2 files changed

+43
-24
lines changed

docs/specifications/update.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ In order to implement migrations in a library, the author must add the `ng-updat
7575
|---|---|---|
7676
| `requirements` | `{ [packageName: string]: VersionRange }` | A map of package names to version to check for minimal requirement. If one of the libraries listed here does not match the version range specified in `requirements`, an error will be shown to the user to manually update those libraries. For example, `@angular/core` does not support updates from versions earlier than 5, so this field would be `{ '@angular/core': '>= 5' }`.
7777
| `migrations` | `string` | A relative path (or resolved using Node module resolution) to a Schematics collection definition. |
78-
| `packageGroup` | `string[]` | A list of npm packages that are to be grouped together. When running
78+
| `packageGroup` | `string[]` | A list of npm packages that are to be grouped together. When running the update schematic it will automatically include all packages as part of the packageGroup in the update (if the user also installed them). |
79+
| `packageGroupName` | `string` | The name of the packageGroup to use. By default, uses the first package in the packageGroup. The packageGroupName needs to be part of the packageGroup and should be a valid package name. |
7980
8081
#### Example given:
8182
Library my-lib wants to have 2 steps to update from version 4 -> 4.5 and 4.5 to 5. It would add this information in its `package.json`:

packages/schematics/update/update/index.ts

+41-23
Original file line numberDiff line numberDiff line change
@@ -331,26 +331,53 @@ function _usageMessage(
331331
infoMap: Map<string, PackageInfo>,
332332
logger: logging.LoggerApi,
333333
) {
334+
const packageGroups = new Map<string, string>();
334335
const packagesToUpdate = [...infoMap.entries()]
335-
.sort()
336336
.map(([name, info]) => {
337337
const tag = options.next ? 'next' : 'latest';
338338
const version = info.npmPackageJson['dist-tags'][tag];
339339
const target = info.npmPackageJson.versions[version];
340340

341-
return [
341+
return {
342342
name,
343343
info,
344344
version,
345+
tag,
345346
target,
346-
] as [string, PackageInfo, string, JsonSchemaForNpmPackageJsonFiles];
347+
};
347348
})
348-
.filter(([name, info, version, target]) => {
349+
.filter(({ name, info, version, target }) => {
349350
return (target && semver.compare(info.installed.version, version) < 0);
350351
})
351-
.filter(([, , , target]) => {
352+
.filter(({ target }) => {
352353
return target['ng-update'];
353-
});
354+
})
355+
.map(({ name, info, version, tag, target }) => {
356+
// Look for packageGroup.
357+
if (target['ng-update'] && target['ng-update']['packageGroup']) {
358+
const packageGroup = target['ng-update']['packageGroup'];
359+
const packageGroupName = target['ng-update']['packageGroupName']
360+
|| target['ng-update']['packageGroup'][0];
361+
if (packageGroupName) {
362+
if (packageGroups.has(name)) {
363+
return null;
364+
}
365+
366+
packageGroup.forEach((x: string) => packageGroups.set(x, packageGroupName));
367+
packageGroups.set(packageGroupName, packageGroupName);
368+
name = packageGroupName;
369+
}
370+
}
371+
372+
let command = `ng update ${name}`;
373+
if (tag == 'next') {
374+
command += ' --next';
375+
}
376+
377+
return [name, `${info.installed.version} -> ${version}`, command];
378+
})
379+
.filter(x => x !== null)
380+
.sort((a, b) => a && b ? a[0].localeCompare(b[0]) : 0);
354381

355382
if (packagesToUpdate.length == 0) {
356383
logger.info('We analyzed your package.json and everything seems to be in order. Good work!');
@@ -367,29 +394,20 @@ function _usageMessage(
367394
if (!Number.isFinite(namePad)) {
368395
namePad = 30;
369396
}
397+
const pads = [namePad, 25, 0];
370398

371399
logger.info(
372400
' '
373-
+ 'Name'.padEnd(namePad)
374-
+ 'Version'.padEnd(25)
375-
+ ' Command to update',
401+
+ ['Name', 'Version', 'Command to update'].map((x, i) => x.padEnd(pads[i])).join(''),
376402
);
377-
logger.info(' ' + '-'.repeat(namePad * 2 + 35));
378-
379-
packagesToUpdate.forEach(([name, info, version, target]) => {
380-
let command = `npm install ${name}`;
381-
if (target && target['ng-update']) {
382-
// Show the ng command only when migrations are supported, otherwise it's a fancy
383-
// npm install, really.
384-
command = `ng update ${name}`;
403+
logger.info(' ' + '-'.repeat(pads.reduce((s, x) => s += x, 0) + 20));
404+
405+
packagesToUpdate.forEach(fields => {
406+
if (!fields) {
407+
return;
385408
}
386409

387-
logger.info(
388-
' '
389-
+ name.padEnd(namePad)
390-
+ `${info.installed.version} -> ${version}`.padEnd(25)
391-
+ ' ' + command,
392-
);
410+
logger.info(' ' + fields.map((x, i) => x.padEnd(pads[i])).join(''));
393411
});
394412

395413
logger.info('\n');

0 commit comments

Comments
 (0)