feat(misc): repair should support running extra migrations (#11504)

This commit is contained in:
Victor Savkin 2022-08-09 13:52:28 -04:00 committed by GitHub
parent 520e5852f7
commit efea1a511e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 66 additions and 50 deletions

View File

@ -20,8 +20,8 @@ import {
NxMigrationsConfiguration, NxMigrationsConfiguration,
PackageGroup, PackageGroup,
PackageJson, PackageJson,
readNxMigrateConfig,
readModulePackageJson, readModulePackageJson,
readNxMigrateConfig,
} from '../utils/package-json'; } from '../utils/package-json';
import { import {
createTempNpmDirectory, createTempNpmDirectory,
@ -33,6 +33,7 @@ import {
} from '../utils/package-manager'; } from '../utils/package-manager';
import { handleErrors } from '../utils/params'; import { handleErrors } from '../utils/params';
import { connectToNxCloudCommand } from './connect-to-nx-cloud'; import { connectToNxCloudCommand } from './connect-to-nx-cloud';
import { output } from '../utils/output';
export interface ResolvedMigrationConfiguration extends MigrationsJson { export interface ResolvedMigrationConfiguration extends MigrationsJson {
packageGroup?: NxMigrationsConfiguration['packageGroup']; packageGroup?: NxMigrationsConfiguration['packageGroup'];
@ -539,9 +540,11 @@ function createFetcher() {
let resolvedVersion: string = packageVersion; let resolvedVersion: string = packageVersion;
let migrations: Promise<ResolvedMigrationConfiguration>; let migrations: Promise<ResolvedMigrationConfiguration>;
function setCache(packageName: string, packageVersion: string) { function setCache(packageName: string, packageVersion: string) {
migrationsCache[packageName + '-' + packageVersion] = migrations; migrationsCache[packageName + '-' + packageVersion] = migrations;
} }
migrations = fetchMigrations(packageName, packageVersion, setCache).then( migrations = fetchMigrations(packageName, packageVersion, setCache).then(
(result) => { (result) => {
if (result.schematics) { if (result.schematics) {
@ -558,6 +561,7 @@ function createFetcher() {
return migrations; return migrations;
}; };
} }
// testing-fetch-end // testing-fetch-end
async function getPackageMigrationsUsingRegistry( async function getPackageMigrationsUsingRegistry(
@ -843,32 +847,38 @@ async function generateMigrationsJsonAndUpdatePackageJson(
createMigrationsFile(root, migrations); createMigrationsFile(root, migrations);
} }
logger.info(`NX The migrate command has run successfully.`); output.success({
logger.info(`- package.json has been updated`); title: `The migrate command has run successfully.`,
if (migrations.length > 0) { bodyLines: [
logger.info(`- migrations.json has been generated`); `- Package.json has been updated.`,
} else { migrations.length > 0
logger.info( ? `- Migrations.json has been generated.`
`- there are no migrations to run, so migrations.json has not been created.` : `- There are no migrations to run, so migrations.json has not been created.`,
); ],
} });
logger.info(`NX Next steps:`);
logger.info(
`- Make sure package.json changes make sense and then run '${pmc.install}'`
);
if (migrations.length > 0) {
logger.info(`- Run '${pmc.exec} nx migrate --run-migrations'`);
}
logger.info(`- To learn more go to https://nx.dev/using-nx/updating-nx`);
if (showConnectToCloudMessage()) { output.log({
const cmd = pmc.run('nx', 'connect-to-nx-cloud'); title: 'Next steps:',
logger.info( bodyLines: [
`- You may run '${cmd}' to get faster builds, GitHub integration, and more. Check out https://nx.app` `- Make sure package.json changes make sense and then run '${pmc.install}',`,
); ...(migrations.length > 0
} ? [`- Run '${pmc.exec} nx migrate --run-migrations'`]
: []),
`- To learn more go to https://nx.dev/using-nx/updating-nx`,
...(showConnectToCloudMessage()
? [
`- You may run '${pmc.run(
'nx',
'connect-to-nx-cloud'
)}' to get faster builds, GitHub integration, and more. Check out https://nx.app`,
]
: []),
],
});
} catch (e) { } catch (e) {
logger.error(`NX The migrate command failed.`); output.error({
title: `The migrate command failed.`,
});
throw e; throw e;
} }
} }
@ -891,9 +901,9 @@ function showConnectToCloudMessage() {
function runInstall() { function runInstall() {
const pmCommands = getPackageManagerCommand(); const pmCommands = getPackageManagerCommand();
logger.info( output.log({
`NX Running '${pmCommands.install}' to make sure necessary packages are installed` title: `Running '${pmCommands.install}' to make sure necessary packages are installed`,
); });
execSync(pmCommands.install, { stdio: [0, 1, 2] }); execSync(pmCommands.install, { stdio: [0, 1, 2] });
} }
@ -965,9 +975,9 @@ export async function executeMigrations(
} }
logger.info(`---------------------------------------------------------`); logger.info(`---------------------------------------------------------`);
} catch (e) { } catch (e) {
logger.error( output.error({
`NX Failed to run ${m.name} from ${m.package}. This workspace is NOT up to date!` title: `Failed to run ${m.name} from ${m.package}. This workspace is NOT up to date!`,
); });
throw e; throw e;
} }
} }
@ -990,10 +1000,11 @@ async function runMigrations(
runInstall(); runInstall();
} }
logger.info( output.log({
`NX Running migrations from '${opts.runMigrations}'` + title:
(shouldCreateCommits ? ', with each applied in a dedicated commit' : '') `Running migrations from '${opts.runMigrations}'` +
); (shouldCreateCommits ? ', with each applied in a dedicated commit' : ''),
});
const migrations: { const migrations: {
package: string; package: string;
@ -1011,13 +1022,13 @@ async function runMigrations(
); );
if (migrationsWithNoChanges.length < migrations.length) { if (migrationsWithNoChanges.length < migrations.length) {
logger.info( output.success({
`NX Successfully finished running migrations from '${opts.runMigrations}'. This workspace is up to date!` title: `Successfully finished running migrations from '${opts.runMigrations}'. This workspace is up to date!`,
); });
} else { } else {
logger.info( output.success({
`NX No changes were made from running '${opts.runMigrations}'. This workspace is up to date!` title: `No changes were made from running '${opts.runMigrations}'. This workspace is up to date!`,
); });
} }
} }

View File

@ -1,11 +1,14 @@
import { logger } from '../utils/logger';
import { handleErrors } from '../utils/params'; import { handleErrors } from '../utils/params';
import * as migrationsJson from '../../migrations.json'; import * as migrationsJson from '../../migrations.json';
import { executeMigrations } from './migrate'; import { executeMigrations } from './migrate';
import { output } from '../utils/output';
export async function repair(args: { verbose: boolean }) { export async function repair(
args: { verbose: boolean },
extraMigrations = [] as any[]
) {
return handleErrors(args['verbose'], async () => { return handleErrors(args['verbose'], async () => {
const migrations = Object.entries(migrationsJson.generators).map( const nxMigrations = Object.entries(migrationsJson.generators).map(
([name, migration]) => { ([name, migration]) => {
return { return {
package: 'nx', package: 'nx',
@ -16,6 +19,8 @@ export async function repair(args: { verbose: boolean }) {
} as const; } as const;
} }
); );
const migrations = [...nxMigrations, ...extraMigrations];
const migrationsThatMadeNoChanges = await executeMigrations( const migrationsThatMadeNoChanges = await executeMigrations(
process.cwd(), process.cwd(),
migrations, migrations,
@ -25,13 +30,13 @@ export async function repair(args: { verbose: boolean }) {
); );
if (migrationsThatMadeNoChanges.length < migrations.length) { if (migrationsThatMadeNoChanges.length < migrations.length) {
logger.info( output.success({
`NX Successfully repaired your configuration. This workspace is up to date!` title: `Successfully repaired your configuration. This workspace is up to date!`,
); });
} else { } else {
logger.info( output.success({
`NX No changes were necessary. This workspace is up to date!` title: `No changes were necessary. This workspace is up to date!`,
); });
} }
}); });
} }