Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): resolve nx migrate target version against registry #23450

Merged
merged 1 commit into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions packages/nx/src/command-line/migrate/migrate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1512,6 +1512,9 @@ describe('Migration', () => {

describe('parseMigrationsOptions', () => {
it('should work for generating migrations', async () => {
jest
.spyOn(packageMgrUtils, 'resolvePackageVersionUsingRegistry')
.mockResolvedValue('12.3.0');
const r = await parseMigrationsOptions({
packageAndVersion: '8.12.0',
from: '@myscope/[email protected],@myscope/[email protected]',
Expand Down Expand Up @@ -1544,8 +1547,8 @@ describe('Migration', () => {
});

it('should handle different variations of the target package', async () => {
jest
.spyOn(packageMgrUtils, 'packageRegistryView')
const packageRegistryViewSpy = jest
.spyOn(packageMgrUtils, 'resolvePackageVersionUsingRegistry')
.mockImplementation((pkg, version) => {
return Promise.resolve(version);
});
Expand All @@ -1555,30 +1558,35 @@ describe('Migration', () => {
targetPackage: '@angular/core',
targetVersion: 'latest',
});
packageRegistryViewSpy.mockResolvedValue('8.12.5');
expect(
await parseMigrationsOptions({ packageAndVersion: '8.12' })
).toMatchObject({
targetPackage: '@nrwl/workspace',
targetVersion: '8.12.0',
targetVersion: '8.12.5',
});
expect(
await parseMigrationsOptions({ packageAndVersion: '8' })
).toMatchObject({
targetPackage: '@nrwl/workspace',
targetVersion: '8.0.0',
targetVersion: '8.12.5',
});
packageRegistryViewSpy.mockResolvedValue('12.6.3');
expect(
await parseMigrationsOptions({ packageAndVersion: '12' })
).toMatchObject({
targetPackage: '@nrwl/workspace',
targetVersion: '12.0.0',
targetVersion: '12.6.3',
});
expect(
await parseMigrationsOptions({ packageAndVersion: '8.12.0-beta.0' })
).toMatchObject({
targetPackage: '@nrwl/workspace',
targetVersion: '8.12.0-beta.0',
});
packageRegistryViewSpy.mockImplementation((pkg, version) =>
Promise.resolve(version)
);
expect(
await parseMigrationsOptions({ packageAndVersion: 'next' })
).toMatchObject({
Expand All @@ -1597,6 +1605,7 @@ describe('Migration', () => {
targetPackage: '@nrwl/workspace',
targetVersion: '13.10.0',
});
packageRegistryViewSpy.mockResolvedValue('8.12.0');
expect(
await parseMigrationsOptions({
packageAndVersion: '@nx/[email protected]',
Expand All @@ -1611,6 +1620,9 @@ describe('Migration', () => {
targetPackage: 'mypackage',
targetVersion: '8.12.0',
});
packageRegistryViewSpy.mockImplementation((pkg, version) =>
Promise.resolve(version)
);
expect(
await parseMigrationsOptions({ packageAndVersion: 'mypackage' })
).toMatchObject({
Expand Down Expand Up @@ -1690,6 +1702,11 @@ describe('Migration', () => {
});

it('should handle backslashes in package names', async () => {
jest
.spyOn(packageMgrUtils, 'resolvePackageVersionUsingRegistry')
.mockImplementation((pkg, version) => {
return Promise.resolve('12.3.0');
});
const r = await parseMigrationsOptions({
packageAndVersion: '@nx\\[email protected]',
from: '@myscope\\[email protected],@myscope\\[email protected]',
Expand Down
5 changes: 3 additions & 2 deletions packages/nx/src/command-line/migrate/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
lt,
lte,
major,
parse,
satisfies,
valid,
} from 'semver';
Expand Down Expand Up @@ -647,9 +648,9 @@ async function normalizeVersionWithTagCheck(
version: string
): Promise<string> {
// This doesn't seem like a valid version, lets check if its a tag on the registry.
if (version && !coerce(version)) {
if (version && !parse(version)) {
try {
return packageRegistryView(pkg, version, 'version');
return resolvePackageVersionUsingRegistry(pkg, version);
} catch {
// fall through to old logic
}
Expand Down
20 changes: 15 additions & 5 deletions packages/nx/src/utils/package-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,11 +306,21 @@ export async function resolvePackageVersionUsingRegistry(
throw new Error(`Unable to resolve version ${packageName}@${version}.`);
}

// get the last line of the output, strip the package version and quotes
const resolvedVersion = result
.split('\n')
.pop()
.split(' ')
const lines = result.split('\n');
if (lines.length === 1) {
return lines[0];
}

/**
* The output contains multiple lines ordered by release date, so the last
* version might not be the last one in the list. We need to sort it. Each
* line looks like:
*
* <package>@<version> '<version>'
*/
const resolvedVersion = lines
.map((line) => line.split(' ')[1])
.sort()
.pop()
.replace(/'/g, '');

Expand Down