nx/scripts/angular-support-upgrades/fetch-versions-from-registry.ts
Leosvel Pérez Espinosa 58ba1ffc6d
feat(angular): support angular v18.1.0 (#26504)
<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

## Current Behavior
<!-- This is the behavior we have today -->

Angular v18.1.0 is not supported.

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

Angular v18.1.0 should be supported.

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
2024-07-11 11:17:00 -04:00

93 lines
2.5 KiB
TypeScript

import axios from 'axios';
import { coerce, gt, SemVer } from 'semver';
type PackageSpec = string | { main: string; children: string[] };
const packagesToUpdate: PackageSpec[] = [
{
main: '@angular/cli',
children: [
'@angular-devkit/build-angular',
'@angular-devkit/core',
'@angular-devkit/schematics',
'@angular/build',
'@angular/pwa',
'@angular/ssr',
'@schematics/angular',
],
},
{
main: '@angular-devkit/architect',
children: ['@angular-devkit/build-webpack'],
},
{
main: '@angular/core',
children: [
'@angular/common',
'@angular/compiler',
'@angular/compiler-cli',
'@angular/router',
],
},
{
main: '@angular/material',
children: ['@angular/cdk'],
},
'ng-packagr',
];
export async function fetchVersionsFromRegistry(
targetVersion: 'latest' | 'next'
) {
console.log('⏳ - Fetching versions from registry...');
const packageVersionMap = new Map<string, string>();
await Promise.all(
packagesToUpdate.map((pkgSpec) =>
fetch(packageVersionMap, pkgSpec, targetVersion)
)
);
console.log('✅ - Finished fetching versions from registry');
return packageVersionMap;
}
async function fetch(
packageVersionMap: Map<string, string>,
pkgSpec: PackageSpec,
targetVersion: 'latest' | 'next'
) {
function setPackageVersions(pkgSpec: PackageSpec, version: string) {
if (typeof pkgSpec === 'string') {
packageVersionMap.set(pkgSpec, version);
return;
}
packageVersionMap.set(pkgSpec.main, version);
for (const child of pkgSpec.children) {
packageVersionMap.set(child, version);
}
}
const pkgName = typeof pkgSpec === 'string' ? pkgSpec : pkgSpec.main;
// set it to empty initially to keep the order of the specified packages
setPackageVersions(pkgSpec, '');
const response = await axios.get(`https://registry.npmjs.org/${pkgName}`);
const distTags = response.data['dist-tags'];
const latestVersion = distTags['latest'];
if (targetVersion === 'latest') {
setPackageVersions(pkgSpec, latestVersion);
console.log(` ${pkgName}: ${latestVersion}`);
} else {
const nextVersion = distTags['next'] ?? latestVersion;
const coercedNextVersion = coerce(nextVersion) as SemVer;
// check which is the greater version
const versionToUse = gt(coercedNextVersion, latestVersion)
? nextVersion
: latestVersion;
setPackageVersions(pkgSpec, versionToUse);
console.log(` ${pkgName}: ${versionToUse}`);
}
}