Skip to content

Commit

Permalink
feat(core): show link to migrate detail page in --interactive mode (#…
Browse files Browse the repository at this point in the history
…29874)

(cherry picked from commit 781b300)
  • Loading branch information
MaxKless authored and FrozenPandaz committed Feb 6, 2025
1 parent 04efd60 commit aec9215
Showing 1 changed file with 54 additions and 31 deletions.
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

0 comments on commit aec9215

Please sign in to comment.