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): show link to migrate detail page in --interactive mode #29874

Merged
merged 1 commit into from
Feb 5, 2025
Merged
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
85 changes: 54 additions & 31 deletions packages/nx/src/command-line/migrate/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,17 @@ export class Migrator {
);
for (const packageToCheck of packagesToCheck) {
const filteredUpdates: Record<string, PackageUpdate> = {};
for (const packageUpdate of packageToCheck.updates) {
for (const [packageUpdateKey, packageUpdate] of Object.entries(
packageToCheck.updates
)) {
if (
this.areRequirementsMet(packageUpdate.requires) &&
(!this.interactive ||
(await this.runPackageJsonUpdatesConfirmationPrompt(packageUpdate)))
(await this.runPackageJsonUpdatesConfirmationPrompt(
packageUpdate,
packageUpdateKey,
packageToCheck.package
)))
) {
Object.entries(packageUpdate.packages).forEach(([name, update]) => {
filteredUpdates[name] = update;
Expand All @@ -243,7 +249,7 @@ export class Migrator {
): Promise<
{
package: string;
updates: PackageJsonUpdates[string][];
updates: PackageJsonUpdates;
}[]
> {
let targetVersion = target.version;
Expand Down Expand Up @@ -293,11 +299,11 @@ export class Migrator {
migrationConfig
);

if (!packageJsonUpdates.length) {
if (!Object.keys(packageJsonUpdates).length) {
return [];
}

const shouldCheckUpdates = packageJsonUpdates.some(
const shouldCheckUpdates = Object.values(packageJsonUpdates).some(
(packageJsonUpdate) =>
(this.interactive && packageJsonUpdate['x-prompt']) ||
Object.keys(packageJsonUpdate.requires ?? {}).length
Expand All @@ -307,7 +313,7 @@ export class Migrator {
return [{ package: targetPackage, updates: packageJsonUpdates }];
}

const packageUpdatesToApply = packageJsonUpdates.reduce(
const packageUpdatesToApply = Object.values(packageJsonUpdates).reduce(
(m, c) => ({ ...m, ...c.packages }),
{} as Record<string, PackageUpdate>
);
Expand Down Expand Up @@ -336,7 +342,7 @@ export class Migrator {
targetVersion: string,
migrationConfig: ResolvedMigrationConfiguration
): {
packageJsonUpdates: PackageJsonUpdates[string][];
packageJsonUpdates: PackageJsonUpdates;
packageGroupOrder: string[];
} {
const packageGroupOrder: string[] =
Expand All @@ -350,7 +356,7 @@ export class Migrator {
!migrationConfig.packageJsonUpdates ||
!this.getPkgVersion(packageName)
) {
return { packageJsonUpdates: [], packageGroupOrder };
return { packageJsonUpdates: {}, packageGroupOrder };
}

const packageJsonUpdates = this.filterPackageJsonUpdates(
Expand Down Expand Up @@ -416,10 +422,12 @@ export class Migrator {
packageJsonUpdates: PackageJsonUpdates,
packageName: string,
targetVersion: string
): PackageJsonUpdates[string][] {
const filteredPackageJsonUpdates: PackageJsonUpdates[string][] = [];
): PackageJsonUpdates {
const filteredPackageJsonUpdates: PackageJsonUpdates = {};

for (const packageJsonUpdate of Object.values(packageJsonUpdates)) {
for (const [packageJsonUpdateKey, packageJsonUpdate] of Object.entries(
packageJsonUpdates
)) {
if (
!packageJsonUpdate.packages ||
this.lt(packageJsonUpdate.version, this.getPkgVersion(packageName)) ||
Expand Down Expand Up @@ -456,7 +464,7 @@ export class Migrator {
}
if (Object.keys(filtered).length) {
packageJsonUpdate.packages = filtered;
filteredPackageJsonUpdates.push(packageJsonUpdate);
filteredPackageJsonUpdates[packageJsonUpdateKey] = packageJsonUpdate;
}
}

Expand Down Expand Up @@ -563,7 +571,9 @@ export class Migrator {
}

private async runPackageJsonUpdatesConfirmationPrompt(
packageUpdate: PackageJsonUpdates[string]
packageUpdate: PackageJsonUpdates[string],
packageUpdateKey: string,
packageName: string
): Promise<boolean> {
if (!packageUpdate['x-prompt']) {
return Promise.resolve(true);
Expand All @@ -574,26 +584,39 @@ export class Migrator {
return Promise.resolve(false);
}

return await prompt([
{
name: 'shouldApply',
type: 'confirm',
message: packageUpdate['x-prompt'],
initial: true,
},
]).then(({ shouldApply }: { shouldApply: boolean }) => {
this.promptAnswers[promptKey] = shouldApply;
const promptConfig = {
name: 'shouldApply',
type: 'confirm',
message: packageUpdate['x-prompt'],
initial: true,
};

if (
!shouldApply &&
(!this.minVersionWithSkippedUpdates ||
lt(packageUpdate.version, this.minVersionWithSkippedUpdates))
) {
this.minVersionWithSkippedUpdates = packageUpdate.version;
}
if (packageName.startsWith('@nx/')) {
// @ts-expect-error -- enquirer types aren't correct, footer does exist
promptConfig.footer = () =>
chalk.dim(
` View migration details at https://nx.dev/nx-api/${packageName.replace(
'@nx/',
''
)}#${packageUpdateKey.replace(/[-\.]/g, '')}packageupdates`
);
}

return shouldApply;
});
return await prompt([promptConfig]).then(
({ shouldApply }: { shouldApply: boolean }) => {
this.promptAnswers[promptKey] = shouldApply;

if (
!shouldApply &&
(!this.minVersionWithSkippedUpdates ||
lt(packageUpdate.version, this.minVersionWithSkippedUpdates))
) {
this.minVersionWithSkippedUpdates = packageUpdate.version;
}

return shouldApply;
}
);
}

private getPackageUpdatePromptKey(
Expand Down