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

fix(misc): handle packages rescope in nx init flows #16404

Merged
merged 2 commits into from
Apr 20, 2023
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
32 changes: 7 additions & 25 deletions e2e/nx-init/src/nx-init-react.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,9 @@ describe('nx init (for React)', () => {
expect(craToNxOutput).toContain('🎉 Done!');

const packageJson = readJson('package.json');
expect(
packageJson.devDependencies['@nrwl/jest'] ||
packageJson.devDependencies['@nx/jest']
).toBeDefined();
expect(
packageJson.devDependencies['@nrwl/vite'] ||
packageJson.devDependencies['@nx/vite']
).toBeUndefined();
expect(
packageJson.devDependencies['@nrwl/webpack'] ||
packageJson.devDependencies['@nx/webpack']
).toBeDefined();
expect(packageJson.devDependencies['@nx/jest']).toBeDefined();
expect(packageJson.devDependencies['@nx/vite']).toBeUndefined();
expect(packageJson.devDependencies['@nx/webpack']).toBeDefined();

runCLI(`build ${appName}`, {
env: {
Expand Down Expand Up @@ -77,18 +68,9 @@ describe('nx init (for React)', () => {
expect(craToNxOutput).toContain('🎉 Done!');

const packageJson = readJson('package.json');
expect(
packageJson.devDependencies['@nrwl/jest'] ||
packageJson.devDependencies['@nx/jest']
).toBeUndefined();
expect(
packageJson.devDependencies['@nrwl/vite'] ||
packageJson.devDependencies['@nx/vite']
).toBeDefined();
expect(
packageJson.devDependencies['@nrwl/webpack'] ||
packageJson.devDependencies['@nx/webpack']
).toBeUndefined();
expect(packageJson.devDependencies['@nx/jest']).toBeUndefined();
expect(packageJson.devDependencies['@nx/vite']).toBeDefined();
expect(packageJson.devDependencies['@nx/webpack']).toBeUndefined();

const viteConfig = readFile(`apps/${appName}/vite.config.js`);
expect(viteConfig).toContain('port: 4200'); // default port
Expand Down Expand Up @@ -166,7 +148,7 @@ describe('nx init (for React)', () => {
);

const packageJson = readJson('package.json');
expect(packageJson.devDependencies['@nrwl/jest']).toBeUndefined();
expect(packageJson.devDependencies['@nx/jest']).toBeUndefined();

const viteConfig = readFile(`vite.config.js`);
expect(viteConfig).toContain('port: 4200'); // default port
Expand Down
54 changes: 40 additions & 14 deletions packages/nx/src/nx-init/angular/legacy-angular-versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const nxAngularLegacyVersionMap: Record<number, string> = {};
const minMajorAngularVersionSupported = 14;
// version when the Nx CLI changed from @nrwl/tao & @nrwl/cli to nx
const versionWithConsolidatedPackages = '13.9.0';
// version when packages were rescoped from @nrwl/* to @nx/*
const versionWithRescopeToNx = '16.0.0-beta.2';

export async function getLegacyMigrationFunctionIfApplicable(
repoRoot: string,
Expand All @@ -36,10 +38,14 @@ export async function getLegacyMigrationFunctionIfApplicable(

let legacyMigrationCommand: string;
let pkgName: string;
let unscopedPkgName: string;
let pkgScope: string;
let pkgVersion: string;
if (majorAngularVersion < 13) {
// for versions lower than 13, the migration was in @nrwl/workspace:ng-add
pkgName = '@nrwl/workspace';
pkgScope = '@nrwl';
unscopedPkgName = 'workspace';
pkgName = `${pkgScope}/${unscopedPkgName}`;
pkgVersion = await resolvePackageVersion(
pkgName,
`^${majorAngularVersion}.0.0`
Expand All @@ -50,19 +56,25 @@ export async function getLegacyMigrationFunctionIfApplicable(
legacyMigrationCommand = `ng g ${pkgName}:ng-add ${preserveAngularCliLayoutFlag}`;
} else if (majorAngularVersion < 14) {
// for v13, the migration was in @nrwl/angular:ng-add
pkgName = '@nrwl/angular';
pkgScope = '@nrwl';
unscopedPkgName = 'angular';
pkgName = `${pkgScope}/${unscopedPkgName}`;
pkgVersion = await resolvePackageVersion(pkgName, '~14.1.0');
const preserveAngularCliLayoutFlag = !isIntegratedMigration
? '--preserve-angular-cli-layout'
: '--preserve-angular-cli-layout=false';
legacyMigrationCommand = `ng g ${pkgName}:ng-add ${preserveAngularCliLayoutFlag}`;
} else {
// use the latest Nx version that supported the Angular version
pkgName = '@nrwl/angular';
pkgVersion = await resolvePackageVersion(
'nx',
nxAngularLegacyVersionMap[majorAngularVersion]
);

pkgScope = gte(pkgVersion, versionWithRescopeToNx) ? '@nx' : '@nrwl';
unscopedPkgName = 'angular';
pkgName = `${pkgScope}/${unscopedPkgName}`;

legacyMigrationCommand = `nx@${pkgVersion} init ${process.argv
.slice(2)
.join(' ')}`;
Expand All @@ -74,7 +86,17 @@ export async function getLegacyMigrationFunctionIfApplicable(

output.log({ title: '📦 Installing dependencies' });
const pmc = getPackageManagerCommand();
await installDependencies(repoRoot, pkgName, pkgVersion, useNxCloud, pmc);
await installDependencies(
repoRoot,
{
pkgName,
pkgScope,
pkgVersion,
unscopedPkgName,
},
useNxCloud,
pmc
);

output.log({ title: '📝 Setting up workspace' });
execSync(`${pmc.exec} ${legacyMigrationCommand}`, { stdio: [0, 1, 2] });
Expand All @@ -95,36 +117,40 @@ export async function getLegacyMigrationFunctionIfApplicable(

async function installDependencies(
repoRoot: string,
pkgName: string,
pkgVersion: string,
pkgInfo: {
pkgName: string;
pkgScope: string;
pkgVersion: string;
unscopedPkgName: string;
},
useNxCloud: boolean,
pmc: PackageManagerCommands
): Promise<void> {
const json = readJsonFile(join(repoRoot, 'package.json'));

json.devDependencies ??= {};
json.devDependencies['@nrwl/workspace'] = pkgVersion;
json.devDependencies[`${pkgInfo.pkgScope}/workspace`] = pkgInfo.pkgVersion;

if (gte(pkgVersion, versionWithConsolidatedPackages)) {
json.devDependencies['nx'] = pkgVersion;
if (gte(pkgInfo.pkgVersion, versionWithConsolidatedPackages)) {
json.devDependencies['nx'] = pkgInfo.pkgVersion;
} else {
json.devDependencies['@nrwl/cli'] = pkgVersion;
json.devDependencies['@nrwl/tao'] = pkgVersion;
json.devDependencies[`${pkgInfo.pkgScope}/cli`] = pkgInfo.pkgVersion;
json.devDependencies[`${pkgInfo.pkgScope}/tao`] = pkgInfo.pkgVersion;
}

if (useNxCloud) {
// get the latest nx-cloud version compatible with the Nx major
// version being installed
json.devDependencies['nx-cloud'] = await resolvePackageVersion(
'nx-cloud',
`^${major(pkgVersion)}.0.0`
`^${major(pkgInfo.pkgVersion)}.0.0`
);
}
json.devDependencies = sortObjectByKeys(json.devDependencies);

if (pkgName === '@nrwl/angular') {
if (pkgInfo.unscopedPkgName === 'angular') {
json.dependencies ??= {};
json.dependencies['@nrwl/angular'] = pkgVersion;
json.dependencies[pkgInfo.pkgName] = pkgInfo.pkgVersion;
json.dependencies = sortObjectByKeys(json.dependencies);
}
writeJsonFile(`package.json`, json);
Expand Down
8 changes: 1 addition & 7 deletions packages/nx/src/nx-init/react/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,13 +317,7 @@ function cleanUpUnusedFilesAndAddConfigFiles(options: NormalizedOptions) {
setupE2eProject(options.reactAppName);
} else {
removeSync(join('apps', `${options.reactAppName}-e2e`));
const packageJson = readJsonFile('package.json');
const cypressPkgName =
packageJson.devDependencies?.['@nrwl/cypress'] ||
packageJson.dependencies?.['@nrwl/cypress']
? '@nrwl/cypress'
: '@nx/cypress';
execSync(`${options.pmc.rm} ${cypressPkgName} eslint-plugin-cypress`);
execSync(`${options.pmc.rm} @nx/cypress eslint-plugin-cypress`);
}

if (options.isStandalone) {
Expand Down