chore(angular): improve angular updater script helper (#20532)

This commit is contained in:
Leosvel Pérez Espinosa 2023-12-01 17:30:08 +01:00 committed by GitHub
parent 4c17667c84
commit 68d0f792e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 92 additions and 68 deletions

View File

@ -15,7 +15,6 @@ async function addMigrationPackageGroup(
) { ) {
angularPackageMigrations.packageJsonUpdates[targetNxVersion] = { angularPackageMigrations.packageJsonUpdates[targetNxVersion] = {
version: `${targetNxMigrationVersion}`, version: `${targetNxMigrationVersion}`,
packages: {},
}; };
const promptAndRequirements = await getPromptAndRequiredVersions( const promptAndRequirements = await getPromptAndRequiredVersions(
@ -35,10 +34,16 @@ async function addMigrationPackageGroup(
}; };
} }
for (const [pkgName, version] of packageVersionMap.entries()) { angularPackageMigrations.packageJsonUpdates[targetNxVersion].packages = {};
for (const [pkgName, version] of packageVersionMap) {
if ( if (
pkgName.startsWith('@angular/') && pkgName.startsWith('@angular/') &&
!['@angular/core', '@angular/material', '@angular/cdk'].includes(pkgName) ![
'@angular/core',
'@angular/material',
'@angular/cdk',
'@angular/ssr',
].includes(pkgName)
) { ) {
continue; continue;
} }

View File

@ -1,24 +1,35 @@
import axios from 'axios'; import axios from 'axios';
import { coerce, gt, SemVer } from 'semver'; import { coerce, gt, SemVer } from 'semver';
const packagesToUpdate = [ type PackageSpec = string | { main: string; children: string[] };
'@angular-devkit/architect', const packagesToUpdate: PackageSpec[] = [
'@angular-devkit/build-angular', {
'@angular-devkit/build-webpack', main: '@angular/cli',
'@angular-devkit/core', children: [
'@angular-devkit/schematics', '@angular-devkit/build-angular',
'@angular/cli', '@angular-devkit/core',
'@angular/common', '@angular-devkit/schematics',
'@angular/compiler', '@angular/ssr',
'@angular/compiler-cli', '@schematics/angular',
'@angular/core', ],
'@angular/router', },
'@angular/material', {
'@angular/cdk', main: '@angular-devkit/architect',
'@nguniversal/builders', children: ['@angular-devkit/build-webpack'],
'@nguniversal/common', },
'@nguniversal/express-engine', {
'@schematics/angular', main: '@angular/core',
children: [
'@angular/common',
'@angular/compiler',
'@angular/compiler-cli',
'@angular/router',
],
},
{
main: '@angular/material',
children: ['@angular/cdk'],
},
'ng-packagr', 'ng-packagr',
]; ];
@ -26,25 +37,54 @@ export async function fetchVersionsFromRegistry(
targetVersion: 'latest' | 'next' targetVersion: 'latest' | 'next'
) { ) {
console.log('⏳ - Fetching versions from registry...'); console.log('⏳ - Fetching versions from registry...');
const packageVersionMap = new Map<string, string>(); const packageVersionMap = new Map<string, string>();
for (const pkgName of packagesToUpdate) { await Promise.all(
const response = await axios.get(`https://registry.npmjs.org/${pkgName}`); packagesToUpdate.map((pkgSpec) =>
const distTags = response.data['dist-tags']; fetch(packageVersionMap, pkgSpec, targetVersion)
const latestVersion = distTags['latest']; )
if (targetVersion === 'latest') { );
packageVersionMap.set(pkgName, latestVersion);
console.log(` ${pkgName}: ${latestVersion}`);
} else {
const nextVersion = distTags['next'];
const coercedNextVersion = coerce(nextVersion) as SemVer;
// check which is the greater version
const versionToUse = gt(coercedNextVersion, latestVersion)
? nextVersion
: latestVersion;
packageVersionMap.set(pkgName, versionToUse);
console.log(` ${pkgName}: ${versionToUse}`);
}
}
console.log('✅ - Finished fetching versions from registry'); console.log('✅ - Finished fetching versions from registry');
return packageVersionMap; 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}`);
}
}

View File

@ -1,34 +1,18 @@
import { readFileSync, writeFileSync } from 'fs'; import { readFileSync, writeFileSync } from 'fs';
function updateRootPackageJson(packageVersionMap: Map<string, string>) { function updatePackageJson(
const pathToPkgJson = 'package.json'; pathToPkgJson: string,
packageVersionMap: Map<string, string>
) {
const pkgJson = JSON.parse( const pkgJson = JSON.parse(
readFileSync(pathToPkgJson, { encoding: 'utf-8' }) readFileSync(pathToPkgJson, { encoding: 'utf-8' })
); );
for (const [pkgName, version] of packageVersionMap.entries()) { for (const [pkgName, version] of packageVersionMap.entries()) {
if (pkgJson.devDependencies && pkgJson.devDependencies[pkgName]) { if (pkgJson.devDependencies?.[pkgName]) {
pkgJson.devDependencies[pkgName] = `~${version}`; pkgJson.devDependencies[pkgName] = `~${version}`;
} }
if (pkgJson.dependencies[pkgName]) { if (pkgJson.dependencies?.[pkgName]) {
pkgJson.dependencies[pkgName] = `~${version}`;
}
}
writeFileSync(pathToPkgJson, JSON.stringify(pkgJson, null, 2));
}
function updateAngularPackageJson(packageVersionMap: Map<string, string>) {
const pathToPkgJson = 'packages/angular/package.json';
const pkgJson = JSON.parse(
readFileSync(pathToPkgJson, { encoding: 'utf-8' })
);
for (const [pkgName, version] of packageVersionMap.entries()) {
if (pkgJson.devDependencies && pkgJson.devDependencies[pkgName]) {
pkgJson.devDependencies[pkgName] = `~${version}`;
}
if (pkgJson.dependencies && pkgJson.dependencies[pkgName]) {
pkgJson.dependencies[pkgName] = `~${version}`; pkgJson.dependencies[pkgName] = `~${version}`;
} }
} }
@ -40,7 +24,7 @@ export async function updatePackageJsonForAngular(
packageVersionMap: Map<string, string> packageVersionMap: Map<string, string>
) { ) {
console.log('⏳ - Writing package.json files...'); console.log('⏳ - Writing package.json files...');
updateRootPackageJson(packageVersionMap); updatePackageJson('package.json', packageVersionMap);
updateAngularPackageJson(packageVersionMap); updatePackageJson('packages/angular/package.json', packageVersionMap);
console.log('✅ - Wrote package.json files'); console.log('✅ - Wrote package.json files');
} }

View File

@ -7,7 +7,6 @@ function updateAngularVersionUtils(packageVersionMap: Map<string, string>) {
const angularVersion = packageVersionMap.get('@angular/core')!; const angularVersion = packageVersionMap.get('@angular/core')!;
const angularDevkitVersion = packageVersionMap.get('@angular/cli')!; const angularDevkitVersion = packageVersionMap.get('@angular/cli')!;
const ngPackagrVersion = packageVersionMap.get('ng-packagr')!; const ngPackagrVersion = packageVersionMap.get('ng-packagr')!;
const ngUniversalVersion = packageVersionMap.get('@nguniversal/common')!;
versionUtilContents = versionUtilContents.replace( versionUtilContents = versionUtilContents.replace(
/export const angularVersion = '~.+';/, /export const angularVersion = '~.+';/,
@ -21,10 +20,6 @@ function updateAngularVersionUtils(packageVersionMap: Map<string, string>) {
/export const ngPackagrVersion = '~.+';/, /export const ngPackagrVersion = '~.+';/,
`export const ngPackagrVersion = '~${ngPackagrVersion}';` `export const ngPackagrVersion = '~${ngPackagrVersion}';`
); );
versionUtilContents = versionUtilContents.replace(
/export const ngUniversalVersion = '~.+';/,
`export const ngUniversalVersion = '~${ngUniversalVersion}';`
);
writeFileSync(pathToFile, versionUtilContents); writeFileSync(pathToFile, versionUtilContents);
} }