From 6025ce57ae742ed541cf420c6b35a5a7b1166f23 Mon Sep 17 00:00:00 2001 From: Craigory Coppola Date: Mon, 9 May 2022 20:06:20 -0400 Subject: [PATCH] fix(core): migrate should not fail under certain circumstances (#10225) Closes #10144 --- packages/nx/src/command-line/migrate.ts | 42 ++++++++++++++++--------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/packages/nx/src/command-line/migrate.ts b/packages/nx/src/command-line/migrate.ts index fccd20a681dff..51295cafd593e 100644 --- a/packages/nx/src/command-line/migrate.ts +++ b/packages/nx/src/command-line/migrate.ts @@ -804,6 +804,15 @@ async function isMigratingToNewMajor(from: string, to: string) { return major(from) < major(to); } +function readNxVersion(packageJson: PackageJson) { + return ( + packageJson?.devDependencies?.['nx'] ?? + packageJson?.dependencies?.['nx'] ?? + packageJson?.devDependencies?.['@nrwl/workspace'] ?? + packageJson?.dependencies?.['@nrwl/workspace'] + ); +} + async function generateMigrationsJsonAndUpdatePackageJson( root: string, opts: { @@ -818,20 +827,25 @@ async function generateMigrationsJsonAndUpdatePackageJson( let originalPackageJson = readJsonFile( join(root, 'package.json') ); - if ( - ['nx', '@nrwl/workspace'].includes(opts.targetPackage) && - (await isMigratingToNewMajor( - originalPackageJson.devDependencies?.['nx'] ?? - originalPackageJson.devDependencies?.['@nrwl/workspace'], - opts.targetVersion - )) - ) { - await connectToNxCloudCommand( - 'We noticed you are migrating to a new major version, but are not taking advantage of Nx Cloud. Nx Cloud can make your CI up to 10 times faster. Learn more about it here: nx.app. Would you like to add it?' - ); - originalPackageJson = readJsonFile( - join(root, 'package.json') - ); + + try { + if ( + ['nx', '@nrwl/workspace'].includes(opts.targetPackage) && + (await isMigratingToNewMajor( + readNxVersion(originalPackageJson), + opts.targetVersion + )) + ) { + await connectToNxCloudCommand( + 'We noticed you are migrating to a new major version, but are not taking advantage of Nx Cloud. Nx Cloud can make your CI up to 10 times faster. Learn more about it here: nx.app. Would you like to add it?' + ); + originalPackageJson = readJsonFile( + join(root, 'package.json') + ); + } + } catch { + // The above code is to remind folks when updating to a new major and not currently using Nx cloud. + // If for some reason it fails, it shouldn't affect the overall migration process } logger.info(`Fetching meta data about packages.`);