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): respect packageGroup in nx migrate #9667

Merged
merged 13 commits into from
Apr 8, 2022
4 changes: 2 additions & 2 deletions e2e/cli/src/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { packagesWeCareAbout } from 'nx/src/command-line/report';
describe('Cli', () => {
beforeEach(() => newProject());

it('vvvshould execute long running tasks', async () => {
it('should execute long running tasks', async () => {
const myapp = uniq('myapp');
runCLI(`generate @nrwl/web:app ${myapp}`);
updateProjectConfig(myapp, (c) => {
Expand Down Expand Up @@ -156,7 +156,7 @@ describe('list', () => {
describe('migrate', () => {
beforeEach(() => newProject());

it('clear-cacheshould run migrations', () => {
it('should run migrations', () => {
updateFile(
`./node_modules/migrate-parent-package/package.json`,
JSON.stringify({
Expand Down
32 changes: 17 additions & 15 deletions packages/make-angular-cli-faster/src/utilities/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ export async function determineMigration(
): Promise<MigrationDefinition> {
const angularVersion = getInstalledAngularVersion();
const majorAngularVersion = major(angularVersion);
latestWorkspaceVersionWithMigration = resolvePackageVersion(
latestWorkspaceVersionWithMigration = await resolvePackageVersion(
'@nrwl/angular',
latestWorkspaceRangeVersionWithMigration
);

if (version) {
const normalizedVersion = normalizeVersion(version);
const normalizedVersion = await normalizeVersion(version);
if (lte(normalizedVersion, latestWorkspaceVersionWithMigration)) {
// specified version should use @nrwl/workspace:ng-add
return { packageName: '@nrwl/workspace', version: normalizedVersion };
Expand Down Expand Up @@ -66,10 +66,11 @@ export async function determineMigration(
);
}

const latestNxCompatibleVersion = getNxVersionBasedOnInstalledAngularVersion(
angularVersion,
majorAngularVersion
);
const latestNxCompatibleVersion =
await getNxVersionBasedOnInstalledAngularVersion(
angularVersion,
majorAngularVersion
);

// should use @nrwl/workspace:ng-add if the version is less than the
// latest workspace version that has the migration, otherwise use
Expand Down Expand Up @@ -105,10 +106,11 @@ async function findAndSuggestVersionToUse(
majorAngularVersion: number,
userSpecifiedVersion: string
): Promise<MigrationDefinition> {
const latestNxCompatibleVersion = getNxVersionBasedOnInstalledAngularVersion(
angularVersion,
majorAngularVersion
);
const latestNxCompatibleVersion =
await getNxVersionBasedOnInstalledAngularVersion(
angularVersion,
majorAngularVersion
);
const useSuggestedVersion = await promptForVersion(latestNxCompatibleVersion);
if (useSuggestedVersion) {
// should use @nrwl/workspace:ng-add if the version is less than the
Expand All @@ -134,10 +136,10 @@ async function findAndSuggestVersionToUse(
process.exit(1);
}

function getNxVersionBasedOnInstalledAngularVersion(
async function getNxVersionBasedOnInstalledAngularVersion(
angularVersion: string,
majorAngularVersion: number
): string {
): Promise<string> {
if (lt(angularVersion, '13.0.0')) {
// the @nrwl/angular:ng-add generator is only available for versions supporting
// Angular >= 13.0.0, fall back to @nrwl/workspace:ng-add
Expand All @@ -154,7 +156,7 @@ function getNxVersionBasedOnInstalledAngularVersion(
}

// use latest, only the last version in the map should not contain a max
return resolvePackageVersion('@nrwl/angular', 'latest');
return await resolvePackageVersion('@nrwl/angular', 'latest');
}

async function promptForVersion(version: string): Promise<boolean> {
Expand All @@ -177,13 +179,13 @@ function getInstalledAngularVersion(): string {
return readJsonFile(packageJsonPath).version;
}

function normalizeVersion(version: string): string {
async function normalizeVersion(version: string): Promise<string> {
if (
version.startsWith('^') ||
version.startsWith('~') ||
version.split('.').length < 3
) {
return resolvePackageVersion('@nrwl/angular', version);
return await resolvePackageVersion('@nrwl/angular', version);
}

return version;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ export function installDependencies(
});
}

export function resolvePackageVersion(
export async function resolvePackageVersion(
packageName: string,
version: string
): string {
): Promise<string> {
try {
return resolvePackageVersionUsingRegistry(packageName, version);
return await resolvePackageVersionUsingRegistry(packageName, version);
} catch {
return resolvePackageVersionUsingInstallation(packageName, version);
return await resolvePackageVersionUsingInstallation(packageName, version);
}
}
23 changes: 3 additions & 20 deletions packages/nx/src/command-line/migrate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,19 @@ describe('Migration', () => {
fetch: (_p, _v) => {
throw new Error('cannot fetch');
},
from: {},
to: {},
});

try {
await migrator.updatePackageJson('mypackage', 'myversion');
throw new Error('fail');
} catch (e) {
expect(e.message).toEqual(`cannot fetch`);
}
await expect(
migrator.updatePackageJson('mypackage', 'myversion')
).rejects.toThrowError(/cannot fetch/);
});

it('should return a patch to the new version', async () => {
const migrator = new Migrator({
packageJson: {},
versions: () => '1.0.0',
fetch: (_p, _v) => Promise.resolve({ version: '2.0.0' }),
from: {},
to: {},
});

Expand Down Expand Up @@ -68,7 +63,6 @@ describe('Migration', () => {
return Promise.resolve(null);
}
},
from: {},
to: {},
});

Expand Down Expand Up @@ -108,7 +102,6 @@ describe('Migration', () => {
return Promise.resolve(null);
}
},
from: {},
to: {},
});

Expand Down Expand Up @@ -157,7 +150,6 @@ describe('Migration', () => {
return Promise.resolve(null);
}
},
from: {},
to: {},
});

Expand Down Expand Up @@ -225,7 +217,6 @@ describe('Migration', () => {
return Promise.resolve({ version: '4.0.0' });
}
},
from: {},
to: {},
});

Expand Down Expand Up @@ -275,7 +266,6 @@ describe('Migration', () => {
return Promise.resolve({ version: '2.0.0' });
}
},
from: {},
to: {},
});

Expand Down Expand Up @@ -318,7 +308,6 @@ describe('Migration', () => {
return Promise.resolve(null);
}
},
from: {},
to: {},
});

Expand Down Expand Up @@ -359,7 +348,6 @@ describe('Migration', () => {
},
versions: () => '1.0.0',
fetch: (_p, _v) => Promise.resolve({ version: '2.0.0' }),
from: {},
to: {},
});

Expand Down Expand Up @@ -402,7 +390,6 @@ describe('Migration', () => {
version: '2.0.0',
packageJsonUpdates: { one: { version: '2.0.0', packages: {} } },
}),
from: {},
to: {},
});
await migrator.updatePackageJson('@nrwl/workspace', '2.0.0');
Expand All @@ -421,7 +408,6 @@ describe('Migration', () => {
packageJsonUpdates: { one: { version: '2.0.0', packages: {} } },
});
},
from: {},
to: {},
});
await migrator.updatePackageJson('@nrwl/workspace', '2.0.0');
Expand Down Expand Up @@ -455,7 +441,6 @@ describe('Migration', () => {
throw new Error('Boom');
}
},
from: {},
to: {},
});

Expand Down Expand Up @@ -518,7 +503,6 @@ describe('Migration', () => {
return Promise.resolve(null);
}
},
from: {},
to: {},
});
expect(await migrator.updatePackageJson('parent', '2.0.0')).toEqual({
Expand Down Expand Up @@ -599,7 +583,6 @@ describe('Migration', () => {
return Promise.resolve(null);
}
},
from: {},
to: {},
});

Expand Down
Loading