Skip to content

Commit

Permalink
feat(core): resolve nx migrate target version against registry (#23450)
Browse files Browse the repository at this point in the history
<!-- 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 -->

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

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

Fixes #21418

(cherry picked from commit 8685e10)
  • Loading branch information
leosvelperez authored and FrozenPandaz committed May 21, 2024
1 parent 485bf1b commit c071590
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
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/a@12.3,@myscope/b@1.1.1',
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/workspace@8.12',
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\\workspace@8.12.0',
from: '@myscope\\a@12.3,@myscope\\b@1.1.1',
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

0 comments on commit c071590

Please sign in to comment.