chore(angular): improve script to scaffold new angular version support (#16938)
This commit is contained in:
parent
09525e8b30
commit
279154436a
@ -1,11 +1,13 @@
|
|||||||
|
import axios from 'axios';
|
||||||
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
|
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
|
||||||
|
import { join } from 'path';
|
||||||
|
import { gt, major, minor, parse } from 'semver';
|
||||||
import {
|
import {
|
||||||
getAngularCliMigrationGenerator,
|
getAngularCliMigrationGenerator,
|
||||||
getAngularCliMigrationGeneratorSpec,
|
getAngularCliMigrationGeneratorSpec,
|
||||||
} from './files/angular-cli-upgrade-migration';
|
} from './files/angular-cli-upgrade-migration';
|
||||||
import { join } from 'path';
|
|
||||||
|
|
||||||
function addMigrationPackageGroup(
|
async function addMigrationPackageGroup(
|
||||||
angularPackageMigrations: Record<string, any>,
|
angularPackageMigrations: Record<string, any>,
|
||||||
targetNxVersion: string,
|
targetNxVersion: string,
|
||||||
targetNxMigrationVersion: string,
|
targetNxMigrationVersion: string,
|
||||||
@ -16,6 +18,24 @@ function addMigrationPackageGroup(
|
|||||||
packages: {},
|
packages: {},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const promptAndRequirements = await getPromptAndRequiredVersions(
|
||||||
|
packageVersionMap
|
||||||
|
);
|
||||||
|
if (!promptAndRequirements) {
|
||||||
|
console.warn(
|
||||||
|
'❗️ - The `@angular/core` latest version is greater than the next version. Skipping generating migration prompt and requirements.\n' +
|
||||||
|
' Please review the migrations and manually add the prompt and requirements if needed.'
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
angularPackageMigrations.packageJsonUpdates[targetNxVersion][
|
||||||
|
'x-prompt'
|
||||||
|
] = `Do you want to update the Angular version to v${promptAndRequirements.promptVersion}?`;
|
||||||
|
angularPackageMigrations.packageJsonUpdates[targetNxVersion].requires = {
|
||||||
|
'@angular/core': promptAndRequirements.angularCoreRequirement,
|
||||||
|
typescript: promptAndRequirements.typescriptRequirement,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
for (const [pkgName, version] of packageVersionMap.entries()) {
|
for (const [pkgName, version] of packageVersionMap.entries()) {
|
||||||
if (
|
if (
|
||||||
pkgName.startsWith('@angular/') &&
|
pkgName.startsWith('@angular/') &&
|
||||||
@ -33,7 +53,47 @@ function addMigrationPackageGroup(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function buildMigrations(
|
async function getPromptAndRequiredVersions(
|
||||||
|
packageVersionMap: Map<string, string>
|
||||||
|
): Promise<{
|
||||||
|
angularCoreRequirement: string;
|
||||||
|
promptVersion: string;
|
||||||
|
typescriptRequirement: string;
|
||||||
|
} | null> {
|
||||||
|
// @angular/core
|
||||||
|
const angularCoreMetadata = await axios.get(
|
||||||
|
'https://registry.npmjs.org/@angular/core'
|
||||||
|
);
|
||||||
|
const { latest, next } = angularCoreMetadata.data['dist-tags'];
|
||||||
|
if (gt(latest, next)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
const angularCoreRequirement = `>=${major(latest)}.${minor(
|
||||||
|
latest
|
||||||
|
)}.0 <${next}`;
|
||||||
|
|
||||||
|
// prompt version (e.g. v16 or v16.1)
|
||||||
|
const angularCoreVersion = packageVersionMap.get('@angular/core');
|
||||||
|
const { major: majorVersion, minor: minorVersion } =
|
||||||
|
parse(angularCoreVersion)!;
|
||||||
|
const promptVersion = `v${majorVersion}${
|
||||||
|
minorVersion !== 0 ? `.${minorVersion}` : ''
|
||||||
|
}`;
|
||||||
|
|
||||||
|
// typescript
|
||||||
|
const angularCompilerCliVersion = packageVersionMap.get(
|
||||||
|
'@angular/compiler-cli'
|
||||||
|
);
|
||||||
|
const angularCompilerCliMetadata = await axios.get(
|
||||||
|
`https://registry.npmjs.org/@angular/compiler-cli/${angularCompilerCliVersion}`
|
||||||
|
);
|
||||||
|
const typescriptRequirement =
|
||||||
|
angularCompilerCliMetadata.data.peerDependencies.typescript;
|
||||||
|
|
||||||
|
return { angularCoreRequirement, promptVersion, typescriptRequirement };
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function buildMigrations(
|
||||||
packageVersionMap: Map<string, string>,
|
packageVersionMap: Map<string, string>,
|
||||||
targetNxVersion: string,
|
targetNxVersion: string,
|
||||||
targetNxMigrationVersion: string
|
targetNxMigrationVersion: string
|
||||||
@ -44,7 +104,7 @@ export function buildMigrations(
|
|||||||
readFileSync(pathToMigrationsJsonFile, { encoding: 'utf-8' })
|
readFileSync(pathToMigrationsJsonFile, { encoding: 'utf-8' })
|
||||||
);
|
);
|
||||||
|
|
||||||
addMigrationPackageGroup(
|
await addMigrationPackageGroup(
|
||||||
angularPackageMigrations,
|
angularPackageMigrations,
|
||||||
targetNxVersion,
|
targetNxVersion,
|
||||||
targetNxMigrationVersion,
|
targetNxMigrationVersion,
|
||||||
@ -55,7 +115,7 @@ export function buildMigrations(
|
|||||||
const angularCliMigrationGeneratorContents =
|
const angularCliMigrationGeneratorContents =
|
||||||
getAngularCliMigrationGenerator(angularCLIVersion);
|
getAngularCliMigrationGenerator(angularCLIVersion);
|
||||||
const angularCliMigrationGeneratorSpecContents =
|
const angularCliMigrationGeneratorSpecContents =
|
||||||
getAngularCliMigrationGeneratorSpec(angularCLIVersion);
|
getAngularCliMigrationGeneratorSpec();
|
||||||
|
|
||||||
// Create the directory update-targetNxVersion.dasherize()
|
// Create the directory update-targetNxVersion.dasherize()
|
||||||
// Write the generator
|
// Write the generator
|
||||||
@ -69,9 +129,13 @@ export function buildMigrations(
|
|||||||
'-'
|
'-'
|
||||||
)}`;
|
)}`;
|
||||||
|
|
||||||
angularPackageMigrations.schematics[generatorName] = {
|
const angularCoreVersion = packageVersionMap.get('@angular/core');
|
||||||
|
angularPackageMigrations.generators[generatorName] = {
|
||||||
cli: 'nx',
|
cli: 'nx',
|
||||||
version: targetNxMigrationVersion,
|
version: targetNxMigrationVersion,
|
||||||
|
requires: {
|
||||||
|
'@angular/core': `>=${angularCoreVersion}`,
|
||||||
|
},
|
||||||
description: `Update the @angular/cli package version to ~${angularCLIVersion}.`,
|
description: `Update the @angular/cli package version to ~${angularCLIVersion}.`,
|
||||||
factory: `./src/migrations/${migrationGeneratorFolderName}/${migrationFileName}`,
|
factory: `./src/migrations/${migrationGeneratorFolderName}/${migrationFileName}`,
|
||||||
};
|
};
|
||||||
|
|||||||
@ -15,6 +15,9 @@ const packagesToUpdate = [
|
|||||||
'@angular/router',
|
'@angular/router',
|
||||||
'@angular/material',
|
'@angular/material',
|
||||||
'@angular/cdk',
|
'@angular/cdk',
|
||||||
|
'@nguniversal/builders',
|
||||||
|
'@nguniversal/common',
|
||||||
|
'@nguniversal/express-engine',
|
||||||
'@schematics/angular',
|
'@schematics/angular',
|
||||||
'ng-packagr',
|
'ng-packagr',
|
||||||
];
|
];
|
||||||
@ -30,6 +33,7 @@ export async function fetchVersionsFromRegistry(
|
|||||||
const latestVersion = distTags['latest'];
|
const latestVersion = distTags['latest'];
|
||||||
if (targetVersion === 'latest') {
|
if (targetVersion === 'latest') {
|
||||||
packageVersionMap.set(pkgName, latestVersion);
|
packageVersionMap.set(pkgName, latestVersion);
|
||||||
|
console.log(` ${pkgName}: ${latestVersion}`);
|
||||||
} else {
|
} else {
|
||||||
const nextVersion = distTags['next'];
|
const nextVersion = distTags['next'];
|
||||||
const coercedNextVersion = coerce(nextVersion) as SemVer;
|
const coercedNextVersion = coerce(nextVersion) as SemVer;
|
||||||
@ -38,12 +42,9 @@ export async function fetchVersionsFromRegistry(
|
|||||||
? nextVersion
|
? nextVersion
|
||||||
: latestVersion;
|
: latestVersion;
|
||||||
packageVersionMap.set(pkgName, versionToUse);
|
packageVersionMap.set(pkgName, versionToUse);
|
||||||
|
console.log(` ${pkgName}: ${versionToUse}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
console.log(
|
console.log('✅ - Finished fetching versions from registry');
|
||||||
`✅ - Fetched versions from registry (${packageVersionMap.get(
|
|
||||||
'@angular/cli'
|
|
||||||
)})`
|
|
||||||
);
|
|
||||||
return packageVersionMap;
|
return packageVersionMap;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,7 +2,7 @@ export const getAngularCliMigrationGenerator = (
|
|||||||
version: string
|
version: string
|
||||||
) => `import { formatFiles, Tree, updateJson } from '@nx/devkit';
|
) => `import { formatFiles, Tree, updateJson } from '@nx/devkit';
|
||||||
|
|
||||||
const angularCliVersion = '~${version}';
|
export const angularCliVersion = '~${version}';
|
||||||
|
|
||||||
export default async function (tree: Tree) {
|
export default async function (tree: Tree) {
|
||||||
let shouldFormat = false;
|
let shouldFormat = false;
|
||||||
@ -25,11 +25,10 @@ export default async function (tree: Tree) {
|
|||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
export const getAngularCliMigrationGeneratorSpec = (
|
export const getAngularCliMigrationGeneratorSpec =
|
||||||
version: string
|
() => `import { readJson, Tree, writeJson } from '@nx/devkit';
|
||||||
) => `import { readJson, Tree, writeJson } from '@nx/devkit';
|
|
||||||
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
|
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
|
||||||
import updateAngularCli from './update-angular-cli';
|
import updateAngularCli, { angularCliVersion } from './update-angular-cli';
|
||||||
|
|
||||||
describe('update-angular-cli migration', () => {
|
describe('update-angular-cli migration', () => {
|
||||||
let tree: Tree;
|
let tree: Tree;
|
||||||
@ -46,7 +45,7 @@ describe('update-angular-cli migration', () => {
|
|||||||
await updateAngularCli(tree);
|
await updateAngularCli(tree);
|
||||||
|
|
||||||
const { devDependencies } = readJson(tree, 'package.json');
|
const { devDependencies } = readJson(tree, 'package.json');
|
||||||
expect(devDependencies['@angular/cli']).toEqual('~${version}');
|
expect(devDependencies['@angular/cli']).toBe(angularCliVersion);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should update @angular/cli version when defined as a dependency', async () => {
|
it('should update @angular/cli version when defined as a dependency', async () => {
|
||||||
@ -57,10 +56,10 @@ describe('update-angular-cli migration', () => {
|
|||||||
await updateAngularCli(tree);
|
await updateAngularCli(tree);
|
||||||
|
|
||||||
const { dependencies } = readJson(tree, 'package.json');
|
const { dependencies } = readJson(tree, 'package.json');
|
||||||
expect(dependencies['@angular/cli']).toEqual('~${version}');
|
expect(dependencies['@angular/cli']).toBe(angularCliVersion);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should add @angular/cli to package.json when it is not set', async () => {
|
it('should not add @angular/cli to package.json when it is not set', async () => {
|
||||||
const initialPackageJson = readJson(tree, 'package.json');
|
const initialPackageJson = readJson(tree, 'package.json');
|
||||||
|
|
||||||
await updateAngularCli(tree);
|
await updateAngularCli(tree);
|
||||||
|
|||||||
@ -9,9 +9,10 @@
|
|||||||
* npx ts-node scripts/angular-support-upgrades/init-upgrade.ts --angularVersion=next --targetNxVersion=15.5.0 --targetNxMigrationVersion=15.5.0-beta.0
|
* npx ts-node scripts/angular-support-upgrades/init-upgrade.ts --angularVersion=next --targetNxVersion=15.5.0 --targetNxMigrationVersion=15.5.0-beta.0
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
import { execSync } from 'child_process';
|
||||||
|
import { buildMigrations } from './build-migrations';
|
||||||
import { fetchVersionsFromRegistry } from './fetch-versions-from-registry';
|
import { fetchVersionsFromRegistry } from './fetch-versions-from-registry';
|
||||||
import { updatePackageJsonForAngular } from './update-package-jsons';
|
import { updatePackageJsonForAngular } from './update-package-jsons';
|
||||||
import { buildMigrations } from './build-migrations';
|
|
||||||
import { updateVersionUtils } from './update-version-utils';
|
import { updateVersionUtils } from './update-version-utils';
|
||||||
|
|
||||||
const yargs = require('yargs/yargs');
|
const yargs = require('yargs/yargs');
|
||||||
@ -33,12 +34,20 @@ async function run() {
|
|||||||
argv.angularVersion
|
argv.angularVersion
|
||||||
);
|
);
|
||||||
await updatePackageJsonForAngular(packageVersionMap);
|
await updatePackageJsonForAngular(packageVersionMap);
|
||||||
buildMigrations(
|
await buildMigrations(
|
||||||
packageVersionMap,
|
packageVersionMap,
|
||||||
argv.targetNxVersion,
|
argv.targetNxVersion,
|
||||||
argv.targetNxMigrationVersion
|
argv.targetNxMigrationVersion
|
||||||
);
|
);
|
||||||
updateVersionUtils(packageVersionMap);
|
updateVersionUtils(packageVersionMap);
|
||||||
|
|
||||||
|
console.log('⏳ - Formatting files...');
|
||||||
|
execSync('pnpm nx format', { stdio: 'inherit', encoding: 'utf8' });
|
||||||
|
console.log('✅ - Finished formatting files');
|
||||||
|
|
||||||
|
console.log('⏳ - Installing packages...');
|
||||||
|
execSync('pnpm install', { stdio: 'inherit', encoding: 'utf8' });
|
||||||
|
console.log('✅ - Finished creating migrations!');
|
||||||
}
|
}
|
||||||
|
|
||||||
run();
|
run();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user