Skip to content

Commit 781b300

Browse files
authored
feat(core): show link to migrate detail page in --interactive mode (#29874)
1 parent a6e3833 commit 781b300

File tree

1 file changed

+54
-31
lines changed

1 file changed

+54
-31
lines changed

packages/nx/src/command-line/migrate/migrate.ts

Lines changed: 54 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,17 @@ export class Migrator {
216216
);
217217
for (const packageToCheck of packagesToCheck) {
218218
const filteredUpdates: Record<string, PackageUpdate> = {};
219-
for (const packageUpdate of packageToCheck.updates) {
219+
for (const [packageUpdateKey, packageUpdate] of Object.entries(
220+
packageToCheck.updates
221+
)) {
220222
if (
221223
this.areRequirementsMet(packageUpdate.requires) &&
222224
(!this.interactive ||
223-
(await this.runPackageJsonUpdatesConfirmationPrompt(packageUpdate)))
225+
(await this.runPackageJsonUpdatesConfirmationPrompt(
226+
packageUpdate,
227+
packageUpdateKey,
228+
packageToCheck.package
229+
)))
224230
) {
225231
Object.entries(packageUpdate.packages).forEach(([name, update]) => {
226232
filteredUpdates[name] = update;
@@ -243,7 +249,7 @@ export class Migrator {
243249
): Promise<
244250
{
245251
package: string;
246-
updates: PackageJsonUpdates[string][];
252+
updates: PackageJsonUpdates;
247253
}[]
248254
> {
249255
let targetVersion = target.version;
@@ -293,11 +299,11 @@ export class Migrator {
293299
migrationConfig
294300
);
295301

296-
if (!packageJsonUpdates.length) {
302+
if (!Object.keys(packageJsonUpdates).length) {
297303
return [];
298304
}
299305

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

310-
const packageUpdatesToApply = packageJsonUpdates.reduce(
316+
const packageUpdatesToApply = Object.values(packageJsonUpdates).reduce(
311317
(m, c) => ({ ...m, ...c.packages }),
312318
{} as Record<string, PackageUpdate>
313319
);
@@ -336,7 +342,7 @@ export class Migrator {
336342
targetVersion: string,
337343
migrationConfig: ResolvedMigrationConfiguration
338344
): {
339-
packageJsonUpdates: PackageJsonUpdates[string][];
345+
packageJsonUpdates: PackageJsonUpdates;
340346
packageGroupOrder: string[];
341347
} {
342348
const packageGroupOrder: string[] =
@@ -350,7 +356,7 @@ export class Migrator {
350356
!migrationConfig.packageJsonUpdates ||
351357
!this.getPkgVersion(packageName)
352358
) {
353-
return { packageJsonUpdates: [], packageGroupOrder };
359+
return { packageJsonUpdates: {}, packageGroupOrder };
354360
}
355361

356362
const packageJsonUpdates = this.filterPackageJsonUpdates(
@@ -416,10 +422,12 @@ export class Migrator {
416422
packageJsonUpdates: PackageJsonUpdates,
417423
packageName: string,
418424
targetVersion: string
419-
): PackageJsonUpdates[string][] {
420-
const filteredPackageJsonUpdates: PackageJsonUpdates[string][] = [];
425+
): PackageJsonUpdates {
426+
const filteredPackageJsonUpdates: PackageJsonUpdates = {};
421427

422-
for (const packageJsonUpdate of Object.values(packageJsonUpdates)) {
428+
for (const [packageJsonUpdateKey, packageJsonUpdate] of Object.entries(
429+
packageJsonUpdates
430+
)) {
423431
if (
424432
!packageJsonUpdate.packages ||
425433
this.lt(packageJsonUpdate.version, this.getPkgVersion(packageName)) ||
@@ -456,7 +464,7 @@ export class Migrator {
456464
}
457465
if (Object.keys(filtered).length) {
458466
packageJsonUpdate.packages = filtered;
459-
filteredPackageJsonUpdates.push(packageJsonUpdate);
467+
filteredPackageJsonUpdates[packageJsonUpdateKey] = packageJsonUpdate;
460468
}
461469
}
462470

@@ -563,7 +571,9 @@ export class Migrator {
563571
}
564572

565573
private async runPackageJsonUpdatesConfirmationPrompt(
566-
packageUpdate: PackageJsonUpdates[string]
574+
packageUpdate: PackageJsonUpdates[string],
575+
packageUpdateKey: string,
576+
packageName: string
567577
): Promise<boolean> {
568578
if (!packageUpdate['x-prompt']) {
569579
return Promise.resolve(true);
@@ -574,26 +584,39 @@ export class Migrator {
574584
return Promise.resolve(false);
575585
}
576586

577-
return await prompt([
578-
{
579-
name: 'shouldApply',
580-
type: 'confirm',
581-
message: packageUpdate['x-prompt'],
582-
initial: true,
583-
},
584-
]).then(({ shouldApply }: { shouldApply: boolean }) => {
585-
this.promptAnswers[promptKey] = shouldApply;
587+
const promptConfig = {
588+
name: 'shouldApply',
589+
type: 'confirm',
590+
message: packageUpdate['x-prompt'],
591+
initial: true,
592+
};
586593

587-
if (
588-
!shouldApply &&
589-
(!this.minVersionWithSkippedUpdates ||
590-
lt(packageUpdate.version, this.minVersionWithSkippedUpdates))
591-
) {
592-
this.minVersionWithSkippedUpdates = packageUpdate.version;
593-
}
594+
if (packageName.startsWith('@nx/')) {
595+
// @ts-expect-error -- enquirer types aren't correct, footer does exist
596+
promptConfig.footer = () =>
597+
chalk.dim(
598+
` View migration details at https://nx.dev/nx-api/${packageName.replace(
599+
'@nx/',
600+
''
601+
)}#${packageUpdateKey.replace(/[-\.]/g, '')}packageupdates`
602+
);
603+
}
594604

595-
return shouldApply;
596-
});
605+
return await prompt([promptConfig]).then(
606+
({ shouldApply }: { shouldApply: boolean }) => {
607+
this.promptAnswers[promptKey] = shouldApply;
608+
609+
if (
610+
!shouldApply &&
611+
(!this.minVersionWithSkippedUpdates ||
612+
lt(packageUpdate.version, this.minVersionWithSkippedUpdates))
613+
) {
614+
this.minVersionWithSkippedUpdates = packageUpdate.version;
615+
}
616+
617+
return shouldApply;
618+
}
619+
);
597620
}
598621

599622
private getPackageUpdatePromptKey(

0 commit comments

Comments
 (0)