chore(angular): improve angular updater script helper (#20532)
This commit is contained in:
parent
4c17667c84
commit
68d0f792e8
@ -15,7 +15,6 @@ async function addMigrationPackageGroup(
|
||||
) {
|
||||
angularPackageMigrations.packageJsonUpdates[targetNxVersion] = {
|
||||
version: `${targetNxMigrationVersion}`,
|
||||
packages: {},
|
||||
};
|
||||
|
||||
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 (
|
||||
pkgName.startsWith('@angular/') &&
|
||||
!['@angular/core', '@angular/material', '@angular/cdk'].includes(pkgName)
|
||||
![
|
||||
'@angular/core',
|
||||
'@angular/material',
|
||||
'@angular/cdk',
|
||||
'@angular/ssr',
|
||||
].includes(pkgName)
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -1,24 +1,35 @@
|
||||
import axios from 'axios';
|
||||
import { coerce, gt, SemVer } from 'semver';
|
||||
|
||||
const packagesToUpdate = [
|
||||
'@angular-devkit/architect',
|
||||
'@angular-devkit/build-angular',
|
||||
'@angular-devkit/build-webpack',
|
||||
'@angular-devkit/core',
|
||||
'@angular-devkit/schematics',
|
||||
'@angular/cli',
|
||||
'@angular/common',
|
||||
'@angular/compiler',
|
||||
'@angular/compiler-cli',
|
||||
'@angular/core',
|
||||
'@angular/router',
|
||||
'@angular/material',
|
||||
'@angular/cdk',
|
||||
'@nguniversal/builders',
|
||||
'@nguniversal/common',
|
||||
'@nguniversal/express-engine',
|
||||
'@schematics/angular',
|
||||
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/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',
|
||||
];
|
||||
|
||||
@ -26,25 +37,54 @@ export async function fetchVersionsFromRegistry(
|
||||
targetVersion: 'latest' | 'next'
|
||||
) {
|
||||
console.log('⏳ - Fetching versions from registry...');
|
||||
|
||||
const packageVersionMap = new Map<string, string>();
|
||||
for (const pkgName of packagesToUpdate) {
|
||||
const response = await axios.get(`https://registry.npmjs.org/${pkgName}`);
|
||||
const distTags = response.data['dist-tags'];
|
||||
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}`);
|
||||
}
|
||||
}
|
||||
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}`);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,34 +1,18 @@
|
||||
import { readFileSync, writeFileSync } from 'fs';
|
||||
|
||||
function updateRootPackageJson(packageVersionMap: Map<string, string>) {
|
||||
const pathToPkgJson = 'package.json';
|
||||
function updatePackageJson(
|
||||
pathToPkgJson: string,
|
||||
packageVersionMap: Map<string, string>
|
||||
) {
|
||||
const pkgJson = JSON.parse(
|
||||
readFileSync(pathToPkgJson, { encoding: 'utf-8' })
|
||||
);
|
||||
|
||||
for (const [pkgName, version] of packageVersionMap.entries()) {
|
||||
if (pkgJson.devDependencies && pkgJson.devDependencies[pkgName]) {
|
||||
if (pkgJson.devDependencies?.[pkgName]) {
|
||||
pkgJson.devDependencies[pkgName] = `~${version}`;
|
||||
}
|
||||
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]) {
|
||||
if (pkgJson.dependencies?.[pkgName]) {
|
||||
pkgJson.dependencies[pkgName] = `~${version}`;
|
||||
}
|
||||
}
|
||||
@ -40,7 +24,7 @@ export async function updatePackageJsonForAngular(
|
||||
packageVersionMap: Map<string, string>
|
||||
) {
|
||||
console.log('⏳ - Writing package.json files...');
|
||||
updateRootPackageJson(packageVersionMap);
|
||||
updateAngularPackageJson(packageVersionMap);
|
||||
updatePackageJson('package.json', packageVersionMap);
|
||||
updatePackageJson('packages/angular/package.json', packageVersionMap);
|
||||
console.log('✅ - Wrote package.json files');
|
||||
}
|
||||
|
||||
@ -7,7 +7,6 @@ function updateAngularVersionUtils(packageVersionMap: Map<string, string>) {
|
||||
const angularVersion = packageVersionMap.get('@angular/core')!;
|
||||
const angularDevkitVersion = packageVersionMap.get('@angular/cli')!;
|
||||
const ngPackagrVersion = packageVersionMap.get('ng-packagr')!;
|
||||
const ngUniversalVersion = packageVersionMap.get('@nguniversal/common')!;
|
||||
|
||||
versionUtilContents = versionUtilContents.replace(
|
||||
/export const angularVersion = '~.+';/,
|
||||
@ -21,10 +20,6 @@ function updateAngularVersionUtils(packageVersionMap: Map<string, string>) {
|
||||
/export const ngPackagrVersion = '~.+';/,
|
||||
`export const ngPackagrVersion = '~${ngPackagrVersion}';`
|
||||
);
|
||||
versionUtilContents = versionUtilContents.replace(
|
||||
/export const ngUniversalVersion = '~.+';/,
|
||||
`export const ngUniversalVersion = '~${ngUniversalVersion}';`
|
||||
);
|
||||
|
||||
writeFileSync(pathToFile, versionUtilContents);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user